Monday, January 26, 2015

BDD and SpecFlow for .NET

SpecFlow is a tool for creating automated user acceptance tests in .NET. It consumes tests written in the Gherkin syntax. The syntax is as follows:




Given 20 races
  And 5 of them failed with the same engine issue
When preparing for another race
Then a failure rate of over .10 should trigger an investigation into the cause of the engine failures




This test is parsed and a unit test is generated. At that point it's up to the test engineer/developer to implement the unit test. The unit tests are the mechanism by which the acceptance tests are run. You can choose from a variety of common rest engines.


SpecFlow is available as a Visual Studio extension. After the extension is installed, it's a nuget package to add it to a test project. From there, you add a feature file which contains the specification. Then, you need to generate the steps file which contains the implementation.


The value of doing tests this way lies both the automation of acceptance tests and in the format of the specification. The Given-When-Then syntax ensures that all specs follow a specific syntax. If implemented properly, these tests provide a reliable and testable way to proof the code against the requirements.


These tests must be supplemented by unit tests that further flesh out the details of the code. They should not serve as tests for low-level details. They should be written by and/or with the business - hence the value in the ubiquitous language syntax. They should also use domain terms for entities and processes. This will help define and communicate the domain specific language.


An example entity:


Driver
|Name|Car|
|Ricky-Bobby|FirstNotLast|


RaceHistory
|Date|Track|RaceName|Result|
|1/1/10|Daytona|Daytona500|Crash|
|1/1/11|Wis|Cheese300|First|


These Entities are passed to the test context and can be used to drive the tests.



No comments:

Post a Comment