Keeping your codebase as cool as a Cucumber
What is Behaviour Driven Development (BDD)?
(Photo Credit: It's a Delivery Thing)
- Behaviour Driven Development is a software methodology in which an application is specified and designed by describing how its behaviour should appear to an outside observer.
- Simply put, it's a methodolgy that ensures a product is built the way the domain expert expects it to behave
U w0t Mate
- Imagine we are making a simple calculator that only adds two small numbers together
- We can assume that we display: 2 + 2 = 4
- Actually our domain expert expected our calculator to display: 2 + 2 = FOUR
- I'll give you an example. Our domain expert has asked us to build a basic calculator that adds two numbers together
- That's simple enough that we, as developers, can make some basic assumptions. So let's assume when entering 2 + 2 we want to see it as 2 + 2 = 4 as all integers
- But what if our domain expert expected to see 2 + 2 = FOUR, so an integer + an integer returns a string
Our Assumption Implemented
The Expectation Implemented
When we print out addTwoNumbers(2, 2) we will be returned 4
Our answer will now be returned as FOUR
- Just a little sidebar - I come from a strongly typed background and first learned to code by learning the granddaddy of Object Orientated Languages C++, so this for this example I'm explicitly declaring my data types to help all of us understand my point
BDD vs TDD
Behaviour Driven Development (BDD)
Test Driven Development (TDD)
- Customer friendly language, e.g. $(count).should_be(5)
- Expresses what the application needs to do for the stakeholder to find acceptable
- Ensures the right application is built
- Developer friendly language assertEquals(count, 5)
- Aimed for developers to help them check their software designs
- Ensures you build the application correctly
How to use BDD?
Can be used with:
Ruby
Java
Python
Javascript
Clojure
.NET
PHP
C++
- There are many different BDD frameworks available, but my framework of choice of Cucumber
- It came be used with all these languages and a few more
- With Cucumber you can write a user story, step definitions and a world for the steps to run in
Gherkin
- Cucumber's Domain Specific Language that allows you to describe the expected behaviour
- It's two purposes are documentation and automated tests
Gherkin Keywords
Feature
Background
Scenario
Given
When
Then
And
But
*
Scenario Outline
Examples
Simple Feature Template (Feature Injection)
In order to { meet some goal }
As a { type of stakeholder }
I want { a feature }
Scenario: Adding Two Numbers
Why use BDD?
- It's simple and can improve team communication
- The developer will know what to build
- The tester will know what to test
- The domain expert knows what bahaviour to expect