SlideShare a Scribd company logo
1 of 44
Download to read offline
Double Loop
TDD & BDD Done Right!
@jessicamauerhan | #ATO2016
@jessicamauerhan
Senior Software Engineer
TDD/BDD Expert
jessicamauerhan@gmail.com
jmauerhan.wordpress.com
@jessicamauerhan | #ATO2016
What is
Test Driven
Development?
Test Driven Development is a process in
which you first write a failing unit test,
execute that test, write just enough code to
make the test pass, then refactor that code
for improvements.
@jessicamauerhan | #ATO2016
Benefits of Test Driven Development
● Guides you towards creating a
well-abstracted and highly
modular system.
● Makes the software easier to
change, maintain and
understand.
● Promotes using design patterns,
refactoring, and helps prevent
scope creep.
● Complete test coverage
● Preventing and identifying
regressions and mistakes with
automated tests
● Executable documentation.
Primary Secondary
@jessicamauerhan | #ATO2016
What is
Behavior
Driven
Development?
Behaviour-driven development is an “outside-in”
methodology. It starts at the outside by identifying
business outcomes, and then drills down into the
feature set that will achieve those outcomes. Each
feature is captured as a “story”, which defines the
scope of the feature along with its acceptance
criteria.
Dan North, “What’s in a Story”
http://dannorth.net/whats-in-a-story
It’s the idea that you start by writing
human-readable sentences that describe a
feature of your application and how it should
work, and only then implement this behavior in
software.
Behat Documentation
http://docs.behat.org/en/v2.5
@jessicamauerhan | #ATO2016
Why Practice BDD?
● Well Designed Code
● Automated Testing
● Implementation
● UI Testing
BDD Is Not About...
● Communication
● Collaboration
● Documentation
● Preventing Regressions
BDD Is About...
@jessicamauerhan | #ATO2016
Why Practice
Behavior Driven
Development?
“You can turn an idea for a
requirement into implemented,
tested, production-ready code
simply and effectively, as long as the
requirement is specific enough that
everyone knows what’s going on.”
Dan North, “What’s in a Story”
http://dannorth.net/whats-in-a-story
@jessicamauerhan | #ATO2016
Accuracy
Unit Tests describe accuracy
of code and prescribe
maintainable code
Suitability
Behavior Tests describe
suitability of software's
features for the end user
@jessicamauerhan | #ATO2016
Testing Pyramid
Maintenance
Coverage
Fragility
Duration
Cost
Number of Tests
Unit Tests
Behavior Tests
System Tests
(performance)
GUI
Tests
Unit
Behavior
System
GUI
Manual QA
Testing Ice Cream Cone
@jessicamauerhan | #ATO2016
The Vicious Cycle of the Testing Ice Cream Cone
Unit
Behavior
System
GUI
Manual QA
Non Testable
Code
More Expensive
Tests
Developers Don't
Run Tests
More Bugs
Developers Don't
Write Unit Tests
Add
Behavior
Test
Add
Unit
Test
Run Unit
Test
Add/Edit
Some Code
Refactor
Run All
Unit Tests
Run All
Behavior
Tests
Pass* Fail
Fail
Pass
Run
Behavior
Test Pass*
Fail
Fail Pass
Revert Changes
@jessicamauerhan | #ATO2016
Double Loop: TDD & BDD Done Right!
Feature
Done?
NoYes
* A test that passes the first time
before code has been written means
the test is inaccurate or poorly
written, not added to the test suite,
or the code/feature already exists.
Add
Behavior
Test
@jessicamauerhan | #ATO2016
Parts of
a Behavior Test
● Feature: Gherkin
● Test Code
● BDD Framework /
Test Runner
@jessicamauerhan | #ATO2016
Gherkin
● Business Readable, Domain
Specific Language
● Living Documentation / User
Manual
● User Story with Narrative
● Contains at least one
Scenario: Acceptance Criteria
@jessicamauerhan | #ATO2016
● Narrative
○ As a [role]
I want [feature]
So that [benefit / business reason]
○ Use “5 Whys” to determine narrative
As an Account Holder
I want to withdraw cash from an ATM
So that I can get money when the bank is closed
Scenario: Account has sufficient funds
Given the account has a balance
And the card is valid
And the machine contains at least the amount of my balance
When I request an amount less than or equal to my balance
Then the ATM should dispense that amount
And the amount should be subtracted from my account balance
And the card should be returned
Scenario: Account has insufficient funds
Given the account has a balance
And the card is valid
And the machine contains at least the amount of my balance
When I request an amount greater than my balance
Then the ATM should not dispense any money
And the ATM should say there are insufficient funds
And my account balance should not change
And the card should be returned
● Feature: Short, Descriptive, Action
● Acceptance Criteria: Scenarios
○ Given: Exact Context
○ When: Action/Event
○ Then: Outcomes
○ And/But: More of the same...
●
Feature: Account Holder Withdraws Cash
@jessicamauerhan | #ATO2016
As a user viewing a broadcast page before the broadcast starts
I want to see a countdown timer
So that I can know how long until the broadcast actually starts
Scenario: View Countdown before Broadcast
Given I view the “Future Broadcast” broadcast
Then I should see "Future Broadcast" on the page
And I should see "Future Broadcast Author" on the page
And I should see "This broadcast begins at 6:00 pm EST" on the
page
And I should see a countdown timer
Feature: View Countdown before Broadcast
Process
● Author describes Feature with
implementation specific
example
● Developer adds fixture data
Issues
● Test only works before 6pm
● What is the intent?
● Confusion for business users
@jessicamauerhan | #ATO2016
As a user viewing a broadcast page before the broadcast starts
I want to see a countdown timer
So that I can know how long until the broadcast actually starts
Scenario: View Countdown before Broadcast
Given there is a broadcast scheduled for the future
When I view that broadcast’s page
Then I should see the broadcast title
And I should see the broadcast author’s name
And I should see "This broadcast begins at" followed by the start
time in EST
And I should see a countdown timer
Feature: View Countdown before Broadcast Changes
● Given now explains exact
context
● Test is no longer
time-dependent
● Intent - not implementation
Why?
● Overall understanding
● Communication value
● Help identify poorly written
code
@jessicamauerhan | #ATO2016
Feature: Customer Views Product and Services Catalog
As a customer
I want to view the product catalog
So that I can browse products and services
Scenario: Customer views Product Catalog
Given I view the catalog
When I select a state from the list of states
Then I should see the list of products for sale
Scenario: Display Local Services in Product Catalog
Given the company has a regional office in a state
When I view the catalog
And I select that state from the list of states
Then I should see the list of services offered
echo '<h1>Products</h1>';
foreach ($products AS $product) {
echo '<p>' . $product->getName() . '</p>';
}
echo '<h1>Services</h1>';
foreach ($services AS $service) {
echo '<p>' . $service->getName() . '</p>';
}
Scenario: Don’t Display Local Services in Product Catalog For States
With No Regional Office
Given the company does not have a regional office in a
state
When I view the catalog
And I select that state from the list of states
Then I should not see a list of services offered
@jessicamauerhan | #ATO2016
Feature: Customer Views Product and Services Catalog
As a customer
I want to view the product catalog
So that I can browse products and services
Scenario: Customer views Product Catalog
Given I view the catalog
When I select a state from the list of states
Then I should see the list of products for sale
Scenario: Display Local Services in Product Catalog
Given the company has a regional office in a state
When I view the catalog
And I select that state from the list of states
Then I should see the list of services offered
Scenario: Don’t Display Local Services in Product Catalog For States
With No Regional Office
Given the company does not have a regional office in a
state
When I view the catalog
And I select that state from the list of states
Then I should not see a list of services offered
echo '<h1>Products</h1>';
foreach ($products AS $product) {
echo '<p>' . $product->getName() . '</p>';
}
if(count($services) > 0) {
echo '<h1>Services</h1>';
foreach ($services AS $service) {
echo '<p>' . $service->getName() . '</p>';
}
}
@jessicamauerhan | #ATO2016
Writing Great Features
● Exact Context
● Independent Scenarios &
Features
● Intention, not Implementation
● Defined Narrative
● All Paths Explored
Feature “Smells”
● Time Dependency
● Interdependency
● Multi-Scenario Scenarios
● Missing Scenarios
● Overuse of Variables
● Examples of Behavior
(Implementation, not Intention)
@jessicamauerhan | #ATO2016
Parts of
a Behavior Test
● Feature: Gherkin
● Test Code
● BDD Framework /
Test Runner
@jessicamauerhan | #ATO2016
BDD Frameworks / Test Runners
● Java - JBehave, Cucumber and more
● Ruby - Cucumber
● PHP - Behat
● JavaScript - cucumber.js, jasmine
● ...
@jessicamauerhan | #ATO2016
Scenario: Customer views
Product Catalog
Given I view the catalog
When I select a state from the list
of states
Then I should see the list of
products for sale
Behat Code/**
* @Given I view the catalog
*/
public function iViewTheCatalog()
{
$this->visit('catalog');
}
/**
* @When I select a state from the list of states
*/
public function iSelectAStateFromTheListOfStates()
{
$this->selectOption('states', rand(1, 52));
}
/**
* @Then I should see the list of products for sale
*/
public function iShouldSeeTheListOfProductsForSale()
{
$productsDiv = $this->getSession()
->getPage()
->find('css', '#products');
Assertion::notNull($productsDiv);
}
Add
Behavior
Test
Run
Behavior
Test Pass*
@jessicamauerhan | #ATO2016
Double Loop: TDD & BDD Done Right!
* A test that passes the first time
before code has been written means
the test is inaccurate or poorly
written, not added to the test suite,
or the code/feature already exists.
@jessicamauerhan | #ATO2016
Parts of
a Behavior Test
● Feature: Gerkhin
● Test Code
● BDD Framework /
Test Runner
Add
Behavior
Test
Add
Unit
Test
Run
Behavior
Test Pass*
Fail
Double Loop: TDD & BDD Done Right!
* A test that passes the first time
before code has been written means
the test is inaccurate or poorly
written, not added to the test suite,
or the code/feature already exists.
@jessicamauerhan | #ATO2016
@jessicamauerhan | #ATO2016
Writing Unit Tests
What is a unit?
● Unit: As small as possible
● Design and Describe behavior
○ Setup scenario for behavior
○ Cause behavior to occur
○ Assert result is as expected
● Verbose and Descriptive
Names
Add
Behavior
Test
Add
Unit
Test
Run Unit
Test
Run
Behavior
Test Pass*
Fail
Double Loop: TDD & BDD Done Right!
* A test that passes the first time
before code has been written means
the test is inaccurate or poorly
written, not added to the test suite,
or the code/feature already exists.
@jessicamauerhan | #ATO2016
@jessicamauerhan | #ATO2016
Executing Unit
Tests
● Ensures test fails
when code is broken
● Ensures test is added
to suite
● Tells you exactly what
to do next
Always execute before writing
production code
Add
Behavior
Test
Add
Unit
Test
Run Unit
Test
Add/Edit
Some Code
Fail
Run
Behavior
Test Pass*
Fail
Double Loop: TDD & BDD Done Right!
* A test that passes the first time
before code has been written means
the test is inaccurate or poorly
written, not added to the test suite,
or the code/feature already exists.
@jessicamauerhan | #ATO2016
@jessicamauerhan | #ATO2016
Writing Code
Just Enough
● Add just enough code to pass
the test
● Avoid over abstracting or
adding untested logic
@jessicamauerhan | #ATO2016
Three Types of Unit
Tests
● Correctness
● Contract
● Collaboration
New Class
For User
Registration
Class:
Registration
Service
Unit Tests
Registration Service
Correctness
Third Party
Service API
(Mailer)
Integrated
Test
MailChimp API
Forgot
Password
Weekly
Newsletter
Direct
Message
Exciting
Event
Reminder!
@jessicamauerhan | #ATO2016
TDD: Integration Testing with Contract & Collaboration Tests
New Class
For User
Registration
Class:
Registration
Service
Third Party
Service API
(Mailer)
Collaborator:
Mailer
Mailer
Contract
(Interface /
Abstract)
Mailer
Interface
Test Double
Persistence
Layer
Persistence
Layer
(Interface /
Abstract)
Persistence
Layer
Test Double
Fully Unit Tested &
Implemented Class:
Registration
Service
Unit Tests
Registration Service
Correctness
Collaboration
Tests
Collaboration
Tests
@jessicamauerhan | #ATO2016
TDD: Integration Testing with Contract & Collaboration Tests
Third Party
Service API
(Mailer)
Mailer
Implementation
Class:
Mailchimp
Adapter
New Class
For User
Registration
Class:
Registration
Service
Collaborator:
Mailer
Mailer
Contract
(Interface /
Abstract)
Mailer
Interface
Test Double
Persistence
Layer
Persistence
Layer
(Interface /
Abstract)
Persistence
Layer
Test Double
Fully Unit Tested &
Implemented Class:
Registration
Service
Unit Tests
Registration Service
Correctness
Collaboration
Tests
Collaboration
Tests
Third Party Tool:
MailChimp API
(This is a Contract)
Mailchimp
API Double
Fully Unit Tested &
Implemented Class:
Mailchimp
Adapter
Correctness
Unit Tests
Contract
Tests
Collaboration
Tests
@jessicamauerhan | #ATO2016
TDD: Integration Testing with Contract & Collaboration Tests
Mailer
Implementation
Class:
SendBlue
Adapter
New Class
For User
Registration
Class:
Registration
Service
Third Party
Service API
(Mailer)
Collaborator:
Mailer
Mailer
Contract
(Interface /
Abstract)
Mailer
Interface
Test Double
Persistence
Layer
Persistence
Layer
(Interface /
Abstract)
Persistence
Layer
Test Double
Fully Unit Tested &
Implemented Class:
Registration
Service
Unit Tests
Registration Service
Correctness
Collaboration
Tests
Collaboration
Tests
Third Party Tool:
SendBlue
(This is a Contract)
Third Party Tool:
SendBlue
(This is a Contract)
SendBlue
API Double
Fully Unit Tested &
Implemented Class:
SendBlue
Adapter
Class:
SendBlue
Adapter
Correctness
Unit Tests
Contract
Tests
Collaboration
Tests
@jessicamauerhan | #ATO2016
TDD: Integration Testing with Contract & Collaboration Tests
Forgot
Password
Weekly
Newsletter
Direct
Message
Exciting
Event
Reminder!
Maintenance
Coverage
Fragility
Duration
Cost
Number of Tests
Isolated Unit Tests
Correctness, Contract & Collaboration
Integration Testing
Multiple Components Integrated
Tested for Correctness & Communication
Behavior Tests
Fully Integrated User Experience
System Tests
load balance, performance, security
GUI
Tests
Add
Behavior
Test
Add
Unit
Test
Run Unit
Test
Add/Edit
Some Code
Fail
Run
Behavior
Test Pass*
Fail
@jessicamauerhan | #ATO2016
Double Loop: TDD & BDD Done Right!
* A test that passes the first time
before code has been written means
the test is inaccurate or poorly
written, not added to the test suite,
or the code/feature already exists.
@jessicamauerhan | #ATO2016
Failing Test or
Test Suite?
Revert!
● If the test fails, revert your
work
● Commit small changes, often!
Add
Behavior
Test
Add
Unit
Test
Run Unit
Test
Add/Edit
Some Code
Refactor
Run All
Unit Tests
Pass* Fail
Fail
Pass
Run
Behavior
Test Pass*
Fail
Revert Changes
@jessicamauerhan | #ATO2016
Double Loop: TDD & BDD Done Right!
Feature
Done?
NoYes
* A test that passes the first time
before code has been written means
the test is inaccurate or poorly
written, not added to the test suite,
or the code/feature already exists.
@jessicamauerhan | #ATO2016
Refactoring
The process of
restructuring code
without changing its
behavior
@jessicamauerhan | #ATO2016
Refactoring
● Improving
non-functional aspects
● Removing duplication
● Renaming
● Simplifying
Unit Tests Always Passing!
● Changes that would
break a unit test
● New functionality that
is not tested
New Unit Tests Fail, Then Pass
Adding Functionality
Add
Behavior
Test
Add
Unit
Test
Run Unit
Test
Add/Edit
Some Code
Refactor
Run All
Unit Tests
Run All
Behavior
Tests
Pass* Fail
Fail
Pass
Run
Behavior
Test Pass*
Fail
Fail Pass
Revert Changes
Double Loop: TDD & BDD Done Right!
Feature
Done?
NoYes
* A test that passes the first time
before code has been written means
the test is inaccurate or poorly
written, not added to the test suite,
or the code/feature already exists.
@jessicamauerhan | #ATO2016
@jessicamauerhan | #ATO2016
Thank You!
Double Loop:
TDD & BDD Done Right!
Feedback & Questions?
Welcome & Encouraged!
@jessicamauerhan
jessicamauerhan@gmail.com
jmauerhan.wordpress.com

