GithubHelp home page GithubHelp logo

gbasi / kekiri Goto Github PK

View Code? Open in Web Editor NEW

This project forked from chris-peterson/kekiri

0.0 1.0 0.0 645 KB

A low-friction BDD framework

License: MIT License

C# 99.52% Shell 0.24% Batchfile 0.24%

kekiri's Introduction

Kekiri

A .NET framework that supports writing low-ceremony BDD tests using Gherkin language.

Kekiri honors the conventions of the [cucumber language] (https://github.com/cucumber/cucumber/wiki/Feature-Introduction)

Setup

PM> Install-Package Kekiri

Why Kekiri?

Unlike other BDD frameworks that impose process overhead (management of feature files, custom tools, etc) Kekiri allows developers to write BDD tests just as quickly and easily as they would technical tests.

The resulting tests are concise, highly portable, and adhere to Act, Arrange, and Assert.

Example

For this Test, we will be implementing a basic calculator.

Start with the test

    class Adding_two_numbers : Test {
        [Given]
        public void Given_a_calculator() {}

        [Given]
        public void The_user_enters_50() {}

        [Given]
        public void Next_the_user_enters_70() {}

        [When]
        public void When_the_user_presses_add() { throw new NotImplementedException(); }

        [Then]
        public void The_screen_should_display_result_of_120() {}
    }

If we were to run this test (even though it fails) we get a nice Cucumber-style feature output:

    Scenario: Adding two numbers
    Given a calculator
        And the user enters 50
        And next the user enters 70
    When the user presses add
    Then the screen should display result of 120

Add the implementation

    class Adding_two_numbers : Test {
        private Calculator _calculator;

        [Given]
        public void Given_a_calculator() {
            _calculator = new Calculator();
        }

        [Given]
        public void The_user_enters_50() {
            _calculator.Operand1 = 50;
        }

        [Given]
        public void Next_the_user_enters_70() {
            _calculator.Operand2 = 70;
        }

        [When]
        public void When_the_user_presses_add() {
            _calculator.Add();
        }

        [Then]
        public void The_screen_should_display_result_of_120() {
            Assert.AreEqual(120, _calculator.Result);
        }
    }

    class Calculator {
        public decimal Operand1 { get; set; }
        public decimal Operand2 { get; set; }

        public decimal Result { get; set; }

        public void Add() {
            Result = Operand1 + Operand2;
        }
    }

Supported Naming Conventions

Kekiri supports both Pascal case conventions (e.g. WhenDoingTheThing) just as it does underscore convention (e.g. When_doing_the_thing).


.feature file output

Kekiri is capable of generating .feature files when running tests. To do so, decorate your test fixtures with [Scenario(Feature.X)]. The names of your features will be the names of the generated .feature files.


Wiki

More info available here

Other common use cases

Expected Exceptions

    class Divide_by_zero : Test {
        readonly Calculator _calculator = new Calculator();

        [When, Throws]
        public void When_dividing() {
            _calculator.Divide();
        }

        [Then]
        public void It_should_throw_an_exception() {
            Catch<DivideByZeroException>();
        }
    }

Notice, here we've used the [Throws] attribute to inform the Test that throwing an exception is the expected behavior. In 1 or more [Then]s, the thrown type of exception must be caught (using the templated method Catch<>).

Scenario: Divide by zero When dividing Then it should throw an exception

Data-driven

    [Example(12, 5, 7)]
    [Example(20, 5, 15)]
    public class Eating_cucumbers : Test {
        private readonly int _start;
        private readonly int _eat;
        private readonly int _left;
        private int _cucumbers;

        public Eating_cucumbers(int start, int eat, int left) {
            _start = start;
            _eat = eat;
            _left = left;
        }

        [Given]
        public void Given_there_are_START_cucumbers() {
            _cucumbers = _start;
        }

        [When]
        public void When_I_eat_EAT_cucumbers() {
            _cucumbers -= _eat;
        }

        [Then]
        public void I_should_have_LEFT_cucumbers() {
            Assert.AreEqual(_left, _cucumbers);
        }
    }
    Scenario Outline: eating
    Given there are 12 cucumbers
    When i eat 5 cucumbers
    Then i should have 7 cucumbers

For more advanced topics, check out the wiki.

Contributing

  1. Fork it
  2. Create your feature branch git checkout -b my-new-feature
  3. Commit your changes git commit -am 'Added some feature'
  4. Push to the branch git push origin my-new-feature
  5. Create new Pull Request

Acknowledgements

Kekiri uses and is influenced by the following open source projects:

kekiri's People

Contributors

andyalm avatar jorbor avatar chris-peterson avatar kcamp avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.