I thought I would share with you a recent conversion to SpecFlow of some automated tests that we were running in “pure” NUnit, in order to demonstrate the value of using a Domain Specific Language (DSL). See below for some example code and screenshots.
Pure NUnit Automated Tests:
Tests in SpecFlow:
SpecFlow uses the same Gherkin language as Cucumber, the first automation tool I was aware of that promoted Behaviour Driven Development (BDD). I think you will agree, the SpecFlow tests are more readable. This is primarily because the reader is not looking at code, and as a result the intent of the tests is clearer. Anyone can understand these SpecFlow tests; no code knowledge is required because it is completely abstracted. There is some good abstraction and naming of the NUnit tests but some code knowledge is still required to understand their purpose.
I was recently made aware of the use of “Background” within SpecFlow. This removes the repetition of the “Given” statement, which is the same in every scenario, making it even more readable and maintainable. Basically it supports the DRY principle (Don’t Repeat Yourself):
Feature: Buy Ticket On Smartcard In order to travel using a smartcard As a Seasons website customer I want to be able to buy a ticket on a smartcard Background: Given I am logged in to a Smart Seasons website @NormalScenario Scenario: Buy weekly ticket on existing smartcard When I buy a "Weekly" ticket on my existing smartcard Then The ticket is added to the smartcard @NormalScenario Scenario: Buy monthly ticket on existing smartcard When I buy a "Monthly" ticket on my existing smartcard Then The ticket is added to the smartcard // ... @SmokeTest Scenario Outline: Buying tickets on a smartcard When I buy a "<ticket_period>" ticket on my <smartcard_status> smartcard Then The ticket is added to the smartcard Examples: | ticket_period | smartcard_status | | Weekly | new | | Monthly | existing |
Note also the use of tags (@NormalScenario) which enables tests to be grouped for execution, both within NUnit Test Sessions in Visual Studio and running post-deployment using NAnt scripts.
Best of all, SpecFlow neatly integrates with .NET, Visual Studio and NUnit. I’m a fan!