More Related Content

What's hot

What's hot (20)

Test Automation
Test AutomationTest Automation
Test Automation
 
How to Make Test Automation for Cloud-based System
How to Make Test Automation for Cloud-based SystemHow to Make Test Automation for Cloud-based System
How to Make Test Automation for Cloud-based System
 
Clean architecture
Clean architectureClean architecture
Clean architecture
 
Mock Server Using WireMock
Mock Server Using WireMockMock Server Using WireMock
Mock Server Using WireMock
 
Reasons To Automate API Testing Process
Reasons To Automate API Testing ProcessReasons To Automate API Testing Process
Reasons To Automate API Testing Process
 
TestNG Session presented in PB
TestNG Session presented in PBTestNG Session presented in PB
TestNG Session presented in PB
 
Web application testing with Selenium
Web application testing with SeleniumWeb application testing with Selenium
Web application testing with Selenium
 
From Zero to Docker
From Zero to DockerFrom Zero to Docker
From Zero to Docker
 
Rest API Automation with REST Assured
Rest API Automation with REST AssuredRest API Automation with REST Assured
Rest API Automation with REST Assured
 
Robot framework and selenium2 library
Robot framework and selenium2 libraryRobot framework and selenium2 library
Robot framework and selenium2 library
 
Postman.ppt
Postman.pptPostman.ppt
Postman.ppt
 
