SlideShare a Scribd company logo
INTRODUCTION TO BDDINTRODUCTION TO BDD
BEHAVIOR DRIVEN DEVELOPMENTBEHAVIOR DRIVEN DEVELOPMENT
1
2 . 1
whoamiwhoami
Juan Ignacio Rodríguez de león
@jileon en Twitter
Backend developer en OctopusLabs
2 . 2
WE ARE HIRINGWE ARE HIRING
More info at:
https://octopuslabs.com/careers
2 . 3
HOW IS THIS TALK STRUCTURED?HOW IS THIS TALK STRUCTURED?
Presentation (Finished!)
Teory
Practice
Libraries, tools,
recomendations...
2 . 4
THEORYTHEORY
3 . 1
In theory, there is no difference
between theory and practice. In
practice, there is.
--
Benjamin Brewster
3 . 2
WHAT IS BDDWHAT IS BDD
As stated by wikipedia:
... an extension of TDD that makes use of a simple, domain-specific scripting language
structured like natural language statements to ensures all development projects remain
focused on delivering what the business actually needs while meeting all requirements.
3 . 3
manman
WATH IS BDDWATH IS BDD
As stated by wikipedia:
... an extension of TDD that makes use of a simple, domain-specific scripting language
structured like natural language statements to ensures all development projects remain
focused on delivering what the business actually needs while meeting all requirements.
3 . 4
TLDRTLDR
They are tests. As in TDD, we use test to drive the
design of the so ware
Usually, on a higher level than TDD
Written in a natural lenguage , like English
Example
3 . 5
TLDRTLDR
They are tests. As in TDD, we use test to drive the
design of the so ware
Usually, on a higher level than TDD
Written in a natural lenguage , like English
Example
3 . 5
TLDRTLDR
They are tests. As in TDD, we use test to drive the
design of the so ware
Usually, on a higher level than TDD
Written in a natural lenguage , like English
Example
3 . 5
TLDRTLDR
They are tests. As in TDD, we use test to drive the
design of the so ware
Usually, on a higher level than TDD
Written in a natural lenguage , like English
Example
3 . 5
TARGET: SHARE KNOWLEDGETARGET: SHARE KNOWLEDGE
A natural lenguaje:
Is used for all of us
Everyone can define, debate o improve a test
Allow all the team to discuss on tipically conflictive
topics: edge cases, exceptions...
3 . 6
TARGET: SHARE KNOWLEDGETARGET: SHARE KNOWLEDGE
A natural lenguaje:
Is used for all of us
Everyone can define, debate o improve a test
Allow all the team to discuss on tipically conflictive
topics: edge cases, exceptions...
3 . 6
TARGET: SHARE KNOWLEDGETARGET: SHARE KNOWLEDGE
A natural lenguaje:
Is used for all of us
Everyone can define, debate o improve a test
Allow all the team to discuss on tipically conflictive
topics: edge cases, exceptions...
3 . 6
TARGET: SHARE KNOWLEDGETARGET: SHARE KNOWLEDGE
A natural lenguaje:
Is used for all of us
Everyone can define, debate o improve a test
Allow all the team to discuss on tipically conflictive
topics: edge cases, exceptions...
3 . 6
EXECUTABLE SPECIFICATIONSEXECUTABLE SPECIFICATIONS
Using this test as specifications for our so ware, we
get specifications written on a plain english, but
executables.
3 . 7
EXECUTABLE SPECIFICATIONSEXECUTABLE SPECIFICATIONS
Using this test as specifications for our so ware, we
get specifications written on a plain english, but
executables.
3 . 7
HOW CAN YOU "EXECUTE" ENGLISH?HOW CAN YOU "EXECUTE" ENGLISH?
Transform a plain english specification in a test
Example
3 . 8
NOW WE HAVE TESTS!NOW WE HAVE TESTS!
But, obviously, we need to implement the code. No
black magic
Before that, lets review a few details about files and
organization.
3 . 9
GHERKINGHERKIN
The format used in this specification is called Gherkin
There are just a few words that have a special
meaning: Feature, Scenario, Given, When y
Then.
(Also And, But)
3 . 10
FEATUREFEATURE
Every feature is a capacity of our product. Each
feature must have its own .feature file
The keyword Feature must go at the begining, and
gives us the opportunity to descrive the feature in
one line
The rest of text between the feature line and the first
scenario can be used to describe the feature with
more detail
3 . 11
SCENARIOSCENARIO
Usually, a feature has more than one scenario,
because we are interested on testing diferent
aspects: changing conditiones, edge cases, different
inputs, etc...
Every diferent case is defined with the keyboard
Scenario
As a rule, every scenario is a complete and concrete
example of a feature
3 . 12
GIVENGIVEN
Given put our system in a known and controled
state, before the first intereaction.
(No user interactions if this part)
3 . 13
WHENWHEN
When This is where we describe the actions the user
or external system wants to do. This must yield
some changes in the system.
Or maybe not
3 . 14
THENTHEN
Then This is where we check that the outcomes of
our system are the expected
3 . 15
AND Y BUTAND Y BUT
And y But are alternative versions of Given and Then
just to get a better feeling of proper english.
So, instead of writing:
We can write:
Given I have 10000€ in my current acount
Given I have 300€ in my saving accout
Given I have 10000€ in my current acount
And I have 300€ in my saving accout
3 . 16
GO BACK TO PRACTICEGO BACK TO PRACTICE
LETS IMPLEMENT THE TESTLETS IMPLEMENT THE TEST
Example
3 . 17
A FEW INTERESTING DETAILSA FEW INTERESTING DETAILS
ON STEPSON STEPS
The name of the function used to implemente the
steps it's not important!
Behave decide what funciones to call, and when,
based only in the text of decorator
Main idea is having reusable steps
3 . 18
A FEW INTERESTING DETAILSA FEW INTERESTING DETAILS
ON STEPSON STEPS
The name of the function used to implemente the
steps it's not important!
Behave decide what funciones to call, and when,
based only in the text of decorator
Main idea is having reusable steps
3 . 18
A FEW INTERESTING DETAILSA FEW INTERESTING DETAILS
ON STEPSON STEPS
The name of the function used to implemente the
steps it's not important!
Behave decide what funciones to call, and when,
based only in the text of decorator
Main idea is having reusable steps
3 . 18
REQUESTREQUEST
Every step has a common first parameter called
request, that could be used for Sharing data
This data can be shared at a feaure level or a
scenario level
3 . 19
REQUESTREQUEST
Every step has a common first parameter called
request, that could be used for Sharing data
This data can be shared at a feaure level or a
scenario level
3 . 19
PARAMETER CAPTUREPARAMETER CAPTURE
One interesting ability is to "capture" some part of
the text of the step
This allow us to have parametrizable, and hence,
more reusable steps
Lets see this with another example
3 . 20
DIFFERENCES WITH TDDDIFFERENCES WITH TDD
Emphasis is not in the code, but in the functionality
Specification is written in natural language (i.e.
english)
Spoken by all the members of the team: Developers
testers, Q/A, product owners, skateholders, users...
3 . 21
DIFFERENCES WITH TDDDIFFERENCES WITH TDD
Emphasis is not in the code, but in the functionality
Specification is written in natural language (i.e.
english)
Spoken by all the members of the team: Developers
testers, Q/A, product owners, skateholders, users...
3 . 21
DIFFERENCES WITH TDDDIFFERENCES WITH TDD
Emphasis is not in the code, but in the functionality
Specification is written in natural language (i.e.
english)
Spoken by all the members of the team: Developers
testers, Q/A, product owners, skateholders, users...
3 . 21
DIFFERENCES WITH TDDDIFFERENCES WITH TDD
Emphasis is not in the code, but in the functionality
Specification is written in natural language (i.e.
english)
Spoken by all the members of the team: Developers
testers, Q/A, product owners, skateholders, users...
3 . 21
PRACTICEPRACTICE
4 . 1
A FEATURE FILE EXAMPLEA FEATURE FILE EXAMPLE
4 . 2
Feature: Transfer between accounts
Scenario: Add savings
Given I have 10.000€ in my current acount
And I have 300€ in my saving accout
When I transfer 200€ from current to saving
Then I should have 9.800€ in my current acount
And I should have 500€ in my saving acount
◀
4 . 3
INSTALL BEHAVEINSTALL BEHAVE
Behave is a BDD tool written in Python
We will see other options for other languages at the
end of the talk
4 . 4
pip install behave
4 . 5
FOR OUR FIRST BDD TEST...FOR OUR FIRST BDD TEST...
WE ARE GOING TO NEED ...WE ARE GOING TO NEED ...
A features directory
Inside:
a directory named steps.
And a file named with extension
.feature*
* with the content show in the previous example
4 . 6
FOR OUR FIRST BDD TEST...FOR OUR FIRST BDD TEST...
WE ARE GOING TO NEED ...WE ARE GOING TO NEED ...
A features directory
Inside:
a directory named steps.
And a file named with extension
.feature*
* with the content show in the previous example
4 . 6
FOR OUR FIRST BDD TEST...FOR OUR FIRST BDD TEST...
WE ARE GOING TO NEED ...WE ARE GOING TO NEED ...
A features directory
Inside:
a directory named steps.
And a file named with extension
.feature*
* with the content show in the previous example
4 . 6
FOR OUR FIRST BDD TEST...FOR OUR FIRST BDD TEST...
WE ARE GOING TO NEED ...WE ARE GOING TO NEED ...
A features directory
Inside:
a directory named steps.
And a file named with extension
.feature*
* with the content show in the previous example
4 . 6
mkdir features
mkdir features/steps
vim features/transfer-between-accounts.feature
4 . 7
FIRST TEST WRITTEN!FIRST TEST WRITTEN!
AND IT IS EXECUTABLE!AND IT IS EXECUTABLE!
To execute it, just type on the command line:
behave
4 . 8
Feature: Transfer between accounts # features/transfer-between
Scenario: Add savings # features/t
Given I have 10000€ in my current acount # None
And I have 300€ in my saving accout # None
When I transfer 200€ from current to saving # None
Then I should have 9800€ in my current acount # None
And I should have 500€ in my saving acount # None
Failing scenarios:
features/transfer-between-accounts.feature:3 Add savings
0 features passed, 1 failed, 0 skipped
4 . 9
You can implement step definitions for undefined steps with th
@given(u'I have 10000€ in my current acount')
def step_impl(context):
raise NotImplementedError(u'STEP: Given I have 10000€ in m
@given(u'I have 300€ in my saving accout')
def step_impl(context):
raise NotImplementedError(u'STEP: Given I have 300€ in my
@when(u'I transfer 200€ from current to saving')
def step_impl(context):
raise NotImplementedError(u'STEP: When I transfer 200€ fro
4 . 10
IT WORKS!IT WORKS!
And It even gives us a scheleton of the functions we
need to implement to run the tests
◀
4 . 11
A SIMPLE TESTA SIMPLE TEST
Let's see a first implementation
Given thath we have no real code to test for now,
use a simulation
4 . 12
GIVEN...GIVEN...
#!/usr/bin/env python
from behave import *
@given("I have 10000€ in my current acount")
def step_impl(context):
context.current_account = 10000
@given("I have 300€ in my saving accout")
def step_impl(context):
context.savings_account = 300
4 . 13
WHEN...WHEN...
@when("I transfer 200€ from current to saving")
def step_impl(context):
context.savings_account += 200
context.current_account -= 200
4 . 14
THEN...THEN...
@then("I should have 9800€ in my current acount")
def step_impl(context):
assert context.current_account == 9800
@then("I should have 500€ in my saving acount")
def step_impl(context):
assert context.savings_account == 500
4 . 15
EXECUTE BEHAVEEXECUTE BEHAVE
Feature: Transfer between accounts # features_002/transfer-bet
Scenario: Add savings # features_0
Given I have 10000€ in my current acount # features_0
And I have 300€ in my saving accout # features_0
When I transfer 200€ from current to saving # features_0
Then I should have 9800€ in my current acount # features_0
And I should have 500€ in my saving acount # features_0
1 feature passed, 0 failed, 0 skipped
1 scenario passed, 0 failed, 0 skipped
5 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m0.000s
4 . 16
A bit more Theory
4 . 17
PARAMETRIZABLE STEPSPARAMETRIZABLE STEPS
We can parse the example text strings and get
information for the steps
The library is used by default
parse defines itself as the opposite of format()
parse
4 . 18
Change definition of first step from:
To:
@given("I have 10000€ in my current acount")
def step_impl(context):
context.current_account = 10000
@given("I have {amount}€ in my current acount")
def step_impl(context, amount):
context.current_account = int(amount)
4 . 19
Feature: Transfer between accounts # features_003/transfer-bet
Scenario: Add savings # features_0
Given I have 10000€ in my current acount # features_0
And I have 300€ in my saving accout # features_0
When I transfer 200€ from current to saving # features_0
Then I should have 9800€ in my current acount # features_0
And I should have 500€ in my saving acount # features_0
1 feature passed, 0 failed, 0 skipped
1 scenario passed, 0 failed, 0 skipped
5 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m0.001s
4 . 20
WE CAN EVEN FORGETWE CAN EVEN FORGET
ABOUT THE INTEGER CASTABOUT THE INTEGER CAST
@given("I have {amount:d}€ in my current acount")
def step_impl(context, amount):
context.current_account = amount
4 . 21
BEHAVE HAVE ALTERNATIVE PARSERSBEHAVE HAVE ALTERNATIVE PARSERS
parse is used by default
cfparse let us work with
cardinality
re to use regular expressions
4 . 22
BEHAVE HAVE ALTERNATIVE PARSERSBEHAVE HAVE ALTERNATIVE PARSERS
parse is used by default
cfparse let us work with
cardinality
re to use regular expressions
4 . 22
BEHAVE HAVE ALTERNATIVE PARSERSBEHAVE HAVE ALTERNATIVE PARSERS
parse is used by default
cfparse let us work with
cardinality
re to use regular expressions
4 . 22
BEHAVE HAVE ALTERNATIVE PARSERSBEHAVE HAVE ALTERNATIVE PARSERS
parse is used by default
cfparse let us work with
cardinality
re to use regular expressions
4 . 22
BUT BEFORE...BUT BEFORE...
Lets forget all this financial stuff nobody cares
for...
and use more interesting examples...
4 . 23
POKEMONPOKEMON
4 . 24
TABLE EXAMPLESTABLE EXAMPLES
We can add data to the steps in a tabular form
Feature: Pokemon search
Scenario: Find the weakest
Given I have this pokemons
|name | attack | defense |
|psyduck | 20 | 60 |
|torchic | 20 | 60 |
|spinda | 10 | 80 |
|lillipup | 10 | 50 |
When I search for the one with less defense
Then I should get lillipup
4 . 25
WE CAN PROCESS THIS TABLEWE CAN PROCESS THIS TABLE
IN THE STEP CODEIN THE STEP CODE
@given('I have this pokemons')
def step_impl(context):
for row in context.table:
pokemon = Pokemon(
row['name'],
attack=row['attack'],
defense=row['defense'],
)
setattr(context, row['name'], pokemon)
4 . 26
WE CAN HAVE SHARED PRECONDITIONSWE CAN HAVE SHARED PRECONDITIONS
FOR ALL THE STEPSFOR ALL THE STEPS
Feature: Transfer between accounts
Background:
Given I have this pokemons
|name | attack | defense |
|psyduck | 20 | 60 |
|torchic | 20 | 60 |
|spinda | 10 | 80 |
|lillipup | 10 | 50 |
Scenario: Search for the weakest
When I search for the one with less defense
Then I should get lillipup
4 . 27
TAGSTAGS
Selection of scenarios or features
Can select for one or several tags
Can execute the ones not having one or more
tags
4 . 28
TAGSTAGS
Selection of scenarios or features
Can select for one or several tags
Can execute the ones not having one or more
tags
4 . 28
TAGSTAGS
Selection of scenarios or features
Can select for one or several tags
Can execute the ones not having one or more
tags
4 . 28
TAGSTAGS
Selection of scenarios or features
Can select for one or several tags
Can execute the ones not having one or more
tags
4 . 28
FEATURE WITH TAGSFEATURE WITH TAGS
Asuming this feature and sample data
Feature: Search pokemons
Background:
Given I have this pokemons
|name | attack | defense |
|psyduck | 20 | 60 |
|torchic | 20 | 60 |
|trapinch | 20 | 60 |
|spinda | 10 | 80 |
|lillipup | 10 | 50 |
4 . 29
AND THIS STEPSAND THIS STEPS
@fight
Scenario: Search for the weakest
When I search for the one with less defense
Then I should get lillipup
@fight
Scenario: Search for the stronger
When I search for the one with more attack
Then I should get spinda
Scenario: Search for name
When I search for the letter t
Then I should get torchic
And I should get trapinch
4 . 30
EXECUTINGEXECUTING
JUST THE FIGHT SCENARIOSJUST THE FIGHT SCENARIOS
behave features_006 --tags=fight
4 . 31
EXECUTINGA ALL THE SCENARIOSEXECUTINGA ALL THE SCENARIOS
BUT THE FIGHT ONESBUT THE FIGHT ONES
behave features_006 --tags=-fight
4 . 32
WORKS IN PROGRESSWORKS IN PROGRESS
run with -w flag
turns off stdout capture
turns off logging capture
turns off pretty output
only runs scenarios tagged with
“@wip”
stops at the first error
4 . 33
TO BE HANDLE WITH CARETO BE HANDLE WITH CARE
5 . 1
RECOMENDATIONSRECOMENDATIONS
Don't mix features
One scenario to test just one case
it's code, store it in the repo
High level descriptions: Do not use technology
terms
5 . 2
DANGERSDANGERS
Don't make the system too complex
Don't get used to having some tests never passsing
Don't start testing the easy ones. You must go for the
ones that teaches you the most
5 . 3
DANGERSDANGERS
Don't make the system too complex
Don't get used to having some tests never passsing
Don't start testing the easy ones. You must go for the
ones that teaches you the most
5 . 3
DANGERSDANGERS
Don't make the system too complex
Don't get used to having some tests never passsing
Don't start testing the easy ones. You must go for the
ones that teaches you the most
5 . 3
DANGERSDANGERS
Don't make the system too complex
Don't get used to having some tests never passsing
Don't start testing the easy ones. You must go for the
ones that teaches you the most
5 . 3
RULE NUMBER 1 ON ANALISYS CLUBRULE NUMBER 1 ON ANALISYS CLUB
Don't tell me your solution
Instead, tell me your
problem
(Rule number 2 is Everybody lies, if you are interested)
5 . 4
TOOLS AND OPTIONSTOOLS AND OPTIONS
Ruby - Cucumber
Javascript -
Cucumber.js
.Net - xBehave
Java - JBehave
5 . 5
THANK YOU!THANK YOU!
6

More Related Content

PDF
PPTX
Tools for the Toolmakers
PPTX
Software development best practices & coding guidelines
PPTX
Stop wasting-time-by-applying-clean-code-principles
ODP
Tdd in php a brief example
PDF
Introduction to TDD (Test Driven development) - Ahmed Shreef
PDF
The Ring programming language version 1.7 book - Part 89 of 196
PPTX
Coding standard and coding guideline
Tools for the Toolmakers
Software development best practices & coding guidelines
Stop wasting-time-by-applying-clean-code-principles
Tdd in php a brief example
Introduction to TDD (Test Driven development) - Ahmed Shreef
The Ring programming language version 1.7 book - Part 89 of 196
Coding standard and coding guideline

What's hot (7)

PPTX
Test Driven Development (TDD) Preso 360|Flex 2010
PDF
TDD and Simple Design Workshop - Session 1 - November 2018
PPTX
Coding standards
PDF
C# conventions & good practices
PPSX
Coding standard
PPTX
The Art of Clean code
PPTX
Refactoring, 2nd Edition
Test Driven Development (TDD) Preso 360|Flex 2010
TDD and Simple Design Workshop - Session 1 - November 2018
Coding standards
C# conventions & good practices
Coding standard
The Art of Clean code
Refactoring, 2nd Edition
Ad

Similar to Introduction to BDD (Behavior-Driven Development) (20)

PPTX
UXDX London 2018 Nik Crabtree - Enhancing the Processes of Test Driven Develo...
PDF
Test Driven Infrastructure
PDF
Python overview
PDF
From the designers laptop to the users
PPTX
Top 20 cucumber interview questions for sdet
PPTX
DevLabs Alliance top 20 Cucumber Interview Questions for SDET
PPTX
DevLabs Alliance top 20 Cucumber Interview Questions for SDET
PDF
Save time by applying clean code principles
PPT
Importance Of Being Driven
PPTX
Test Driven Development (TDD) with FlexUnit 4 - 360|Flex San Jose preso
PDF
BDD & Cucumber
PDF
Cs121 Unit Test
PDF
Unison Language - Contact
PPTX
PHPConf.asia 2016 - BDD with Behat for Beginners
PDF
Behavior Driven Development with SpecFlow
PDF
Behavior Driven Testing with SpecFlow
PPTX
Chapter vvxxxxxxxxxxx1 - Part 1 (3).pptx
PDF
cucumber harpal.pdf
PPT
Android coding guide lines
UXDX London 2018 Nik Crabtree - Enhancing the Processes of Test Driven Develo...
Test Driven Infrastructure
Python overview
From the designers laptop to the users
Top 20 cucumber interview questions for sdet
DevLabs Alliance top 20 Cucumber Interview Questions for SDET
DevLabs Alliance top 20 Cucumber Interview Questions for SDET
Save time by applying clean code principles
Importance Of Being Driven
Test Driven Development (TDD) with FlexUnit 4 - 360|Flex San Jose preso
BDD & Cucumber
Cs121 Unit Test
Unison Language - Contact
PHPConf.asia 2016 - BDD with Behat for Beginners
Behavior Driven Development with SpecFlow
Behavior Driven Testing with SpecFlow
Chapter vvxxxxxxxxxxx1 - Part 1 (3).pptx
cucumber harpal.pdf
Android coding guide lines
Ad

More from Juan Rodríguez (20)

PPTX
Estados imposibles y como evitarlos
PDF
Vue.js: El framework javascript para muggles
PDF
Introducción a jupyter (antes i python notebook)
PDF
Acceder a C desde Python (O viceversa)
PDF
Viaje al centro de la locura con Javascript
ODP
Una imagen vale más que mil mentiras
ODP
Introducción a DSL (Lenguajes Específicos de Dominios) con Python
PDF
Presentación estetoscopio
PDF
Charla introducción a RaspberryPI
PDF
Guia (breve) de supervivencia a python 3
PDF
¡A todo Kanban! ~ Introducción a kanban
ODP
02 python Programación orientada a objetos y funcional
ODP
01 el lenguaje Python
ODP
00 introducción a Python
PDF
Taller de introducción al desarrollo web con Django
PDF
Presentación appy/pod
ODP
Presentacion Google Mini Adeje 16 Oct 2008
ODP
Algunos recursos para desarrollo Web
ODP
Hojas de estilo CSS (Cascade Style Sheets)
ODP
Extranet Parlamento
Estados imposibles y como evitarlos
Vue.js: El framework javascript para muggles
Introducción a jupyter (antes i python notebook)
Acceder a C desde Python (O viceversa)
Viaje al centro de la locura con Javascript
Una imagen vale más que mil mentiras
Introducción a DSL (Lenguajes Específicos de Dominios) con Python
Presentación estetoscopio
Charla introducción a RaspberryPI
Guia (breve) de supervivencia a python 3
¡A todo Kanban! ~ Introducción a kanban
02 python Programación orientada a objetos y funcional
01 el lenguaje Python
00 introducción a Python
Taller de introducción al desarrollo web con Django
Presentación appy/pod
Presentacion Google Mini Adeje 16 Oct 2008
Algunos recursos para desarrollo Web
Hojas de estilo CSS (Cascade Style Sheets)
Extranet Parlamento

Recently uploaded (20)

PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PPTX
ai tools demonstartion for schools and inter college
PPT
JAVA ppt tutorial basics to learn java programming
PPTX
Essential Infomation Tech presentation.pptx
PPTX
ISO 45001 Occupational Health and Safety Management System
PPTX
Online Work Permit System for Fast Permit Processing
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
System and Network Administraation Chapter 3
PDF
Softaken Excel to vCard Converter Software.pdf
DOCX
The Five Best AI Cover Tools in 2025.docx
PPTX
history of c programming in notes for students .pptx
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PPTX
Introduction to Artificial Intelligence
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Which alternative to Crystal Reports is best for small or large businesses.pdf
ai tools demonstartion for schools and inter college
JAVA ppt tutorial basics to learn java programming
Essential Infomation Tech presentation.pptx
ISO 45001 Occupational Health and Safety Management System
Online Work Permit System for Fast Permit Processing
PTS Company Brochure 2025 (1).pdf.......
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Odoo POS Development Services by CandidRoot Solutions
Internet Downloader Manager (IDM) Crack 6.42 Build 41
System and Network Administraation Chapter 3
Softaken Excel to vCard Converter Software.pdf
The Five Best AI Cover Tools in 2025.docx
history of c programming in notes for students .pptx
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Introduction to Artificial Intelligence
Design an Analysis of Algorithms II-SECS-1021-03
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool

Introduction to BDD (Behavior-Driven Development)

  • 1. INTRODUCTION TO BDDINTRODUCTION TO BDD BEHAVIOR DRIVEN DEVELOPMENTBEHAVIOR DRIVEN DEVELOPMENT 1
  • 3. whoamiwhoami Juan Ignacio Rodríguez de león @jileon en Twitter Backend developer en OctopusLabs 2 . 2
  • 4. WE ARE HIRINGWE ARE HIRING More info at: https://octopuslabs.com/careers 2 . 3
  • 5. HOW IS THIS TALK STRUCTURED?HOW IS THIS TALK STRUCTURED? Presentation (Finished!) Teory Practice Libraries, tools, recomendations... 2 . 4
  • 7. In theory, there is no difference between theory and practice. In practice, there is. -- Benjamin Brewster 3 . 2
  • 8. WHAT IS BDDWHAT IS BDD As stated by wikipedia: ... an extension of TDD that makes use of a simple, domain-specific scripting language structured like natural language statements to ensures all development projects remain focused on delivering what the business actually needs while meeting all requirements. 3 . 3
  • 9. manman WATH IS BDDWATH IS BDD As stated by wikipedia: ... an extension of TDD that makes use of a simple, domain-specific scripting language structured like natural language statements to ensures all development projects remain focused on delivering what the business actually needs while meeting all requirements. 3 . 4
  • 10. TLDRTLDR They are tests. As in TDD, we use test to drive the design of the so ware Usually, on a higher level than TDD Written in a natural lenguage , like English Example 3 . 5
  • 11. TLDRTLDR They are tests. As in TDD, we use test to drive the design of the so ware Usually, on a higher level than TDD Written in a natural lenguage , like English Example 3 . 5
  • 12. TLDRTLDR They are tests. As in TDD, we use test to drive the design of the so ware Usually, on a higher level than TDD Written in a natural lenguage , like English Example 3 . 5
  • 13. TLDRTLDR They are tests. As in TDD, we use test to drive the design of the so ware Usually, on a higher level than TDD Written in a natural lenguage , like English Example 3 . 5
  • 14. TARGET: SHARE KNOWLEDGETARGET: SHARE KNOWLEDGE A natural lenguaje: Is used for all of us Everyone can define, debate o improve a test Allow all the team to discuss on tipically conflictive topics: edge cases, exceptions... 3 . 6
  • 15. TARGET: SHARE KNOWLEDGETARGET: SHARE KNOWLEDGE A natural lenguaje: Is used for all of us Everyone can define, debate o improve a test Allow all the team to discuss on tipically conflictive topics: edge cases, exceptions... 3 . 6
  • 16. TARGET: SHARE KNOWLEDGETARGET: SHARE KNOWLEDGE A natural lenguaje: Is used for all of us Everyone can define, debate o improve a test Allow all the team to discuss on tipically conflictive topics: edge cases, exceptions... 3 . 6
  • 17. TARGET: SHARE KNOWLEDGETARGET: SHARE KNOWLEDGE A natural lenguaje: Is used for all of us Everyone can define, debate o improve a test Allow all the team to discuss on tipically conflictive topics: edge cases, exceptions... 3 . 6
  • 18. EXECUTABLE SPECIFICATIONSEXECUTABLE SPECIFICATIONS Using this test as specifications for our so ware, we get specifications written on a plain english, but executables. 3 . 7
  • 19. EXECUTABLE SPECIFICATIONSEXECUTABLE SPECIFICATIONS Using this test as specifications for our so ware, we get specifications written on a plain english, but executables. 3 . 7
  • 20. HOW CAN YOU "EXECUTE" ENGLISH?HOW CAN YOU "EXECUTE" ENGLISH? Transform a plain english specification in a test Example 3 . 8
  • 21. NOW WE HAVE TESTS!NOW WE HAVE TESTS! But, obviously, we need to implement the code. No black magic Before that, lets review a few details about files and organization. 3 . 9
  • 22. GHERKINGHERKIN The format used in this specification is called Gherkin There are just a few words that have a special meaning: Feature, Scenario, Given, When y Then. (Also And, But) 3 . 10
  • 23. FEATUREFEATURE Every feature is a capacity of our product. Each feature must have its own .feature file The keyword Feature must go at the begining, and gives us the opportunity to descrive the feature in one line The rest of text between the feature line and the first scenario can be used to describe the feature with more detail 3 . 11
  • 24. SCENARIOSCENARIO Usually, a feature has more than one scenario, because we are interested on testing diferent aspects: changing conditiones, edge cases, different inputs, etc... Every diferent case is defined with the keyboard Scenario As a rule, every scenario is a complete and concrete example of a feature 3 . 12
  • 25. GIVENGIVEN Given put our system in a known and controled state, before the first intereaction. (No user interactions if this part) 3 . 13
  • 26. WHENWHEN When This is where we describe the actions the user or external system wants to do. This must yield some changes in the system. Or maybe not 3 . 14
  • 27. THENTHEN Then This is where we check that the outcomes of our system are the expected 3 . 15
  • 28. AND Y BUTAND Y BUT And y But are alternative versions of Given and Then just to get a better feeling of proper english. So, instead of writing: We can write: Given I have 10000€ in my current acount Given I have 300€ in my saving accout Given I have 10000€ in my current acount And I have 300€ in my saving accout 3 . 16
  • 29. GO BACK TO PRACTICEGO BACK TO PRACTICE LETS IMPLEMENT THE TESTLETS IMPLEMENT THE TEST Example 3 . 17
  • 30. A FEW INTERESTING DETAILSA FEW INTERESTING DETAILS ON STEPSON STEPS The name of the function used to implemente the steps it's not important! Behave decide what funciones to call, and when, based only in the text of decorator Main idea is having reusable steps 3 . 18
  • 31. A FEW INTERESTING DETAILSA FEW INTERESTING DETAILS ON STEPSON STEPS The name of the function used to implemente the steps it's not important! Behave decide what funciones to call, and when, based only in the text of decorator Main idea is having reusable steps 3 . 18
  • 32. A FEW INTERESTING DETAILSA FEW INTERESTING DETAILS ON STEPSON STEPS The name of the function used to implemente the steps it's not important! Behave decide what funciones to call, and when, based only in the text of decorator Main idea is having reusable steps 3 . 18
  • 33. REQUESTREQUEST Every step has a common first parameter called request, that could be used for Sharing data This data can be shared at a feaure level or a scenario level 3 . 19
  • 34. REQUESTREQUEST Every step has a common first parameter called request, that could be used for Sharing data This data can be shared at a feaure level or a scenario level 3 . 19
  • 35. PARAMETER CAPTUREPARAMETER CAPTURE One interesting ability is to "capture" some part of the text of the step This allow us to have parametrizable, and hence, more reusable steps Lets see this with another example 3 . 20
  • 36. DIFFERENCES WITH TDDDIFFERENCES WITH TDD Emphasis is not in the code, but in the functionality Specification is written in natural language (i.e. english) Spoken by all the members of the team: Developers testers, Q/A, product owners, skateholders, users... 3 . 21
  • 37. DIFFERENCES WITH TDDDIFFERENCES WITH TDD Emphasis is not in the code, but in the functionality Specification is written in natural language (i.e. english) Spoken by all the members of the team: Developers testers, Q/A, product owners, skateholders, users... 3 . 21
  • 38. DIFFERENCES WITH TDDDIFFERENCES WITH TDD Emphasis is not in the code, but in the functionality Specification is written in natural language (i.e. english) Spoken by all the members of the team: Developers testers, Q/A, product owners, skateholders, users... 3 . 21
  • 39. DIFFERENCES WITH TDDDIFFERENCES WITH TDD Emphasis is not in the code, but in the functionality Specification is written in natural language (i.e. english) Spoken by all the members of the team: Developers testers, Q/A, product owners, skateholders, users... 3 . 21
  • 41. A FEATURE FILE EXAMPLEA FEATURE FILE EXAMPLE 4 . 2
  • 42. Feature: Transfer between accounts Scenario: Add savings Given I have 10.000€ in my current acount And I have 300€ in my saving accout When I transfer 200€ from current to saving Then I should have 9.800€ in my current acount And I should have 500€ in my saving acount ◀ 4 . 3
  • 43. INSTALL BEHAVEINSTALL BEHAVE Behave is a BDD tool written in Python We will see other options for other languages at the end of the talk 4 . 4
  • 45. FOR OUR FIRST BDD TEST...FOR OUR FIRST BDD TEST... WE ARE GOING TO NEED ...WE ARE GOING TO NEED ... A features directory Inside: a directory named steps. And a file named with extension .feature* * with the content show in the previous example 4 . 6
  • 46. FOR OUR FIRST BDD TEST...FOR OUR FIRST BDD TEST... WE ARE GOING TO NEED ...WE ARE GOING TO NEED ... A features directory Inside: a directory named steps. And a file named with extension .feature* * with the content show in the previous example 4 . 6
  • 47. FOR OUR FIRST BDD TEST...FOR OUR FIRST BDD TEST... WE ARE GOING TO NEED ...WE ARE GOING TO NEED ... A features directory Inside: a directory named steps. And a file named with extension .feature* * with the content show in the previous example 4 . 6
  • 48. FOR OUR FIRST BDD TEST...FOR OUR FIRST BDD TEST... WE ARE GOING TO NEED ...WE ARE GOING TO NEED ... A features directory Inside: a directory named steps. And a file named with extension .feature* * with the content show in the previous example 4 . 6
  • 49. mkdir features mkdir features/steps vim features/transfer-between-accounts.feature 4 . 7
  • 50. FIRST TEST WRITTEN!FIRST TEST WRITTEN! AND IT IS EXECUTABLE!AND IT IS EXECUTABLE! To execute it, just type on the command line: behave 4 . 8
  • 51. Feature: Transfer between accounts # features/transfer-between Scenario: Add savings # features/t Given I have 10000€ in my current acount # None And I have 300€ in my saving accout # None When I transfer 200€ from current to saving # None Then I should have 9800€ in my current acount # None And I should have 500€ in my saving acount # None Failing scenarios: features/transfer-between-accounts.feature:3 Add savings 0 features passed, 1 failed, 0 skipped 4 . 9
  • 52. You can implement step definitions for undefined steps with th @given(u'I have 10000€ in my current acount') def step_impl(context): raise NotImplementedError(u'STEP: Given I have 10000€ in m @given(u'I have 300€ in my saving accout') def step_impl(context): raise NotImplementedError(u'STEP: Given I have 300€ in my @when(u'I transfer 200€ from current to saving') def step_impl(context): raise NotImplementedError(u'STEP: When I transfer 200€ fro 4 . 10
  • 53. IT WORKS!IT WORKS! And It even gives us a scheleton of the functions we need to implement to run the tests ◀ 4 . 11
  • 54. A SIMPLE TESTA SIMPLE TEST Let's see a first implementation Given thath we have no real code to test for now, use a simulation 4 . 12
  • 55. GIVEN...GIVEN... #!/usr/bin/env python from behave import * @given("I have 10000€ in my current acount") def step_impl(context): context.current_account = 10000 @given("I have 300€ in my saving accout") def step_impl(context): context.savings_account = 300 4 . 13
  • 56. WHEN...WHEN... @when("I transfer 200€ from current to saving") def step_impl(context): context.savings_account += 200 context.current_account -= 200 4 . 14
  • 57. THEN...THEN... @then("I should have 9800€ in my current acount") def step_impl(context): assert context.current_account == 9800 @then("I should have 500€ in my saving acount") def step_impl(context): assert context.savings_account == 500 4 . 15
  • 58. EXECUTE BEHAVEEXECUTE BEHAVE Feature: Transfer between accounts # features_002/transfer-bet Scenario: Add savings # features_0 Given I have 10000€ in my current acount # features_0 And I have 300€ in my saving accout # features_0 When I transfer 200€ from current to saving # features_0 Then I should have 9800€ in my current acount # features_0 And I should have 500€ in my saving acount # features_0 1 feature passed, 0 failed, 0 skipped 1 scenario passed, 0 failed, 0 skipped 5 steps passed, 0 failed, 0 skipped, 0 undefined Took 0m0.000s 4 . 16
  • 59. A bit more Theory 4 . 17
  • 60. PARAMETRIZABLE STEPSPARAMETRIZABLE STEPS We can parse the example text strings and get information for the steps The library is used by default parse defines itself as the opposite of format() parse 4 . 18
  • 61. Change definition of first step from: To: @given("I have 10000€ in my current acount") def step_impl(context): context.current_account = 10000 @given("I have {amount}€ in my current acount") def step_impl(context, amount): context.current_account = int(amount) 4 . 19
  • 62. Feature: Transfer between accounts # features_003/transfer-bet Scenario: Add savings # features_0 Given I have 10000€ in my current acount # features_0 And I have 300€ in my saving accout # features_0 When I transfer 200€ from current to saving # features_0 Then I should have 9800€ in my current acount # features_0 And I should have 500€ in my saving acount # features_0 1 feature passed, 0 failed, 0 skipped 1 scenario passed, 0 failed, 0 skipped 5 steps passed, 0 failed, 0 skipped, 0 undefined Took 0m0.001s 4 . 20
  • 63. WE CAN EVEN FORGETWE CAN EVEN FORGET ABOUT THE INTEGER CASTABOUT THE INTEGER CAST @given("I have {amount:d}€ in my current acount") def step_impl(context, amount): context.current_account = amount 4 . 21
  • 64. BEHAVE HAVE ALTERNATIVE PARSERSBEHAVE HAVE ALTERNATIVE PARSERS parse is used by default cfparse let us work with cardinality re to use regular expressions 4 . 22
  • 65. BEHAVE HAVE ALTERNATIVE PARSERSBEHAVE HAVE ALTERNATIVE PARSERS parse is used by default cfparse let us work with cardinality re to use regular expressions 4 . 22
  • 66. BEHAVE HAVE ALTERNATIVE PARSERSBEHAVE HAVE ALTERNATIVE PARSERS parse is used by default cfparse let us work with cardinality re to use regular expressions 4 . 22
  • 67. BEHAVE HAVE ALTERNATIVE PARSERSBEHAVE HAVE ALTERNATIVE PARSERS parse is used by default cfparse let us work with cardinality re to use regular expressions 4 . 22
  • 68. BUT BEFORE...BUT BEFORE... Lets forget all this financial stuff nobody cares for... and use more interesting examples... 4 . 23
  • 70. TABLE EXAMPLESTABLE EXAMPLES We can add data to the steps in a tabular form Feature: Pokemon search Scenario: Find the weakest Given I have this pokemons |name | attack | defense | |psyduck | 20 | 60 | |torchic | 20 | 60 | |spinda | 10 | 80 | |lillipup | 10 | 50 | When I search for the one with less defense Then I should get lillipup 4 . 25
  • 71. WE CAN PROCESS THIS TABLEWE CAN PROCESS THIS TABLE IN THE STEP CODEIN THE STEP CODE @given('I have this pokemons') def step_impl(context): for row in context.table: pokemon = Pokemon( row['name'], attack=row['attack'], defense=row['defense'], ) setattr(context, row['name'], pokemon) 4 . 26
  • 72. WE CAN HAVE SHARED PRECONDITIONSWE CAN HAVE SHARED PRECONDITIONS FOR ALL THE STEPSFOR ALL THE STEPS Feature: Transfer between accounts Background: Given I have this pokemons |name | attack | defense | |psyduck | 20 | 60 | |torchic | 20 | 60 | |spinda | 10 | 80 | |lillipup | 10 | 50 | Scenario: Search for the weakest When I search for the one with less defense Then I should get lillipup 4 . 27
  • 73. TAGSTAGS Selection of scenarios or features Can select for one or several tags Can execute the ones not having one or more tags 4 . 28
  • 74. TAGSTAGS Selection of scenarios or features Can select for one or several tags Can execute the ones not having one or more tags 4 . 28
  • 75. TAGSTAGS Selection of scenarios or features Can select for one or several tags Can execute the ones not having one or more tags 4 . 28
  • 76. TAGSTAGS Selection of scenarios or features Can select for one or several tags Can execute the ones not having one or more tags 4 . 28
  • 77. FEATURE WITH TAGSFEATURE WITH TAGS Asuming this feature and sample data Feature: Search pokemons Background: Given I have this pokemons |name | attack | defense | |psyduck | 20 | 60 | |torchic | 20 | 60 | |trapinch | 20 | 60 | |spinda | 10 | 80 | |lillipup | 10 | 50 | 4 . 29
  • 78. AND THIS STEPSAND THIS STEPS @fight Scenario: Search for the weakest When I search for the one with less defense Then I should get lillipup @fight Scenario: Search for the stronger When I search for the one with more attack Then I should get spinda Scenario: Search for name When I search for the letter t Then I should get torchic And I should get trapinch 4 . 30
  • 79. EXECUTINGEXECUTING JUST THE FIGHT SCENARIOSJUST THE FIGHT SCENARIOS behave features_006 --tags=fight 4 . 31
  • 80. EXECUTINGA ALL THE SCENARIOSEXECUTINGA ALL THE SCENARIOS BUT THE FIGHT ONESBUT THE FIGHT ONES behave features_006 --tags=-fight 4 . 32
  • 81. WORKS IN PROGRESSWORKS IN PROGRESS run with -w flag turns off stdout capture turns off logging capture turns off pretty output only runs scenarios tagged with “@wip” stops at the first error 4 . 33
  • 82. TO BE HANDLE WITH CARETO BE HANDLE WITH CARE 5 . 1
  • 83. RECOMENDATIONSRECOMENDATIONS Don't mix features One scenario to test just one case it's code, store it in the repo High level descriptions: Do not use technology terms 5 . 2
  • 84. DANGERSDANGERS Don't make the system too complex Don't get used to having some tests never passsing Don't start testing the easy ones. You must go for the ones that teaches you the most 5 . 3
  • 85. DANGERSDANGERS Don't make the system too complex Don't get used to having some tests never passsing Don't start testing the easy ones. You must go for the ones that teaches you the most 5 . 3
  • 86. DANGERSDANGERS Don't make the system too complex Don't get used to having some tests never passsing Don't start testing the easy ones. You must go for the ones that teaches you the most 5 . 3
  • 87. DANGERSDANGERS Don't make the system too complex Don't get used to having some tests never passsing Don't start testing the easy ones. You must go for the ones that teaches you the most 5 . 3
  • 88. RULE NUMBER 1 ON ANALISYS CLUBRULE NUMBER 1 ON ANALISYS CLUB Don't tell me your solution Instead, tell me your problem (Rule number 2 is Everybody lies, if you are interested) 5 . 4
  • 89. TOOLS AND OPTIONSTOOLS AND OPTIONS Ruby - Cucumber Javascript - Cucumber.js .Net - xBehave Java - JBehave 5 . 5