Behavior Driven Development (BDD)

Following on from my previous post on test-driven development, there is a closely related concept know as behavior-driven development. So the question is how does this differ from TDD, it does so in a few key ways.

Image from Testbytes

BDD like TDD consists of writing tests to ensure they pass to have the desired outcome, however as the name suggests the is a key focus on the behavior of the program. We can lead with TDD and run all the unit tests we want but the real question with BDD is “When I interact with my code is it behaving as I expect it to?”. To ensure our code is acting as it should we write tests that are commonly referred to as feature tests.

I guess the key take away here is to look at your program from the perspective of a user, when a user uses a feature does it produce their expected outcome? For example, a user goes to a cash point to withdraw money, we could have a test that confirms they have successfully withdrawn their money. But wait! has their balance been updated, has the cash machine ejected the card, did they have sufficient funds to start with? As you can guess this can be a lengthy process and BDD alone may not always provide us solutions to our problems. Not to fair we can use concepts from TDD and unit testing to help!

One way to break down these problems if by referring to user stories and writing the task out into plain old English. “The user inserts their cart to the reader and enters their pin, providing the pin matches grant access to the account. The user requests to withdraw £20, check to see if they have sufficient funds, if so dispense the money and eject the card”.

Hopefully this brief overview of BDD clarifies the differences it has between TDD, happy feature testing!

Check out this animation if your still confused, it does a great job of explaining BDD concepts.