.Net Core
.Net Core.Net Core
.Net Core
 
testng
testngtestng
testng
 
Postman: An Introduction for Testers
Postman: An Introduction for TestersPostman: An Introduction for Testers
Postman: An Introduction for Testers
 
JUnit 5
JUnit 5JUnit 5
JUnit 5
 
Ship quality mobile apps with speed [Webinar]
Ship quality mobile apps with speed [Webinar]Ship quality mobile apps with speed [Webinar]
Ship quality mobile apps with speed [Webinar]
 
Unit Testing like a Pro - The Circle of Purity
Unit Testing like a Pro - The Circle of PurityUnit Testing like a Pro - The Circle of Purity
Unit Testing like a Pro - The Circle of Purity
 
Test Automation Frameworks: Assumptions, Concepts & Tools
Test Automation Frameworks: Assumptions, Concepts & ToolsTest Automation Frameworks: Assumptions, Concepts & Tools
Test Automation Frameworks: Assumptions, Concepts & Tools
 
Automated Web Testing Using Selenium
Automated Web Testing Using SeleniumAutomated Web Testing Using Selenium
Automated Web Testing Using Selenium
 
JUNit Presentation
JUNit PresentationJUNit Presentation
JUNit Presentation
 

Viewers also liked

Mocking in Java with Mockito
Mocking in Java with MockitoMocking in Java with Mockito
Mocking in Java with Mockito
Richard Paul
 
