10 things about bdd, cucumber and specflow sdd 2016

77
@sebrose h)p://cucumber.io Seb Rose Cucumber Limited [email protected] 10 things you need to know about Specflow

Upload: seb-rose

Post on 08-Feb-2017

354 views

Category:

Technology


2 download

TRANSCRIPT

@sebrose h)p://cucumber.io

Seb Rose Cucumber Limited [email protected]

10 things you need to know about Specflow

@sebrose h)p://cucumber.io

Seb Rose Cucumber Limited [email protected]

10 things you need to know about BDD, Cucumber and Specflow

@sebrose h)p://cucumber.io

0. Know your BDD history

@sebrose h)p://cucumber.io

Last century?

h)p://www.serena.com/docs/agile/papers/Managing-The-Development-of-Large-SoEware-Systems.pdf

1970

@sebrose h)p://cucumber.io

2002

2006

1995

1997

1990s 1994

1980s

@sebrose h)p://cucumber.io

Agile manifesto signatories

2001

@sebrose h)p://cucumber.ioh)p://media.mediatemple.netdna-cdn.com/wp-content/uploads/2013/01/floops_loops.png

@sebrose h)p://cucumber.io

Chris Ma)s working with Rohit Darji develop

“Pair Analysis”

2001

@sebrose h)p://cucumber.io

While using and teaching agile pracUces like test-driven development (TDD) on projects in different environments, I kept coming across the same confusion and misunderstandings.

Dan North c. 2002

h)p://dannorth.net/introducing-bdd/

@sebrose h)p://cucumber.io

2002 (published 2005)

@sebrose h)p://cucumber.io

Chris Ma)s working with Sanela Hodzic formalise

“Break the model”

2003

@sebrose h)p://cucumber.io

At the end of 2003, I decided it was Ume to put my money – or at least my Ume – where my mouth was. I started wriUng a replacement for JUnit called JBehave, which removed any reference to tesUng and replaced it with a vocabulary built around verifying behaviour.

Dan North

h)p://dannorth.net/introducing-bdd/

@sebrose h)p://cucumber.io

Chris Ma)s and Dan North discuss the power of “should” on a flight back from

Agile Development Conference 2004

@sebrose h)p://cucumber.io

@sebrose h)p://cucumber.io

Given/Then/When sees the light of day

Acceptance Criteria: Prevent cash withdrawal when overdrawn

Given successfully login from user login and the account is in overdrawn [balance = -50] When the user requests cash [request 20]Then refuse cash and return card

abc.truemesh.com/archives/2004_11.html

@sebrose h)p://cucumber.io

@sebrose h)p://cucumber.io

Feature InjecUon & Real OpUons

2007 (published 2014)

@sebrose h)p://cucumber.io2008 (published 2012)

@sebrose h)p://cucumber.io

@sebrose h)p://cucumber.io

@sebrose h)p://cucumber.io

2009

h)p://www.specflow.org/media/specflow_logo.png

@sebrose h)p://cucumber.io

2009 2011

@sebrose h)p://cucumber.io

@sebrose h)p://cucumber.io

BDD is a second-generaUon, outside-in, pull-based, mulUple-stakeholder, mulUple-scale, high-automaUon, agile methodology.

It describes a cycle of interacUons with well-defined outputs, resulUng in the delivery of working, tested soEware that ma)ers.

Dan Northh)p://skillsma)er.com/podcast/java-jee/how-to-sell-bdd-to-the-business

@sebrose h)p://cucumber.io

1. Different names, same idea

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

@sebrose h)p://cucumber.io

The best TDD pracUUoners...

Work from the outside-in, i.e. test-first

Use examples to clarify their requirements

Develop and use a ubiquitous language

@sebrose h)p://cucumber.io

The best BDD pracUUoners...

Focus on value

Discover examples collaboraAvely

Create living documentaAon

@sebrose h)p://cucumber.io

2. Tools are NOT essenAal

@sebrose h)p://cucumber.io

BDD is a second-generaUon, outside-in, pull-based, mulUple-stakeholder, mulUple-scale, high-automaUon, agile methodology.

