One of my roles in my paid employment is monitoring, testing and automation. I have a stack of PowerShell scripts I use for this, as well as a couple of C# programs that use Selenium.
I was surfing the Internet this week (AKA professional development) when I chanced on an article about SpecFlow. SpecFlow is an open-source tool that helps you write and execute automated tests using the Gherkin language. If this wasn't the entrance to a deep rabbit-hole I haven't seen one!
[Cue White Rabbit by Jefferson Airplane Jefferson Airplane -White Rabbit- (youtube.com)]
OK, what is Gherkin? Gherkin is a domain-specific language for writing use cases for a software system in plain language. It promotes behaviour driven development and allows developers, managers and business officials involved in the process to clearly understand the requirements of the project and the life cycle. Gherkin’s text also acts as documentation and a skeleton of the automated tests.
OK, what is behaviour driven development? Behaviour-driven development (BDD) involves naming software tests using business domain language that describes the behaviour of the code.
So now we know that SpecFlowis a C# library that facilitates BDD. How does it work?
You install the SpecFlow package into your IDE and add a folder called Features. In this folder create a file called <Your Feature>.feature. I was writing tests for my website, admoss.info, so I named my file admoss.feature .
Inside the file I wrote:
Feature: admoss.info website
Scenario: Check if site exists
Given I have loaded the site
Then I check the title
Scenario: I press the Resume button
Given I have loaded the site
When I click the Resume button
Then The Resume page is displayed
Scenario: I quit the Web Driver
Given I shutdown the Webdriver
With the SpecFlow plugin installed in my IDE I then.... went further down the rabbit-hole.
[Cue White Rabbit by Jefferson Airplane Jefferson Airplane -White Rabbit- (youtube.com)] again.
It seems SpecFlow has not been updated since 2023. The company that created it was acquired by another company. This halted development. I use JetBrains Rider as an IDE and the SpecFlow plugin won't work with Rider versions after 2023.
The open-source community responded to the situation by forking SpecFlow as Reqnroll. Reqnroll is an open-source Cucumber-style BDD test automation framework for .NET. It has been created as a reboot of the SpecFlow project.
(This rabbit hole is deep. Suffice to say Cucumber is the Unix world's version of BDD)
Reqnroll started as a clone of SpecFlow so all the on-line tutorials and documentation still apply. I deleted the SpecFlow package from my IDE and installed Reqnroll in its place.
[Given(@"I have loaded the site")]
public void GivenIHaveLoadedTheSite()
{
_edgeDriver.Url = "https://show.admoss.info";
_edgeDriver.Navigate();
}
to the .cs file. That is all there is to it! Well, not really. I had to fill in the template with the C# that actually does things:
_edgeDriver.Url = "https://show.admoss.info";
_edgeDriver.Navigate();
I repeated the above for the line Then I check the title
[Then(@"I check the title")]
public void WhenILookAtTheTitle()
{
try
{
Assert.AreEqual("David Moss", _edgeDriver.Title);
_edgeDriver.Quit();
}
catch (Exception e)
{
Console.WriteLine(e);
}
_edgeDriver.Quit();
}
How easy was that!
I went from a requirements file, that could have been written in Gherkin by the most tech-illiterate business user, to a C# unit test smoothly and easily.
What is more, the steps we defined and coded are re-usable.
Notice that Given I have loaded the site step is used two of the scenarios. I only had to fill in the C# code once. I can re-use that step in as many scenarios as I want.
I have only scratched the surface of Behaviour Driven Design with SpecFlow ReqnRoll in this blog post. Once I am more familiar with it, I will become a BDD evangelist at work.
I suppose I should end with something about why it is called Behaviour Driven Design.
The idea is that a business user writes the behaviours expected by the system in Gherkin.
The programmer then adds the steps captured in the requirements file to the steps file.
At this stage all the tests will fail. That's OK. Not only have real tests not been written yet, but the system being tested hasn't either! But the tests will run.
As features are added to the system, the programmer adds real C# code to test them to the steps file. Now some tests will (hopefully) pass. The tests for unimplemented features will continue to fail.
Testing now becomes a measure of progress in the development project.
If a test fails it drives effort to either implement the feature as described by the business or fix the bug that is causing it to fail. The development process is driven by the desired behaviour of the system.
Finally, the test results end up in cryptic .trx files. (test results in xml)
There are utilities available that will translate these .trx files to HTML. Some of these utilities are pretty basic, others include nifty graphics showing progress. I used a basic one called trxlog2html.