Behavior Driven Development (BDD) and Agile Testing
Behavior Driven Development (BDD) and Agile TestingBehavior Driven Development (BDD) and Agile Testing
Behavior Driven Development (BDD) and Agile Testing
dversaci
 

Viewers also liked (18)

BDD, Behat & Drupal
BDD, Behat & DrupalBDD, Behat & Drupal
BDD, Behat & Drupal
 
Behat - Beyond the Basics (2016 - SunshinePHP)
Behat - Beyond the Basics (2016 - SunshinePHP)Behat - Beyond the Basics (2016 - SunshinePHP)
Behat - Beyond the Basics (2016 - SunshinePHP)
 
xUnit
xUnitxUnit
xUnit
 
Double Loop
Double LoopDouble Loop
Double Loop
 
It's Testing, Jim, but not as we know it - BDD for Testers
It's Testing, Jim, but not as we know it - BDD for TestersIt's Testing, Jim, but not as we know it - BDD for Testers
It's Testing, Jim, but not as we know it - BDD for Testers
 
xUnit and TDD: Why and How in Enterprise Software, August 2012
xUnit and TDD: Why and How in Enterprise Software, August 2012xUnit and TDD: Why and How in Enterprise Software, August 2012
xUnit and TDD: Why and How in Enterprise Software, August 2012
 
Test Driven Development for Embedded C
Test Driven Development for Embedded CTest Driven Development for Embedded C
Test Driven Development for Embedded C
 
TDD, BDD, RSpec
TDD, BDD, RSpecTDD, BDD, RSpec
TDD, BDD, RSpec
 
TDD with BDD in PHP and Symfony
TDD with BDD in PHP and SymfonyTDD with BDD in PHP and Symfony
TDD with BDD in PHP and Symfony
 
Principles and patterns for test driven development
Principles and patterns for test driven developmentPrinciples and patterns for test driven development
Principles and patterns for test driven development
 
TDD, BDD and mocks
TDD, BDD and mocksTDD, BDD and mocks
TDD, BDD and mocks
 
Mocking in Java with Mockito
Mocking in Java with MockitoMocking in Java with Mockito
Mocking in Java with Mockito
 
Behavior Driven Development (BDD) and Agile Testing
Behavior Driven Development (BDD) and Agile TestingBehavior Driven Development (BDD) and Agile Testing
Behavior Driven Development (BDD) and Agile Testing
 
