double loop

44
Double Loop TDD & BDD Done Right! @jessicamauerhan | #OpenWest | https://joind.in/talk/dd1c5

Upload: jessica-mauerhan

Post on 09-Jan-2017

234 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Double Loop

Double LoopTDD & BDD Done Right!

@jessicamauerhan | #OpenWest | https://joind.in/talk/dd1c5

Page 2: Double Loop

@jessicamauerhan

Senior Test Engineer

Grovo Learning, Inc.

[email protected]

jmauerhan.wordpress.com

Page 3: Double Loop

Double Loop: TDD & BDD

Page 4: Double Loop

@jessicamauerhan | #OpenWest | https://joind.in/talk/dd1c5

What is Test Driven Developmen

t?

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.

Page 5: Double Loop

@jessicamauerhan | #OpenWest | https://joind.in/talk/dd1c5

Benefits of Test Driven DevelopmentGuides 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 coveragePreventing and identifying

regressions and mistakes with automated tests

Executable documentation.

Primary Secondary

Page 6: Double Loop

@jessicamauerhan | #OpenWest | https://joind.in/talk/dd1c5

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 Documentationhttp://docs.behat.org/en/v2.5

Page 7: Double Loop

@jessicamauerhan | #OpenWest | https://joind.in/talk/dd1c5

Why Practice BDD?

Well Designed CodeAutomated TestingImplementationUI Testing

BDD Is Not About...CommunicationCollaborationDocumentationPreventing Regressions

BDD Is About...

Page 8: Double Loop

@jessicamauerhan | #OpenWest | https://joind.in/talk/dd1c5

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

Page 9: Double Loop

@jessicamauerhan | #OpenWest | https://joind.in/talk/dd1c5

AccuracyUnit Tests describe

accuracy of code and prescribe maintainable

code

SuitabilityBehavior Tests describe suitability of software's

features for the end user

Page 10: Double Loop

MaintenanceCoverageFragility DurationCost

Number of Tests

Unit Tests

Behavior Tests

System Tests(performance)

GUITests

Page 11: Double Loop

Add Behavior

Test

Add Unit Test

Run Unit Test

Add/EditSome Code

Refactor

Run All Unit Tests

Run All Behavior

Tests

Pass* Fail

FailPass

Run Behavior

Test Pass*Fail

Fail Pass

Revert Changes

@jessicamauerhan | #OpenWest | https://joind.in/talk/dd1c5

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.

Page 12: Double Loop

Add Behavior

Test

Add Unit Test

Run Unit Test

Add/EditSome Code

Refactor

Run All Unit Tests

Run All Behavior

Tests

Pass* Fail

FailPass

Run Behavior

Test Pass*Fail

Fail Pass

Revert Changes

@jessicamauerhan | #OpenWest | https://joind.in/talk/dd1c5

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.

Page 13: Double Loop

Add Behavior

Test

Page 14: Double Loop

@jessicamauerhan | #OpenWest | https://joind.in/talk/dd1c5

Parts of a Behavior

Test

Feature: GherkinTest CodeBDD Framework /

Test Runner

Page 15: Double Loop

@jessicamauerhan | #OpenWest | https://joind.in/talk/dd1c5

Gherkin

Business Readable, Domain Specific Language

Living Documentation / User Manual

User Story with NarrativeContains at least one

Scenario: Acceptance Criteria

Page 16: Double Loop

@jessicamauerhan | #OpenWest | https://joind.in/talk/dd1c5

NarrativeAs 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 fundsGiven the account has a balanceAnd the card is validAnd the machine contains at least the amount of my balanceWhen I request an amount less than or equal to my balanceThen the ATM should dispense that amountAnd the amount should be subtracted from my account balance And the card should be returned

Scenario: Account has insufficient fundsGiven the account has a balanceAnd the card is validAnd the machine contains at least the amount of my balanceWhen I request an amount greater than my balanceThen the ATM should not dispense any moneyAnd the ATM should say there are insufficient fundsAnd my account balance should not changeAnd 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

Page 17: Double Loop

@jessicamauerhan | #OpenWest | https://joind.in/talk/dd1c5

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 pageAnd I should see "Future Broadcast Author" on the pageAnd I should see "This broadcast begins at 6:00 pm EST" on the pageAnd I should see a countdown timer

Feature: View Countdown before Broadcast Process

Author describes Feature with implementation specific example

Developer adds fixture data

IssuesTest only works before 6pmWhat is the intent?Confusion for business users

Page 18: Double Loop

@jessicamauerhan | #OpenWest | https://joind.in/talk/dd1c5

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 futureWhen I view that broadcast’s pageThen I should see the broadcast titleAnd I should see the broadcast author’s nameAnd I should see "This broadcast begins at" followed by the start time in ESTAnd 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 implementationWhy?Overall understandingCommunication valueHelp identify poorly written

code

Page 19: Double Loop

@jessicamauerhan | #OpenWest | https://joind.in/talk/dd1c5

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 CatalogGiven I view the catalogWhen I select a state from the list of statesThen I should see the list of products for sale

Scenario: Display Local Services in Product CatalogGiven the company has a regional office in a stateWhen I view the catalogAnd I select that state from the list of statesThen 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 stateWhen I view the catalogAnd I select that state from the list of statesThen I should not see a list of services offered

Page 20: Double Loop

@jessicamauerhan | #OpenWest | https://joind.in/talk/dd1c5

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 CatalogGiven I view the catalogWhen I select a state from the list of statesThen I should see the list of products for sale

Scenario: Display Local Services in Product CatalogGiven the company has a regional office in a stateWhen I view the catalogAnd I select that state from the list of statesThen 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 stateWhen I view the catalogAnd I select that state from the list of statesThen 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>'; }}

