Behavior Driven Development (BDD) began as a means of helping developers practice Test Driven Development (TDD).
In this it was successful, but it quickly proved its value in many other ways. In this presentation, Larry Apke quotes heavily from the work of Uncle Bob Martin to make the case for TDD and then explains how developers can use BDD to take advantage of this excellent software development practice.
Larry also talks about his “Ten Reasons BDD Changes Everything” along with some easy ways to begin implementation of BDD in your software development organization immediately and what the corresponding future steps would be to take full advantage of this technique.
3. 3
Deming on Quality
“We cannot rely on mass inspection to improve
quality… As Harold S. Dodge said many years
ago, 'You cannot inspect quality into a product.'
The quality is there or it isn't by the time it's
inspected. “
4. 4
Red
GreenRefactor
TDD
• Write a unit test
• Validate that the test fails
• Write just the minimum code necessary
• Validate that the test passes successfully
• Refactor the code
BDD Begins with Test Driven Development (TDD)
5. 5
Why Do TDD?
• “Ilities” – Maintainability, Flexibility,
Extensibility
• High Percentage of Test Code
Coverage
• Streamlined Codebase
• Cleaner Interfaces
• Easier Refactoring of Code
• Tests Provide Some Code
Documentation
• Bottom Line - Higher Quality
6. 6
An Expert Speaks on TDD
“It is a myth that we can get systems “right the
first time.” Instead, we should implement only
today’s stories, then refactor and expand the
system to implement new stories tomorrow.
This is the essence of iterative and incremental
agility. Test-driven development, refactoring,
and the clean code they produce make this
work at the code level.”
7. 7
An Expert Speaks on TDD
“If the discipline of requirements specification
has taught us anything, it is that well-specified
requirements are as formal as code and can act
as executable tests of that code!”
8. 8
An Expert Speaks on TDD
“So if you want to go fast, if you want to get
done quickly, if you want your code to be easy
to write, make it easy to read.”
9. 9
An Expert Speaks on TDD
“Indeed, the ratio of time spent reading versus
writing is well over 10 to 1. We are constantly
reading old code as part of the effort to write
new code. ...[Therefore,] making it easy to read
makes it easier to write.”
10. 10
An Expert Speaks on Being a Professional Developer
“Perhaps you thought that “getting it working”
was the first order of business for a professional
developer…The functionality that you create
today has a good chance of changing in the next
release, but the readability of your code will
have a profound effect on all the changes that
will ever be made.”
11. 11
An Expert Speaks on Being a Professional Developer
“Programmers who satisfy themselves with
merely working code are behaving
unprofessionally. “
12. 12
An Expert Speaks on Being a Professional Developer
“Writing clean code is what you must do in
order to call yourself a professional. There is no
reasonable excuse for doing anything less than
your best.”
13. 13
Other Aspects of Being a Professional Developer
In addition to Test Driven Development and
writing testable, readable code, there are a
number of other things that go into making one
a professional programmer.
Your code must be secure
Your code must be performant
Your code must be deployable
Your code must integrate
In other words, code quality must be injected
into the code by the professional programmer.
Quality cannot be inspected into the code ex
post facto (after the fact).
14. 14
So, Why Doesn’t Everyone Do TDD?
• Most developers have not learned TDD and there is a learning curve
• TDD is extremely disciplined
• There is a perception that development is slower
• It is difficult to introduce with legacy code
• Developers often need to mock objects and learn mocking
“I kept coming across the same confusion and misunderstandings.
Programmers wanted to know where to start, what to test and what not
to test, how much to test in one go, what to call their tests, and how to
understand why a test fails.” - Dan North - Introduction to BDD
15. 15
Along Comes BDD (Behavior Driven Development)
“My response is behaviour-driven development
(BDD). It has evolved out of established agile
practices and is designed to make them more
accessible and effective for teams new to agile
software delivery. Over time, BDD has grown to
encompass the wider picture of agile analysis
and automated acceptance testing.”
Created a “ubiquitous language for the analysis
process itself!”
Over time they found that the conversations
between business and development where
richer.
16. 16
Why Do BDD?
TDD builds it right, BDD builds the right thing.
While there is no silver bullet to software
development, BDD (when coupled with
continuous integration) comes as close as
anything I have found.
My blog - “10 Reasons Why BDD Changes
Everything” was published on 3/6/2012 and
still is the most visited page on my website.
17. 17
Ten Reasons Why BDD Changes Everything
• Communication between business and development is extremely focused
as a result of common language.
• Business needs tie directly to the code that is written.
• Developers know what test cases they should write to accommodate TDD.
• All the underlying benefits of TDD become more easily realized.
• Code is easier to maintain.
• Code is self documenting.
• Stories are easier to “groom” – breakdown, task and plan.
• Teams can be more agile.
• Developers can be trained / on-boarded easier.
• There is more visibility into team progress and status.
18. 18
BDD – Real World Stories
Expressing Acceptance Criteria as
BDD scenarios breaks the bottle
neck in story writing for a small
financial services company in
Scottsdale, Arizona.
19. 19
BDD – Real World Stories
BDD provides support necessary
to successfully deliver MVP into
production in record time for
financial services company in San
Antonio, Texas.
20. 20
Adopting BDD – Three Simple Steps
Step One
Write our story acceptance criteria using BDD
scenarios
Step Two
Leverage these BDD scenarios to write our
automated tests first in true TDD fashion
Step Three
Leverage a BDD Tool like Cucumber to map
human-readable BDD scenarios to the
automated tests
21. 21
Step One – Write Acceptance Criteria in BDD Style
Step One is accomplished by the three amigos -
Product Owner, Developer(s) and Quality
Assurance Person(s).
Step One is accomplished during Story Refinement
which is where we go into detail for the stories that
we believe we will take during the next iteration.
This is the first step in transitioning to BDD and the
one that brings the most benefit. It was a pleasant
surprise to Dan North and crew, something that
they did not plan. Better communication equals
better quality.
22. 22
Step One – Write Acceptance Criteria in BDD Style
The Story
Customer Withdraws Cash
As a customer,
I want to withdraw cash from an ATM,
So that I don’t have to wait in line at the
bank.
23. 23
Step One – Write Acceptance Criteria in BDD Style
The Story
Customer Withdraws Cash
Scenario 1 - Account is In Credit
Given the account is in credit
And the card is valid
And the dispenser contains cash
When the customer requests cash
Then ensure the account is debited
And ensure cash is dispensed
And ensure the card is returned
24. 24
Step One – Write Acceptance Criteria in BDD Style
The Story
Customer Withdraws Cash
Scenario 2 - Account is overdrawn past the
overdraft limit
Given the account is overdrawn
And the card is valid
When the customer requests cash
Then ensure a rejection message is displayed
And ensure cash is not dispensed
And ensure the card is returned
25. 25
Step Two – Use BDD Scenarios to Write Tests
In step two we leverage BDD to write our tests
and begin the process of TDD. BDD scenarios
can be written directly into tests using
comments.
26. 26
Step Two – Use BDD Scenarios to Write Tests
public class AccountIsInCredit extends TestCase {
@Setup
//Scenario 1 - Account is In Credit
// Given the account is in credit
// And the card is valid
// And the dispenser contains cash
//Write code here to mock out account and add credit, mock out card and
set as valid, etc.
27. 27
Step Two – Use BDD Scenarios to Write Tests
//When the customer requests cash
Call CustomerRequestsCash (Amount) //call method on class and pass values
@Test
//Then ensure the account is debited
// And ensure cash is dispensed
// And ensure the card is returned
Assert.AssertEquals(ExpectedAccountBalance, Account.Balance);
Assert.AssertEquals(CashDispensed, True);
Assert.AssertEquals(CardReturned, True);
}
28. 28
Step Three – Use a BDD Tool
• BDD Tool will take human-readable
scenarios and use regular expressions to
map the human readable scenarios to Unit
Tests.
• BDD Tools will create a unit test and shell
automatically (referred to as a step
definition file) that helps in the creation of
test.
• When the BDD Tool runs the tests, the
output shows which tests passed and failed
against the human-readable scenarios.
• There are BDD Tools for all major languages.
I recommend Cucumber for Java and Ruby.
29. 29
Step Three – Use a BDD Tool
Scenario: Valid Log On
Given user navigates to logon page
When they enter valid user name and password
Then login should be successful
30. 30
Step Three – Use a BDD Tool
@Given("^user navigates to logonpage$")
pending # express the regexp above with the code you wish you had
end
@When("^they enter valid Username as "([^"]*)" and Password as
"([^"]*)"$")
pending # express the regexp above with the code you wish you had
end
@Then("^login should be successful$")
pending # express the regexp above with the code you wish you had
end
31. 31
Step Three – Use a BDD Tool
@Given("^user navigates to logonpage$")
public void goToLogonPage() {
driver = new FirefoxDriver();
driver.navigate().to(“logonpage.htm");
} end
@When("^they enter valid Username as "([^"]*)" and Password as "([^"]*)"$")
public void ValidUserNameAndPassword(String arg1, String arg2) {
driver.findElement(By.id("email")).sendKeys(arg1);
driver.findElement(By.id("pass")).sendKeys(arg2);
driver.findElement(By.id(“submit")).click();
}
end
32. 32
Step Three – Use a BDD Tool
#encoding: utf-8
Feature: Logging On
Scenario: Valid Log On # /logom:8
Given user navigates to logon page # /step_defs/logon.rb:3
When they enter valid user name and password # /step_defs/logon.rb:7
Then login should be successful # /step_defs/logon.rb:14
1 scenario (1 passed)
3 steps (3 passed)
0m0.005s
35. 35
Helpful Links
Dan North - Introduction to BDD
The Cucumber Book: Behaviour-Driven Development for Testers and
Developers
BDD in Action: Behavior-driven development for the whole software lifecycle
Specification by Example: How Successful Teams Deliver the Right Software
Cucumber Tool
Serenity Tool
Clean Code: A Handbook of Software Craftsmanship
38. 38
Your trusted outsourcing partner for
Agile software product development.
Synerzip
• Accelerate the delivery of your product roadmap
• Address technology skill gaps
• Save at least 50% with offshore software development
• Augment your team with optional on-site professionals
Tell the story about how I became associated with BDD because it is a good example of the efficacy of retrospectives. – BDD Questions – When do we do the BDD scenarios as acceptance criteria?
Most of what we refer to as QA in software development is usually inspection after the fact. In the case of Waterfall, far after the fact. Behavior Driven Development (BDD) provides us with the ability to inject quality into our product instead of merely inspecting after the fact. It moves testing to the beginning of the process instead of the end, creating a higher quality product that can be built quickly.
Robert “Uncle Bob” Martin is a software development professional since 1970, co-author of the original Agile Manifesto, author of numerous books on code quality including Clean Code: A handbook of Agile Software Craftsmanship.
BDD and TDD provide us with the executable tests of the code we write.
Being able to read code is more important than writing it. Being able to write readable code is critical to success and speed.
From POC to customers hands in less than 90 days.
When it was over, they went on “barnstorming” sessions to teach BDD throughout the organization, touting their success. One quote I remember from their training was that there were times they were inclined to write code first without using BDD and TDD because they thought it would be faster. It was this code that had the most problems and took longer to bring to the quality they wanted.
Notice that the scenario is basically a test case. It has prerequisite Given – where am I and what am I doing, it has a change to the system – When and then it has the systems reaction to the behavior change – Then.
The Givens will be mostly the realm of QA because it takes a QA mindset. The Thens are the realm of the Product Owner – as a business how do you want the system to react when this change occurs.
This particular technique was suggested from the San Antonio financial services team I worked with.
There is also SpecFlow for .Net, Lettuce for Python, Spock for Groovy, Jbehave for Java.