It describes a cycle of interacUons with well-defined outputs, resulUng in the delivery of working, tested soEware that ma)ers.

Dan North

h)p://skillsma)er.com/podcast/java-jee/how-to-sell-bdd-to-the-business

@sebrose h)p://cucumber.io

having conversa*ons is more important than capturing conversa*ons is more important than

automa3ng conversa*ons

Liz Keogh

h)p://lizkeogh.com/2014/01/22/using-bdd-with-legacy-systems/

@sebrose h)p://cucumber.ioh)p://lizkeogh.com/2011/03/04/step-away-from-the-tools/

Have enough conversaUons to know how to get started. Find out where the riskiest bits are, and where the system behaves unusually. Chat. Discuss. Discover. Work out how, and when, you’re going to get feedback on the work you’re about to do.

Then, and only then, reach for the tools.

Liz Keogh

@sebrose h)p://cucumber.ioh)p://www.accepUc.com/blog/wp-content/uploads/2013/09/bdd.jpg

3. CollaboraAon IS essenAal

@sebrose h)p://cucumber.io

h)ps://cucumber.pro/blog/2014/03/03/the-worlds-most-misunderstood-collaboraUon-tool.html

When you do BDD/SpecificaUon by Example and Outside-in, regression tests fall out at the other end. They are a by-product of those acUviUes. TesUng isn't the acUvity itself.

Cucumber is first a foremost a collaboraUon tool that aims to bring a common understanding to soEware teams - across roles.

Aslak Hellesøy

@sebrose h)p://cucumber.io

Ideas

User Stories

Examples

Automated Tests

Code

@sebrose h)p://cucumber.ioh)p://www.developersurvivalguide.com/tag/agile/

@sebrose h)p://cucumber.io

4. Specflow is simple

Core concepts

Features

-Scenarios -Steps

-Gherkin

Glue code

-Step definiUons

-C# (& others)

ApplicaUon

@sebrose h)p://cucumber.io

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 And I start playing a game When I submit a correct easy answer Then my score is 10

Given I register a teamThen my score is 0

Given I register a teamAnd I start playing a gameWhen I submit a correct easy answerThen my score is 10

@sebrose h)p://cucumber.io

Given(/^I register a team$/) do pending end

Ruby

@sebrose h)p://cucumber.io

import cucumber.api.java.en.*;

public class HikerStepDef {

@Given("^I register a team$") public void iRegisterATeam()

throws Throwable { throw new PendingException(); }}

Java

@sebrose h)p://cucumber.io

using TechTalk.SpecFlow;

[Binding]public class GameSteps{ [Given(@"I register a team")] public void GivenIRegisterATeam() { ScenarioContext.Current.Pending(); }}

C#

@sebrose h)p://cucumber.io

[Then(@"my score is (.*)")]public void ThenMyScoreIs(int p0){ ScenarioContext.Current.Pending();}

@sebrose h)p://cucumber.io

[Then(@"my score is (.*)")]public void ThenMyScoreIs(int score){ ScenarioContext.Current.Pending();}

@sebrose h)p://cucumber.io

[Then(@"my score is (\d*)")]public void ThenMyScoreIs(int score){ ScenarioContext.Current.Pending();}

@sebrose h)p://cucumber.io

[Then(@"my score is (\d*)")]public void ThenMyScoreIs(int score){ Assert.AreEqual(score, game.Score);}

@sebrose h)p://cucumber.io

5. Cucumber and Specflow aren’t exactly the same

@sebrose h)p://cucumber.io

Given/When/Then namespaces

Global namespace - Given/When/Then interchangeable

Separate namespaces - Given/When/Then disUnct - And/But bind to preceding namespace - [StepDefiniUon] for compaUbility

@sebrose h)p://cucumber.io

Binding & test frameworks

Behind the scenes - may need to specify paths - select required plugin(s)

Some magic code generaUon - NUnit by default - configuraUon changes for others - several output opUons

@sebrose h)p://cucumber.io

Sharing data between steps

Varies by implementaUon - Ruby: World object - Java: Dependency InjecUon