Page 21: Double Loop

@jessicamauerhan | #OpenWest | https://joind.in/talk/dd1c5

Writing Great FeaturesExact 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)

Page 22: Double Loop

@jessicamauerhan | #OpenWest | https://joind.in/talk/dd1c5

Parts of a Behavior

Test

Feature: GherkinTest CodeBDD Framework /

Test Runner

Page 23: Double Loop

@jessicamauerhan | #OpenWest | https://joind.in/talk/dd1c5

BDD Frameworks / Test RunnersJava - JBehave, Cucumber and moreRuby - CucumberPHP - BehatJavaScript - cucumber.js, jasmine...

Page 24: Double Loop

@jessicamauerhan | #OpenWest | https://joind.in/talk/dd1c5

Scenario: Customer views Product CatalogGiven 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);}

Page 25: Double Loop

Add Behavior

Test

Run Behavior

Test Pass*

@jessicamauerhan | #OpenWest | https://joind.in/talk/dd1c5

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.

Page 26: Double Loop

@jessicamauerhan | #OpenWest | https://joind.in/talk/dd1c5

Parts of a Behavior

Test

Feature: GerkhinTest CodeBDD Framework /

Test Runner

Page 27: Double Loop

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.

Page 28: Double Loop

@jessicamauerhan | #OpenWest | https://joind.in/talk/dd1c5

Writing Unit Tests

What is a unit?

Unit: As small as possibleDesign and Describe behavior

Setup scenario for behaviorCause behavior to occurAssert result is as expected

Verbose and Descriptive Names

Page 29: Double Loop

Add Behavior

Test

Add Unit Test

Run Unit Test

Run Behavior

Test Pass*Fail

@jessicamauerhan | #OpenWest | https://joind.in/talk/dd1c5

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.

Page 30: Double Loop

@jessicamauerhan | #OpenWest | https://joind.in/talk/dd1c5

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

Page 31: Double Loop

Add Behavior

Test

Add Unit Test

Run Unit Test

Add/EditSome Code

Fail

Run Behavior

Test Pass*Fail

@jessicamauerhan | #OpenWest | https://joind.in/talk/dd1c5

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.

Page 32: Double Loop

@jessicamauerhan | #OpenWest | https://joind.in/talk/dd1c5

Writing CodeJust Enough

Add just enough code to pass the test

Avoid over abstracting or adding untested logic

Page 33: Double Loop

@jessicamauerhan | #OpenWest | https://joind.in/talk/dd1c5

Three Types of Unit Tests

CorrectnessContractCollaboration

Page 34: Double Loop

New Class For User

Registration

Class:Registration

ServiceUnit Tests

Registration ServiceCorrectness

Third Party Service API

(Mailer)

Integrated Test

MailChimp API

Forgot Password

Weekly Newsletter

Direct Message

Exciting Event

Reminder!

@jessicamauerhan | #OpenWest | https://joind.in/talk/dd1c5

TDD: Integration Testing with Contract & Collaboration Tests

Page 35: Double Loop

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

ServiceUnit Tests

Registration ServiceCorrectness

Collaboration Tests

Collaboration Tests

@jessicamauerhan | #OpenWest | https://joind.in/talk/dd1c5

TDD: Integration Testing with Contract & Collaboration Tests

Page 36: Double Loop

Mailer Implementation

Class: Mailchimp 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

ServiceUnit Tests

Registration ServiceCorrectness

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 | #OpenWest |

https://joind.in/talk/dd1c5

TDD: Integration Testing with Contract & Collaboration Tests

Page 37: Double Loop

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

ServiceUnit Tests

Registration ServiceCorrectness

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 | #OpenWest |

https://joind.in/talk/dd1c5

TDD: Integration Testing with Contract & Collaboration Tests

Forgot Password

Weekly Newsletter

Direct Message

Exciting Event

Reminder!

Page 38: Double Loop

Add Behavior

Test

Add Unit Test

Run Unit Test

Add/EditSome Code

Fail

Run Behavior

Test Pass*Fail

@jessicamauerhan | #OpenWest | https://joind.in/talk/dd1c5

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.

Page 39: Double Loop

@jessicamauerhan | #OpenWest | https://joind.in/talk/dd1c5

Failing Test or

Test Suite?Revert!

If the test fails, revert your work

Commit small changes, often!

Page 40: Double Loop

Add Behavior

Test

Add Unit Test

Run Unit Test

Add/EditSome Code

Refactor

Run All Unit Tests

Pass* Fail

FailPass

Run Behavior

Test Pass*Fail

Revert Changes

@jessicamauerhan | #OpenWest | https://joind.in/talk/dd1c5

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.

Page 41: Double Loop

@jessicamauerhan | #OpenWest | https://joind.in/talk/dd1c5

RefactoringThe process of restructuring code without changing its behavior

Page 42: Double Loop

@jessicamauerhan | #OpenWest | https://joind.in/talk/dd1c5

RefactoringImproving non-

functional aspectsRemoving duplicationRenaming SimplifyingUnit Tests Always

Passing!

Changes that would break a unit test

New functionality that is not tested

New Unit Tests Fail, Then Pass

Adding Functionality

Page 43: Double Loop

Add Behavior

Test

Add Unit Test

Run Unit Test

Add/EditSome Code

Refactor

Run All Unit Tests

Run All Behavior

Tests

Pass* Fail

FailPass

Run Behavior

Test Pass*Fail

Fail Pass

Revert Changes

@jessicamauerhan | #OpenWest | https://joind.in/talk/dd1c5

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.

Page 44: Double Loop

Thank You!Double Loop: TDD & BDD Done

Right!

@jessicamauerhan | #OpenWest| https://joind.in/talk/dd1c5