Wednesday, May 7, 2014

ATDD (Acceptance Test Driven Development) or BDD using SpecFlow and Fluent Assertions


Specflow is popular framework for acceptance test driven development. The aim of specflow to provide interface where both domain experts and developers can work together. The domain expert can write acceptance test in business readable language and specflow provides easy interface to generate ATDD/BDD test from that. SpecFlow is inspired from the Cucumber in Ruby. To know details of SpecFlow follow this url http://www.specflow.org/

Fluent Assertions is a simple ways of writing assertions in a more natural , more readable  and more expressive manner. You can use Fluent Assertions with any unit testing frameworks i. e. NUnit, MSTest, XUnit or Mb-Unit. You just need to add reference to that unit test framework in your project. Fluent Assertions contains huge number of extensions methods even for throwing exceptions.  There have good documentation on supported extension methods here https://fluentassertions.codeplex.com/documentation

Preparing Development Environment:

a) Install Nuget packege for SpecFlow with NUnit:
If you want to use SpecFlow with NUnit you can install the following nuget package manager. However, you can also install SpecFlow and Nunit package separately.
image

b) Install Fluent Assertion : you will also get dll of Fluent Assertions from nuget.
image

c) Install Visual Studio 2010 features : You can write BDD code with these packages. But, this will not generate NUnit test code. You will need .feature files where domain expert can write acceptance test. Also from .feature file NUnit code will be generated. So you will need to install visual studio extension to add feature files. http://visualstudiogallery.msdn.microsoft.com/9915524d-7fb0-43c3-bb3c-a8a14fbd40ee

Development Steps:

When you will install specflow visual studio extension then you will be able to add feature file in your project.
image
.feature file provide domain experts to collaborate with acceptance tests in normal and readable language. However, you can also first write your unit test code and after that you can integrate your code with feature definitions. Here is one example of feature file. The acceptance tests are written in natural language with Given.. When.. Then format. Here is an example of adding task in Kanban board. Kanban service responsibility is to add a new task to Kanban board.
image

But you may not want to write all test cases at a time for this acceptance scenario. In such case you can comment out one of the expected result and you can generate test for this later.
image

This will help to develop in TDD style development. Where developer want to write code for passing those acceptance test.
Specflow also make it easy to generate step definitions from this feature file. Just right click on the scenario and generate step definitions
image

When you will generate code from specflow definitions then it will create functions for each steps.
image

There have no NUnit test attribute then how NUnit can understand these attribute and test? If you open the .feature.cs file then you will see auto generated code for NUnit framework
image

As we are writing code test assertions using Fluent assertions so it is independent of NUnit attribute. Lets now write code for all these pending functions to pass the scenario tests.
image

You can use BeforeScenario and AfterScenario function as Setup and CleanUp for unit testing. Also to communicate within scenario and to store the state of variable you can use ScenarioContext.  Which is very important feature for writing test within a scenario. It provides dictionary with key value pair to store records.
Also using fluent assertions you can compare your expected result easy way.
image

Now suppose a scenario where user will also provide status of task when it will create a ticket.  In this case most of the step definition of this scenario will be same as previous one but only when user will add task then user will also provide status. Do we need to generate all steps again from feature files?
A good feature of SpecFlow that you do not need to write same Given.. steps again. You can directly map the existing definitions with New scenario.
image
So for whole scenario you will need to add only two step definitions.
image

Lets also pass this scenario. What if you want to run only one scenario not both?. Then just comment one scenario from the feature file.
image
After writing and passing the test code also need to be re factored which is must for TDD .

Run Acceptance Tests:

As I mentioned that tested are actually auto generated for NUnit Framework. So using NUnit UI Runner we can check those tests. All my acceptance tests are written using SpecFlow and fluent assertions but as specflow is integrated with NUnit so it will generate dll which we can run through Nunit Runner. As many automated tools already support NUnit Tests results to display so it will be easy to integrate acceptance test results with TeamCity or other continuous integration tools.
image

If you are using Resharper then you can easily run and debug step definition within Visual Studio. As the Resharper supports NUnit by default so you do not need to install any plugin for that.

Generate Report:

If you used NUnit console before then you would have generated TestResults.xml which can be easily integrated with Automation tools. You will not  need spectflow help to generate this xml file. But if you want to generate report in specflow format then the following command will generate html file.
image

The output html will look like this :
image

I have tried TDD approach to pass those acceptance tests which provided me a skeleton for Kanban service. But there will have many features and progressively new scenarios will be added. 
source code : https://dl.dropboxusercontent.com/u/20275838/kanbanservice.rar