#jjug_ccc #ccc_gh5 What's new in Spring Framework 4.3 / Boot 1.4 + Pivotal's ...
#jjug_ccc #ccc_gh5 What's new in Spring Framework 4.3 / Boot 1.4 + Pivotal's ...#jjug_ccc #ccc_gh5 What's new in Spring Framework 4.3 / Boot 1.4 + Pivotal's ...
#jjug_ccc #ccc_gh5 What's new in Spring Framework 4.3 / Boot 1.4 + Pivotal's ...
 
Test driven development in meteor
Test driven development in meteorTest driven development in meteor
Test driven development in meteor
 
BDD presentation
BDD presentationBDD presentation
BDD presentation
 
Test and Behaviour Driven Development (TDD/BDD)
Test and Behaviour Driven Development (TDD/BDD)Test and Behaviour Driven Development (TDD/BDD)
Test and Behaviour Driven Development (TDD/BDD)
 
Test Driven Development (TDD)
Test Driven Development (TDD)Test Driven Development (TDD)
Test Driven Development (TDD)
 

Similar to Double Loop: TDD & BDD Done Right!

Google analytics and website optimizer
Google analytics and website optimizerGoogle analytics and website optimizer
Google analytics and website optimizer
Digiword Ha Noi
 

Similar to Double Loop: TDD & BDD Done Right! (20)

Double Loop: TDD & BDD Done Right
Double Loop: TDD & BDD Done RightDouble Loop: TDD & BDD Done Right
Double Loop: TDD & BDD Done Right
 
BDD Selenium for Agile Teams - User Stories
BDD Selenium for Agile Teams - User StoriesBDD Selenium for Agile Teams - User Stories
BDD Selenium for Agile Teams - User Stories
 
Talkin bout Flow - Meighan Brodkey WIT Devs
Talkin bout Flow - Meighan Brodkey WIT Devs Talkin bout Flow - Meighan Brodkey WIT Devs
Talkin bout Flow - Meighan Brodkey WIT Devs
 
Testable requirements
Testable requirementsTestable requirements
Testable requirements
 
Using Error Budgets to Prioritize Work
Using Error Budgets to Prioritize WorkUsing Error Budgets to Prioritize Work
Using Error Budgets to Prioritize Work
 
Testable Requirements
Testable Requirements Testable Requirements
Testable Requirements
 
Google Analytics Website Optimizer Slideshare
Google Analytics Website Optimizer SlideshareGoogle Analytics Website Optimizer Slideshare
Google Analytics Website Optimizer Slideshare
 
Google Analytics And Website Optimizer
Google Analytics And Website OptimizerGoogle Analytics And Website Optimizer
Google Analytics And Website Optimizer
 
Google analytics and website optimizer
Google analytics and website optimizerGoogle analytics and website optimizer
Google analytics and website optimizer
 
Google Analytics and Website Optimizer
Google Analytics and Website OptimizerGoogle Analytics and Website Optimizer
Google Analytics and Website Optimizer
 
The complete guide to BDD + Cucumber Best Practices and Anti-Patterns.
The complete guide to BDD + Cucumber Best Practices and Anti-Patterns.The complete guide to BDD + Cucumber Best Practices and Anti-Patterns.
The complete guide to BDD + Cucumber Best Practices and Anti-Patterns.
 
Build the Right Regression Suite with Behavior-Driven Testing
Build the Right Regression Suite with Behavior-Driven TestingBuild the Right Regression Suite with Behavior-Driven Testing
Build the Right Regression Suite with Behavior-Driven Testing
 
Performance testing with JMeter
Performance testing with JMeterPerformance testing with JMeter
Performance testing with JMeter
 
Behaviour Driven Development (BDD) - Closing the Loop on a Great Fiori UX
Behaviour Driven Development (BDD) - Closing the Loop on a Great Fiori UXBehaviour Driven Development (BDD) - Closing the Loop on a Great Fiori UX
Behaviour Driven Development (BDD) - Closing the Loop on a Great Fiori UX
 
JBUG 11 - Django-The Web Framework For Perfectionists With Deadlines
JBUG 11 - Django-The Web Framework For Perfectionists With DeadlinesJBUG 11 - Django-The Web Framework For Perfectionists With Deadlines
JBUG 11 - Django-The Web Framework For Perfectionists With Deadlines
 
Implement BDD with Cucumber and SpecFlow
Implement BDD with Cucumber and SpecFlowImplement BDD with Cucumber and SpecFlow
Implement BDD with Cucumber and SpecFlow
 
Frappe ERPNext Open Day August 2014
Frappe ERPNext Open Day August 2014Frappe ERPNext Open Day August 2014
Frappe ERPNext Open Day August 2014
 
Getting Started with Server-Side Testing
Getting Started with Server-Side TestingGetting Started with Server-Side Testing
Getting Started with Server-Side Testing
 
Scrum Process Overview
Scrum Process OverviewScrum Process Overview
Scrum Process Overview
 
Django Shop
Django ShopDjango Shop
Django Shop
 

Recently uploaded

Recently uploaded (20)

WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
WSO2CON 2024 - OSU & WSO2: A Decade Journey in Integration & Innovation
WSO2CON 2024 - OSU & WSO2: A Decade Journey in Integration & InnovationWSO2CON 2024 - OSU & WSO2: A Decade Journey in Integration & Innovation
WSO2CON 2024 - OSU & WSO2: A Decade Journey in Integration & Innovation
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public AdministrationWSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
 
WSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
WSO2CON2024 - Why Should You Consider Ballerina for Your Next IntegrationWSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
WSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
WSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - KanchanaWSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - Kanchana
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
Evolving Data Governance for the Real-time Streaming and AI Era
Evolving Data Governance for the Real-time Streaming and AI EraEvolving Data Governance for the Real-time Streaming and AI Era
Evolving Data Governance for the Real-time Streaming and AI Era
 
