I’ve been working with BDD for a little while now in delivering a practical project, and have started to get a good sense of what is important. Initially I found that a lot of the articles and reading material made sense but were quite abstract. I agreed with the points made in a general sense, but had some questions around the actual practical application that weren’t necessarily answered.
Everything pulls from the defined scenarios
The main thing I like about the BDD workflow is that it gives you great structure around how to approach a project. Often when you first start something new it can be a little daunting to know where to begin – whether you start designing a schema, writing some unit tests (which tests, where do you start…), if you begin by writing up some user stories and apply an agile structure around it etc.
The great thing with BDD is that you are either writing some new features or scenarios using a plain English language, or you are implementing tests to deliver those scenarios. Every piece of code that you write and every test should be directly ‘pulling’ from the defined scenarios. As soon as you have finished implementing tests to prove a given scenario, you move on to the next. If you run out of scenarios, you either write some more scenarios for the current feature, or you begin defining a new feature. At any point in time, you pretty much know exactly what to do next.
How does BDD affect unit tests?
A big area of confusion that I initially had was how BDD affects unit testing – whether unit tests are effectively replaced by the natural language scenarios, and if you are testing more granular technical detail if you are meant to try to express this from an end user point of view with the scenarios etc.
A user’s experience with software is entirely based on the interactions they have with the user interface. Therefore it makes sense to define the expected behaviours from a user’s point of view, in terms of what the current situation is (Given), what action they take (When) and what the expected result is (Then).
It works quite well to have executable tests for these scenarios to test the user interface, and mock the dependencies. In my project this means testing the MVC controllers and action methods, but mocking the application services. Using a mocking framework you set up the expected behaviour from the dependencies in the ‘Given’ steps, the ‘When’ steps execute the action method on the controller, and the ‘Then’ steps verify things like the ActionResult and the ViewData.
You absolutely need unit tests to verify the behaviour of the lower level components, but the point is that the test scenarios that you write and the expected behaviours are all ‘pulling’ from the requirements in the defined BDD scenarios. If at any point you’re unsure of whether you have the correct set of test cases, you should refer back to the plain English scenarios, read through the expected behaviours, then ensure that your unit tests are set up to verify the correct thing.
This really means that the BDD automated acceptance tests and unit tests do both sit well together. The difference is that the BDD tests give the overall context that you’re working to, and provide a focal point about what you should be testing and why.
Language used in unit tests
BDD talks quite a lot about the importance of language used, and how the word ‘test’ implies checking something after the event. Unit tests are all about defining expected behaviours, so the word ‘should’ is much more appropriate. I have to admit, initially I quite seemed like a pretty unimportant distinction to me, and I didn’t quite get it. However I’m now totally convinced.
Previously I would have pretty much created a test fixture per class, and the unit tests names would have probably ended up being oriented around the technical implementation. For example, testing an application service might result in a test fixture called SecurityServiceTestFixture.
I’ve taken to creating a test fixture for each action or process, then starting each unit test method with ‘Should’ to express a particular facet of the expected behaviour. Using the same example, I might create a fixture called RegisteringANewUser and the unit test would be called something like ShouldSendAnEmailToVerifyTheUserEmailAddress.
Although this is quite a subtle distinction it makes it much easier to work out what test cases there should be, and to read back through and understand exactly which behaviours are being tested. It’s also easier to read the original scenario definition, read through your unit tests and get a sense of whether everything is covered.
Composition of a unit test
The actual way that a unit test is put together is pretty much unaffected – you’ll generally go with the same arrange, act assert pattern. The only change that I’ve really had to make is organising the test fixtures, and the language that I use for the test methods.
BDD Workflow
Putting all of this together gives you the following kind of workflow…
- You start with the Gherkin based scenarios defined in plain English
- You bind each scenario step to an executable method, and test your user interface
- During this process you use interfaces for any dependencies, and mock these to verify the UI behaviour
- Once the UI tests are passing, you look at each dependency and write lower level unit tests to cover the expected behaviours
- For each dependency, you look at any further dependencies and write unit tests to cover these additional components
- Work to get each unit test passing
- If at any point you get confused about which unit test cases to write, refer back to the scenario definitions to have a clear set of requirements about the expected behaviours
- Either move on to the next scenario, or define some more scenarios
