Have your business analyst write unit tests

Publish date:

What if there was a clear way to describe business rules and have unit tests that are automatically created from the descriptions and prove that the code is correct?

A typical scenario in software projects:

  • A business analyst writes functional specifications in Word, or, if you are lucky, in Jira or VSTS. The do their best to write clear specifications and acceptance criteria for your new function.
  • A programmer gets this documentation, read it, ask questions during the poker session and builds the function requests.

If done well, the programmer will also create unit tests to prove that the code is correct. The programmer is responsible for the correctness of the test itself.

Computer code

Wouldn’t it be great if this could all be done by the business analyst? What if there was a clear way to describe business rules and have unit tests that are automatically created from the descriptions and prove that the code is correct? And all done by the analyst who knows the business rules?

If the business analyst created the unit tests, there would be no misunderstanding about the business rules and the acceptance criteria. The programmer would ‘simply’ need to make the unit tests succeed and we would know that the code is correct.

Enter Specflow.

The business analyst will use ‘natural language’ to describe features and criteria and Specflow will ensure that you will have a unit tests checking the code. Specflow uses the Gherkin language specification. For more information on Gherkin, go to the Gherkin reference page.

I’ve created an example of how this could work, using my favorite example app: Conway’s Game of Life. I could explain the rules, but you can find them on Wikipedia. Better sill, read the example from one of the Specflow feature files below.


Feature: LifeEvolution
	In order to calculate the board positions of the next iteration
	As a LifeEvolutionEngine
	I want to calculate the new status of a field

	# For more information about the Game of Life, go visit wikipedia https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
	# Rules in short:
	# Any live cell with fewer than two live neighbors dies, as if by under population.
	# Any live cell with two or three live neighbors lives on to the next generation.
	# Any live cell with more than three live neighbors dies, as if by overpopulation.
	# Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.

Scenario: Cell with fewer than two live neighbors die
	Given A Cell is Alive
	And A cell has the following neigbors:
	| North | North | NorthEast | East  | SouthEast | South | SouthWest | West  | NorthWest |
	| Alive | Dead  | Dead      | Dead  | Dead      | Dead  | Dead      | Dead  | Dead      |
	| Dead  | Alive | Dead      | Dead  | Dead      | Dead  | Dead      | Dead  | Dead      |
	| Dead  | Dead  | Alive     | Dead  | Dead      | Dead  | Dead      | Dead  | Dead      |
	| Dead  | Dead  | Dead      | Alive | Dead      | Dead  | Dead      | Dead  | Dead      |
	| Dead  | Dead  | Dead      | Dead  | Alive     | Dead  | Dead      | Dead  | Dead      |
	| Dead  | Dead  | Dead      | Dead  | Dead      | Alive | Dead      | Dead  | Dead      |
	| Dead  | Dead  | Dead      | Dead  | Dead      | Dead  | Alive     | Dead  | Dead      |
	| Dead  | Dead  | Dead      | Dead  | Dead      | Dead  | Dead      | Alive | Dead      |
	| Dead  | Dead  | Dead      | Dead  | Dead      | Dead  | Dead      | Dead  | Alive     |
	| Dead  | Dead  | Dead      | Dead  | Dead      | Dead  | Dead      | Dead  | Dead      |
	When I evaluate the cell's live
	Then The cell should be Dead

As you can see, the above specification is quite readable for a non-technical person. The cool thing is, Specflow will generate the unit test functions to cover the above scenario, so the programmer will have a flying start for creating backing unit tests. The ‘only’ thing the programmer has to do, is to implement the unit tests functions. See the code below for the ‘bare’ unit tests functions.


using System;
using TechTalk.SpecFlow;

namespace Life.Engine.Test
{
    [Binding]
    public class LifeEvolutionSteps
    {
        [Given(@"A Cell is Alive")]
        public void GivenACellIsAlive()
        {
            ScenarioContext.Current.Pending();
        }
        
        [Given(@"A cell has the following neigbors:")]
        public void GivenACellHasTheFollowingNeigbors(Table table)
        {
            ScenarioContext.Current.Pending();
        }
        
        [Given(@"A Cell is Dead")]
        public void GivenACellIsDead()
        {
            ScenarioContext.Current.Pending();
        }
        
        [When(@"I evaluate the cell's live")]
        public void WhenIEvaluateTheCellSLive()
        {
            ScenarioContext.Current.Pending();
        }
        
        [Then(@"The cell should be Dead")]
        public void ThenTheCellShouldBeDead()
        {
            ScenarioContext.Current.Pending();
        }
        
        [Then(@"The cell should be Alive")]
        public void ThenTheCellShouldBeAlive()
        {
            ScenarioContext.Current.Pending();
        }
    }
}

These unit tests will become part of your regular unit tests library and can be run at each build of your software. Specflow will maintain the mapping from the scenarios in your feature file to the unit tests functions. Basically, each line in a scenario will be mapped to a unit test function. You can introduce variables in the mappings, so reuse of these mappings is much easier.

If you are a business analyst, go through the Gherkin language specification. It is really easy to learn and will help you to describe the functions you desire from the system consistently. If you are a programmer, especially if you are accustomed to writing unit tests, Specflow will come very naturally to you.

For me, Specflow is one of the great finds of the last two years and it really helps me to bridge a gap between analysts and programmers. Please contact me if you want to know more about how we use Specflow to create better solutions for our customers.

Bart Roozendaal
+31625754345
bart.roozendaal@capgemini.com
https://www.linkedin.com/in/bartroozendaal/
https://www.capgemini.com/experts/cloud-services/bart-roozendaal

Related Posts

Integrator or deliberator: Will your business still be around in 2020?

Charlie Li
Date icon December 11, 2018

For businesses, digital transformation is divisive. Some are flourishing, finding new ways to...

The Cyber Security World: Understanding the Black Box

Gaonkar, Mahadev
Date icon December 10, 2018

The cybersecurity industry is moving at a rapid pace to ensure quick adoption of the new...

banking

Are you ready for data driven banking?

Rick Vermeer
Date icon December 7, 2018

Data makes the money go around: three reasons why banks should invest more in data now.

cookies.

By continuing to navigate on this website, you accept the use of cookies.

For more information and to change the setting of cookies on your computer, please read our Privacy Policy.

Close

Close cookie information