WSO2Con2024 - Low-Code Integration Tooling
WSO2Con2024 - Low-Code Integration ToolingWSO2Con2024 - Low-Code Integration Tooling
WSO2Con2024 - Low-Code Integration Tooling
 
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
 
WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!
WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!
WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!
 
Driving Innovation: Scania's API Revolution with WSO2
Driving Innovation: Scania's API Revolution with WSO2Driving Innovation: Scania's API Revolution with WSO2
Driving Innovation: Scania's API Revolution with WSO2
 
WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of Transformation
WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of TransformationWSO2CON 2024 - Designing Event-Driven Enterprises: Stories of Transformation
WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of Transformation
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 
WSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AIWSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AI
 

Double Loop: TDD & BDD Done Right!

  • 1. Double Loop TDD & BDD Done Right! @jessicamauerhan | #ATO2016
  • 2. @jessicamauerhan Senior Software Engineer TDD/BDD Expert jessicamauerhan@gmail.com jmauerhan.wordpress.com
  • 3. @jessicamauerhan | #ATO2016 What is Test Driven Development? Test Driven Development is a process in which you first write a failing unit test, execute that test, write just enough code to make the test pass, then refactor that code for improvements.
  • 4. @jessicamauerhan | #ATO2016 Benefits of Test Driven Development ● Guides you towards creating a well-abstracted and highly modular system. ● Makes the software easier to change, maintain and understand. ● Promotes using design patterns, refactoring, and helps prevent scope creep. ● Complete test coverage ● Preventing and identifying regressions and mistakes with automated tests ● Executable documentation. Primary Secondary
  • 5. @jessicamauerhan | #ATO2016 What is Behavior Driven Development? Behaviour-driven development is an “outside-in” methodology. It starts at the outside by identifying business outcomes, and then drills down into the feature set that will achieve those outcomes. Each feature is captured as a “story”, which defines the scope of the feature along with its acceptance criteria. Dan North, “What’s in a Story” http://dannorth.net/whats-in-a-story It’s the idea that you start by writing human-readable sentences that describe a feature of your application and how it should work, and only then implement this behavior in software. Behat Documentation http://docs.behat.org/en/v2.5
  • 6. @jessicamauerhan | #ATO2016 Why Practice BDD? ● Well Designed Code ● Automated Testing ● Implementation ● UI Testing BDD Is Not About... ● Communication ● Collaboration ● Documentation ● Preventing Regressions BDD Is About...
  • 7. @jessicamauerhan | #ATO2016 Why Practice Behavior Driven Development? “You can turn an idea for a requirement into implemented, tested, production-ready code simply and effectively, as long as the requirement is specific enough that everyone knows what’s going on.” Dan North, “What’s in a Story” http://dannorth.net/whats-in-a-story
  • 8. @jessicamauerhan | #ATO2016 Accuracy Unit Tests describe accuracy of code and prescribe maintainable code Suitability Behavior Tests describe suitability of software's features for the end user
  • 9. @jessicamauerhan | #ATO2016 Testing Pyramid Maintenance Coverage Fragility Duration Cost Number of Tests Unit Tests Behavior Tests System Tests (performance) GUI Tests Unit Behavior System GUI Manual QA Testing Ice Cream Cone
  • 10. @jessicamauerhan | #ATO2016 The Vicious Cycle of the Testing Ice Cream Cone Unit Behavior System GUI Manual QA Non Testable Code More Expensive Tests Developers Don't Run Tests More Bugs Developers Don't Write Unit Tests
  • 11. Add Behavior Test Add Unit Test Run Unit Test Add/Edit Some Code Refactor Run All Unit Tests Run All Behavior Tests Pass* Fail Fail Pass Run Behavior Test Pass* Fail Fail Pass Revert Changes @jessicamauerhan | #ATO2016 Double Loop: TDD & BDD Done Right! Feature Done? NoYes * A test that passes the first time before code has been written means the test is inaccurate or poorly written, not added to the test suite, or the code/feature already exists.
  • 13. @jessicamauerhan | #ATO2016 Parts of a Behavior Test ● Feature: Gherkin ● Test Code ● BDD Framework / Test Runner
  • 14. @jessicamauerhan | #ATO2016 Gherkin ● Business Readable, Domain Specific Language ● Living Documentation / User Manual ● User Story with Narrative ● Contains at least one Scenario: Acceptance Criteria
  • 15. @jessicamauerhan | #ATO2016 ● Narrative ○ As a [role] I want [feature] So that [benefit / business reason] ○ Use “5 Whys” to determine narrative As an Account Holder I want to withdraw cash from an ATM So that I can get money when the bank is closed Scenario: Account has sufficient funds Given the account has a balance And the card is valid And the machine contains at least the amount of my balance When I request an amount less than or equal to my balance Then the ATM should dispense that amount And the amount should be subtracted from my account balance And the card should be returned Scenario: Account has insufficient funds Given the account has a balance And the card is valid And the machine contains at least the amount of my balance When I request an amount greater than my balance Then the ATM should not dispense any money And the ATM should say there are insufficient funds And my account balance should not change And the card should be returned ● Feature: Short, Descriptive, Action ● Acceptance Criteria: Scenarios ○ Given: Exact Context ○ When: Action/Event ○ Then: Outcomes ○ And/But: More of the same... ● Feature: Account Holder Withdraws Cash
  • 16. @jessicamauerhan | #ATO2016 As a user viewing a broadcast page before the broadcast starts I want to see a countdown timer So that I can know how long until the broadcast actually starts Scenario: View Countdown before Broadcast Given I view the “Future Broadcast” broadcast Then I should see "Future Broadcast" on the page And I should see "Future Broadcast Author" on the page And I should see "This broadcast begins at 6:00 pm EST" on the page And I should see a countdown timer Feature: View Countdown before Broadcast Process ● Author describes Feature with implementation specific example ● Developer adds fixture data Issues ● Test only works before 6pm ● What is the intent? ● Confusion for business users
  • 17. @jessicamauerhan | #ATO2016 As a user viewing a broadcast page before the broadcast starts I want to see a countdown timer So that I can know how long until the broadcast actually starts Scenario: View Countdown before Broadcast Given there is a broadcast scheduled for the future When I view that broadcast’s page Then I should see the broadcast title And I should see the broadcast author’s name And I should see "This broadcast begins at" followed by the start time in EST And I should see a countdown timer Feature: View Countdown before Broadcast Changes ● Given now explains exact context ● Test is no longer time-dependent ● Intent - not implementation Why? ● Overall understanding ● Communication value ● Help identify poorly written code
  • 18. @jessicamauerhan | #ATO2016 Feature: Customer Views Product and Services Catalog As a customer I want to view the product catalog So that I can browse products and services Scenario: Customer views Product Catalog Given I view the catalog When I select a state from the list of states Then I should see the list of products for sale Scenario: Display Local Services in Product Catalog Given the company has a regional office in a state When I view the catalog And I select that state from the list of states Then I should see the list of services offered echo '<h1>Products</h1>'; foreach ($products AS $product) { echo '<p>' . $product->getName() . '</p>'; } echo '<h1>Services</h1>'; foreach ($services AS $service) { echo '<p>' . $service->getName() . '</p>'; } Scenario: Don’t Display Local Services in Product Catalog For States With No Regional Office Given the company does not have a regional office in a state When I view the catalog And I select that state from the list of states Then I should not see a list of services offered
  • 19. @jessicamauerhan | #ATO2016 Feature: Customer Views Product and Services Catalog As a customer I want to view the product catalog So that I can browse products and services Scenario: Customer views Product Catalog Given I view the catalog When I select a state from the list of states Then I should see the list of products for sale Scenario: Display Local Services in Product Catalog Given the company has a regional office in a state When I view the catalog And I select that state from the list of states Then I should see the list of services offered Scenario: Don’t Display Local Services in Product Catalog For States With No Regional Office Given the company does not have a regional office in a state When I view the catalog And I select that state from the list of states Then I should not see a list of services offered echo '<h1>Products</h1>'; foreach ($products AS $product) { echo '<p>' . $product->getName() . '</p>'; } if(count($services) > 0) { echo '<h1>Services</h1>'; foreach ($services AS $service) { echo '<p>' . $service->getName() . '</p>'; } }
  • 20. @jessicamauerhan | #ATO2016 Writing Great Features ● Exact Context ● Independent Scenarios & Features ● Intention, not Implementation ● Defined Narrative ● All Paths Explored Feature “Smells” ● Time Dependency ● Interdependency ● Multi-Scenario Scenarios ● Missing Scenarios ● Overuse of Variables ● Examples of Behavior (Implementation, not Intention)
  • 21. @jessicamauerhan | #ATO2016 Parts of a Behavior Test ● Feature: Gherkin ● Test Code ● BDD Framework / Test Runner
  • 22. @jessicamauerhan | #ATO2016 BDD Frameworks / Test Runners ● Java - JBehave, Cucumber and more ● Ruby - Cucumber ● PHP - Behat ● JavaScript - cucumber.js, jasmine ● ...
  • 23. @jessicamauerhan | #ATO2016 Scenario: Customer views Product Catalog Given I view the catalog When I select a state from the list of states Then I should see the list of products for sale Behat Code/** * @Given I view the catalog */ public function iViewTheCatalog() { $this->visit('catalog'); } /** * @When I select a state from the list of states */ public function iSelectAStateFromTheListOfStates() { $this->selectOption('states', rand(1, 52)); } /** * @Then I should see the list of products for sale */ public function iShouldSeeTheListOfProductsForSale() { $productsDiv = $this->getSession() ->getPage() ->find('css', '#products'); Assertion::notNull($productsDiv); }
  • 24. Add Behavior Test Run Behavior Test Pass* @jessicamauerhan | #ATO2016 Double Loop: TDD & BDD Done Right! * A test that passes the first time before code has been written means the test is inaccurate or poorly written, not added to the test suite, or the code/feature already exists.
  • 25. @jessicamauerhan | #ATO2016 Parts of a Behavior Test ● Feature: Gerkhin ● Test Code ● BDD Framework / Test Runner
  • 26. Add Behavior Test Add Unit Test Run Behavior Test Pass* Fail Double Loop: TDD & BDD Done Right! * A test that passes the first time before code has been written means the test is inaccurate or poorly written, not added to the test suite, or the code/feature already exists. @jessicamauerhan | #ATO2016
  • 27. @jessicamauerhan | #ATO2016 Writing Unit Tests What is a unit? ● Unit: As small as possible ● Design and Describe behavior ○ Setup scenario for behavior ○ Cause behavior to occur ○ Assert result is as expected ● Verbose and Descriptive Names
  • 28. Add Behavior Test Add Unit Test Run Unit Test Run Behavior Test Pass* Fail Double Loop: TDD & BDD Done Right! * A test that passes the first time before code has been written means the test is inaccurate or poorly written, not added to the test suite, or the code/feature already exists. @jessicamauerhan | #ATO2016
  • 29. @jessicamauerhan | #ATO2016 Executing Unit Tests ● Ensures test fails when code is broken ● Ensures test is added to suite ● Tells you exactly what to do next Always execute before writing production code
  • 30. Add Behavior Test Add Unit Test Run Unit Test Add/Edit Some Code Fail Run Behavior Test Pass* Fail Double Loop: TDD & BDD Done Right! * A test that passes the first time before code has been written means the test is inaccurate or poorly written, not added to the test suite, or the code/feature already exists. @jessicamauerhan | #ATO2016
  • 31. @jessicamauerhan | #ATO2016 Writing Code Just Enough ● Add just enough code to pass the test ● Avoid over abstracting or adding untested logic
  • 32. @jessicamauerhan | #ATO2016 Three Types of Unit Tests ● Correctness ● Contract ● Collaboration
  • 33. New Class For User Registration Class: Registration Service Unit Tests Registration Service Correctness Third Party Service API (Mailer) Integrated Test MailChimp API Forgot Password Weekly Newsletter Direct Message Exciting Event Reminder! @jessicamauerhan | #ATO2016 TDD: Integration Testing with Contract & Collaboration Tests
  • 34. New Class For User Registration Class: Registration Service Third Party Service API (Mailer) Collaborator: Mailer Mailer Contract (Interface / Abstract) Mailer Interface Test Double Persistence Layer Persistence Layer (Interface / Abstract) Persistence Layer Test Double Fully Unit Tested & Implemented Class: Registration Service Unit Tests Registration Service Correctness Collaboration Tests Collaboration Tests @jessicamauerhan | #ATO2016 TDD: Integration Testing with Contract & Collaboration Tests
  • 35. Third Party Service API (Mailer) Mailer Implementation Class: Mailchimp Adapter New Class For User Registration Class: Registration Service Collaborator: Mailer Mailer Contract (Interface / Abstract) Mailer Interface Test Double Persistence Layer Persistence Layer (Interface / Abstract) Persistence Layer Test Double Fully Unit Tested & Implemented Class: Registration Service Unit Tests Registration Service Correctness Collaboration Tests Collaboration Tests Third Party Tool: MailChimp API (This is a Contract) Mailchimp API Double Fully Unit Tested & Implemented Class: Mailchimp Adapter Correctness Unit Tests Contract Tests Collaboration Tests @jessicamauerhan | #ATO2016 TDD: Integration Testing with Contract & Collaboration Tests
  • 36. Mailer Implementation Class: SendBlue Adapter New Class For User Registration Class: Registration Service Third Party Service API (Mailer) Collaborator: Mailer Mailer Contract (Interface / Abstract) Mailer Interface Test Double Persistence Layer Persistence Layer (Interface / Abstract) Persistence Layer Test Double Fully Unit Tested & Implemented Class: Registration Service Unit Tests Registration Service Correctness Collaboration Tests Collaboration Tests Third Party Tool: SendBlue (This is a Contract) Third Party Tool: SendBlue (This is a Contract) SendBlue API Double Fully Unit Tested & Implemented Class: SendBlue Adapter Class: SendBlue Adapter Correctness Unit Tests Contract Tests Collaboration Tests @jessicamauerhan | #ATO2016 TDD: Integration Testing with Contract & Collaboration Tests Forgot Password Weekly Newsletter Direct Message Exciting Event Reminder!
  • 37. Maintenance Coverage Fragility Duration Cost Number of Tests Isolated Unit Tests Correctness, Contract & Collaboration Integration Testing Multiple Components Integrated Tested for Correctness & Communication Behavior Tests Fully Integrated User Experience System Tests load balance, performance, security GUI Tests
  • 38. Add Behavior Test Add Unit Test Run Unit Test Add/Edit Some Code Fail Run Behavior Test Pass* Fail @jessicamauerhan | #ATO2016 Double Loop: TDD & BDD Done Right! * A test that passes the first time before code has been written means the test is inaccurate or poorly written, not added to the test suite, or the code/feature already exists.
  • 39. @jessicamauerhan | #ATO2016 Failing Test or Test Suite? Revert! ● If the test fails, revert your work ● Commit small changes, often!
  • 40. Add Behavior Test Add Unit Test Run Unit Test Add/Edit Some Code Refactor Run All Unit Tests Pass* Fail Fail Pass Run Behavior Test Pass* Fail Revert Changes @jessicamauerhan | #ATO2016 Double Loop: TDD & BDD Done Right! Feature Done? NoYes * A test that passes the first time before code has been written means the test is inaccurate or poorly written, not added to the test suite, or the code/feature already exists.
  • 41. @jessicamauerhan | #ATO2016 Refactoring The process of restructuring code without changing its behavior
  • 42. @jessicamauerhan | #ATO2016 Refactoring ● Improving non-functional aspects ● Removing duplication ● Renaming ● Simplifying Unit Tests Always Passing! ● Changes that would break a unit test ● New functionality that is not tested New Unit Tests Fail, Then Pass Adding Functionality
  • 43. Add Behavior Test Add Unit Test Run Unit Test Add/Edit Some Code Refactor Run All Unit Tests Run All Behavior Tests Pass* Fail Fail Pass Run Behavior Test Pass* Fail Fail Pass Revert Changes Double Loop: TDD & BDD Done Right! Feature Done? NoYes * A test that passes the first time before code has been written means the test is inaccurate or poorly written, not added to the test suite, or the code/feature already exists. @jessicamauerhan | #ATO2016
  • 44. @jessicamauerhan | #ATO2016 Thank You! Double Loop: TDD & BDD Done Right! Feedback & Questions? Welcome & Encouraged! @jessicamauerhan jessicamauerhan@gmail.com jmauerhan.wordpress.com