BDD for C# NUnit

后端 未结 5 1712
甜味超标
甜味超标 2020-12-31 12:09

I\'ve been using a home brewed BDD Spec extension for writing BDD style tests in NUnit, and I wanted to see what everyone thought. Does it add value? Does is suck? If so why

5条回答
  •  醉梦人生
    2020-12-31 12:36

    I've been giving this sort of question a lot of though recently. There are a lot of reasonable options out there, and you can create your own easily, as displayed in some of the answers in this post. I've been working on a BDD testing framework with the intent being to make it easily extended to any unit testing framework. I currently support MSTest and NUnit. Its called Given, and it's opensource. The basic idea is pretty simple, Given provides wrappers for common sets of functionality which can then be implemented for each test runner.

    The following is an example of an NUnit Given test:

    [Story(AsA = "car manufacturer",
           IWant = "a factory that makes the right cars",
           SoThat = "I can make money")]
    public class when_building_a_toyota : Specification
    {
        static CarFactory _factory;
        static Car _car;
    
        given a_car_factory = () =>
                                  {
                                      _factory = new CarFactory();
                                  };
    
        when building_a_toyota = () => _car = _factory.Make(CarType.Toyota);
    
        [then]
        public void it_should_create_a_car()
        {
            _car.ShouldNotBeNull();
        }
    
        [then]
        public void it_should_be_the_right_type_of_car()
        {
            _car.Type.ShouldEqual(CarType.Toyota);
        }
    }
    

    I tried my best to stay true to the concepts from Dan North's Introducting BDD blog, and as such, everything is done using the given, when, then style of specification. The way it is implemented allows you to have multiple givens and even multiple when's, and they should be executed in order (still checking into this).

    Additionally, there is a full suite of Should extensions included directly in Given. This enables things like the ShouldEqual() call seen above, but is full of nice methods for collection comparison and type comparison, etc. For those of you familiar with MSpec, i basically ripped them out and made some modifications to make them work outside of MSpec.

    The payoff, though, I think, is in the reporting. The test runner is filled with the scenario you've created, so that at a glance you can get details about what each test is actually doing without diving into the code: Test Runner

    Additionally, an HTML report is created using t4 templating based on the results of the tests for each assembly. Classes with matching stories are all nested together, and each scenario name is printed for quick reference. For the above tests the report would look like this: Report Example

    Failed tests would be colored red and can be clicked to view the exception details.

    That's pretty much it. I'm using it in several projects I'm working on, so it is still being actively developed, but I'd describe the core as pretty stable. I'm looking at a way to share contexts by composition instead of inheritance, so that will likely be one of the next changes coming down the pike. Bring on the criticism. :)

提交回复
热议问题