Introduction to BDD with Cucumber for Java
-
Upload
seb-rose -
Category
Technology
-
view
4.382 -
download
1
Transcript of Introduction to BDD with Cucumber for Java
Seb Rose [email protected]
@sebrose h0p://cucumber.io
An Introduc9on to Behaviour Driven Development
with Cucumber for Java
@sebrose h)p://cucumber.io
Agenda
-‐ What is BDD? -‐ What is Cucumber? -‐ Demo Cucumber for Java -‐ Cucumber variants -‐ Pu@ng it all together
@sebrose h)p://cucumber.io
BDD is a second-‐generaBon, outside-‐in, pull-‐based, mulBple-‐stakeholder, mulBple-‐scale, high-‐automaBon, agile methodology.
It describes a cycle of interacBons with well-‐defined outputs, resulBng in the delivery of working, tested soJware that ma)ers.
Dan Northh)p://skillsma)er.com/podcast/java-‐jee/how-‐to-‐sell-‐bdd-‐to-‐the-‐business
BDD defined
@sebrose h)p://cucumber.io
The power of “should”
-‐ Diverts developers from their distaste for tesBng
-‐ Encourages people to ask “Should it really?”
@sebrose h)p://cucumber.io
Deliberate discovery“… during an incepBon, when we are most ignorant about most aspects of the project,
the best use we can possibly make of the Bme available is to a)empt to idenBfy and reduce
our ignorance …”
Dan Northh)p://dannorth.net/2010/08/30/introducing-‐deliberate-‐discovery/
@sebrose h)p://cucumber.io
Ubiquitous language“The pracBce of building up a common, rigorous language between developers and users”
MarBn Fowler
@sebrose h)p://cucumber.io
The 3 Amigos
The one where...
User stories & acceptance criteria
Examples
Open quesBons
Domain learning
@sebrose h)p://cucumber.io
Example mapping
Acceptance
Criterion
(Rule)
User Story
Example
Open
Question
Acceptance
Criterion
(Rule)
Acceptance
Criterion
(Rule)
Example
Example
Example Example
Example
@sebrose h)p://cucumber.io
Important conversaBonshaving conversaBons is more important than capturing conversaBons is more important than
automa.ng conversaBons
Liz Keoghh)p://lizkeogh.com/2014/01/22/using-‐bdd-‐with-‐legacy-‐systems/
@sebrose h)p://cucumber.io
Living documentaBona.k.a Executable specificaBon
Living documentaBon is a reliable and authoritaBve source of informaBon on system funcBonality, which anyone can easily access. It is as reliable as the code, but much easier to
read and understand. Gojko Adzic
h)p://specificaBonbyexample.com/key_ideas.html
@sebrose h)p://cucumber.io
What BDD is not….
-‐ Using “Given/When/Then” -‐ The responsibility of testers -‐ An alternaBve to manual tesBng
@sebrose h)p://cucumber.io
What is Cucumber?
Features
-Scenarios -‐Steps-Gherkin
Glue code
-Step definiBons-Ruby -Java -C# & others
ApplicaBon
@sebrose h)p://cucumber.io
Feature: Team Scoring Teams start with zero score. Correct answer gets points depending on how difficult it is.
Scenario: New teams should not have scored yet Given I register a team Then my score is 0
Scenario: Correctly answering a question scores points Given I register a team When I submit a correct answer Then my score is 10
@sebrose h)p://cucumber.io
Feature: Feature name Descrip8on of feature goes here
Scenario: Scenario name Descrip8on of scenario goes here
Given a certain context When something happens Then an outcome And something else But not this though
Scenario: Another scenario name ...
Gherkin fundamentals
@sebrose h)p://cucumber.io
Given(/^I register a team$/) do # Glue code goes here end
Ruby
@sebrose h)p://cucumber.io
import cucumber.api.java.en.*;
public class MyStepDefinitions {
@Given("^I register a team$") public void iRegisterATeam() throws Throwable { // Glue code goes here }}
Java
@sebrose h)p://cucumber.io
@sebrose h)p://cucumber.io
Cucumber variants• Ruby • C# (Specflow) • Java (& other JVM languages) • Javascript • PHP • Python • C++
@sebrose h)p://cucumber.io
Given/When/Then namespaces
Global namespace -‐ Given/When/Then interchangeable
Separate namespaces -‐ Given/When/Then disBnct -‐ And/But bind to preceding namespace -‐ [StepDefiniBon] for compaBbility
@sebrose h)p://cucumber.io
Binding & test frameworks
Behind the scenes -‐ may need to specify paths -‐ select required plugin(s)
Some magic code generaBon -‐ NUnit by default -‐ configuraBon changes for others -‐ several output opBons
@sebrose h)p://cucumber.io
Sharing data between steps
Varies by implementaBon -‐ Ruby, Javascript: World object -‐ Java: Dependency InjecBon
Context object(s) -‐ Injected -‐ Scenario -‐ Feature
@sebrose h)p://cucumber.io
Hooks
Before & AJer Ruby: Around, AJerStep
Before & AJer BeforeStep & AJerStep BeforeFeature & AJerFeature BeforeTestRun & AJerTestRun BeforeScenarioBlock & AJerScenarioBlock
@sebrose h)p://cucumber.io
Pu@ng it all together
SoJware development is hard.
Understanding what your methods and tools
are good for can make it easier.
@sebrose h)p://cucumber.io
BDD is a second-‐generaBon, outside-‐in, pull-‐based, mulBple-‐stakeholder, mulBple-‐scale, high-‐automaBon, agile methodology.
It describes a cycle of interacBons with well-‐defined outputs, resulBng in the delivery of working, tested soJware that ma)ers.
Dan Northh)p://skillsma)er.com/podcast/java-‐jee/how-‐to-‐sell-‐bdd-‐to-‐the-‐business
BDD redefined
@sebrose h)p://cucumber.io
h)ps://cucumber.pro/blog/2014/03/03/the-‐worlds-‐most-‐misunderstood-‐collaboraBon-‐tool.html
When you do BDD/SpecificaBon by Example and Outside-‐in, regression tests fall out at the other end. They are a by-‐product of those acBviBes. TesBng isn't the acBvity itself.
Cucumber is first a foremost a collaboraBon tool that aims to bring a common understanding to soJware teams -‐ across roles.
Aslak Hellesøy
It’s about collaboraBon
@sebrose h)p://cucumber.ioh)p://claysnow.co.uk/architectural-‐alignment-‐and-‐test-‐induced-‐design-‐damage-‐fallacy/
Test = check + explore
@sebrose h)p://cucumber.io
h)p://1.bp.blogspot.com/_YzKCMr-‐tcMM/TFLIGeqTJfI/AAAAAAAAARQ/AhNW62KX5EA/s1600/cartoon6.jpg
AutomaBon is development
@sebrose h)p://cucumber.io
Understand designs
Features
-Scenarios -‐Steps-Gherkin
Glue code
-Step definiBons-Ruby -Java -C# & others ApplicaBon
Support code
@sebrose h)p://cucumber.io
This page intenBonally blank.
<role> will write scenarios
@sebrose h)p://cucumber.io
“I get paid for code that works, not for tests, so my philosophy is to test as li)le as possible to reach a given level of confidence ...
“I suspect this level of confidence is high compared to industry standards”
h)p://stackoverflow.com/quesBons/153234/how-‐deep-‐are-‐your-‐unit-‐tests/153565#153565
Kent Beck
It’s about confidence
@sebrose h)p://cucumber.io
ImperaBve DeclaraBve
Avoid incidental details
Keep it focussed
h)p://lizkeogh.com/2011/06/27/atdd-‐vs-‐bdd-‐and-‐a-‐po)ed-‐history-‐of-‐some-‐related-‐stuff/
What’s the difference
between TDD, ATDD, BDD and SbE?
They’re called
different things
The best TDD pracBBoners...
•Work from the outside-‐in, i.e. test-‐first •Use examples to clarify their requirements •Develop and use a ubiquitous language
The best BDD pracBBoners...
• Focus on value • Discover examples collabora7vely • Create living documenta7on
@sebrose h)p://cucumber.io
Take aways
• BDD is about collaboraBon • You need a ubiquitous language • Cucumber can power conversaBon • and produce living documentaBon • but it’s no subsBtute for tesBng!
Seb Rose Twi)er: @sebrose Blog: www.claysnow.co.uk E-‐mail: [email protected]
QuesBons?
Discou
nt cod
e at
pragpr
og.com
Java_O
ne_25%
_Off_C
uke
Valid
for 1
month
@sebrose h)p://cucumber.io
h)ps://cemarking.net/wp-‐content/uploads/2014/01/User-‐Manual_featured.jpg
DocumentaBon
Acceptance criteria
User Story
Examples
Feature: Team Scoring Teams start with zero score. Correct answer gets points depending on how difficult it is.
Scenario: Score starts at 0 Given I register a team Then my score is 0
Scenario: Correct easy answer scores 10 Given I register a team When I submit a correct easy answer Then my score is 10
Scenario: Correct hard answer scores 50 Given I register a team When I submit a correct hard answer Then my score is 50
Step outcome!Passed
" No exception thrown
!Pending" Pending exception thrown
!Undefined" No matching step definition found
!Failed" Any other exception thrown
!Skipped" Steps that follow a Pending, Undefined or Failed step
!Duplicate" Step matches more than one step definition
@sebrose http://claysnow.co.uk
Scenario outcome
!Passed" All steps passed
!Failed" Any step failed
!Undefined" No failed steps" At least one undefined step
!Pending" No failed or undefined steps" At least one pending step
@sebrose http://claysnow.co.uk