Context object(s) - Injected - Scenario - Feature

@sebrose h)p://cucumber.io

Hooks

Before & AEer Ruby: Around, AEerStep

Before & AEer BeforeStep & AEerStep BeforeFeature & AEerFeature BeforeTestRun & AEerTestRun BeforeScenarioBlock & AEerScenarioBlock

@sebrose h)p://cucumber.io

DocumentaUon

Poor online documentaUon Books for Ruby & Java

Extensive online documentaUon Book in the pipeline

@sebrose h)p://cucumber.ioh)ps://cemarking.net/wp-content/uploads/2014/01/User-Manual_featured.jpg

6. Prefer living documentaAon

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

Other forms of documentaAon are also necessary!

@sebrose h)p://cucumber.io

7. Don’t forget the testers

@sebrose claysnow.co.uk

h)p://claysnow.co.uk/architectural-alignment-and-test-induced-design-damage-fallacy/

@sebrose h)p://cucumber.iohttp://1.bp.blogspot.com/_YzKCMr-tcMM/TFLIGeqTJfI/AAAAAAAAARQ/AhNW62KX5EA/s1600/cartoon6.jpg

8. BDD is not just for testers

Support code

Features

-Scenarios -Steps

-Gherkin

Glue code

-Step definiUons

-Java / C#

ApplicaUon

Support Code

Ubiquitous Language

@sebrose h)p://cucumber.io

9. The right amount of detail

@sebrose h)p://cucumber.io

“I get paid for code that works, not for tests, so my philosophy is to test as li8le 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/quesUons/153234/how-deep-are-your-unit-tests/153565#153565

@sebrose h)p://cucumber.io

ImperaUve DeclaraUve

Keep it Focussed: Avoid incidental

details

@sebrose h)p://cucumber.io

Imperative vs Declarative Style

Feature: Sign up Scenario: Create a new user account Given I am not logged in And I visit the homepage And I follow "Sign up" And I fill in "Username" with "Seb" And I fill in "Password" with "password" And I fill in "Confirm password" with "password" When I press "Sign up" Then a new account should be created for me

@sebrose h)p://cucumber.io

Imperative vs Declarative Style

Feature: Sign up Scenario: Create a new user account Given I am not logged in And I decide to sign up for an account And I provide an acceptable username And I provide an acceptable password And I confirm my password choice When I submit these details Then a new account should be created for me

@sebrose h)p://cucumber.io

Imperative vs Declarative Style

Feature: Sign up Scenario: New user redirected to their own page Given I am not logged in And I decide to sign up for an account And I provide an acceptable username And I provide an acceptable password And I confirm my password choice When I submit these details Then I should be on my feeds page And I should see "Hello, Seb"

@sebrose h)p://cucumber.io

Imperative vs Declarative Style

Feature: Sign up Scenario: New user redirected to their own page Given I am not logged in And I decide to sign up for an account And I provide an acceptable username And I provide an acceptable password And I confirm my password choice When I submit these details Then I should be on my feeds page And I should see a greeting message

@sebrose h)p://cucumber.io

Imperative vs Declarative Style

Feature: Sign up Scenario: New user redirected to their own page When I sign up for a new account

Then I should be taken to my feeds pageAnd I should see a greeting message

@sebrose h)p://cucumber.io

Imperative vs Declarative Style

Feature: The entire system This feature illustrates what can happen when you take the declarative style too far.

Scenario: It works When I use the system Then it should work perfectly

@sebrose h)p://cucumber.io

(A great blog post on this subject by Dan North)

h)p://dannorth.net/2011/01/31/whose-domain-is-it-anyway/

Whose domain is it anyway?

@sebrose h)p://cucumber.io

10. Deliberate pracAce

@sebrose h)p://cucumber.io

@sebrose h)p://cucumber.io

@sebrose h)p://cucumber.io

01 BDD demands collaboraUon

10 Examples facilitate feedback

102 things you should know

@sebrose h)p://cucumber.io

Seb Rose Twi6er: @sebrose

Blog: cucumber.io/blog

E-mail: [email protected]