living with acceptance tests: beyond write-once

63
Living with Acceptance Tests: Beyond Write-Once Daniel Wellman Twitter: @wellman E-mail: [email protected] www.intentmedia.com

Upload: daniel-wellman

Post on 27-Jun-2015

393 views

Category:

Technology


0 download

DESCRIPTION

Presented at CukeUp! NYC 2014 in Brooklyn, NY. Session description: Acceptance tests have become a common part of building software for an agile process. However, I've observed a trend of growing build times - with teams saying "Our build is 15 minutes... well, actually it's 45 minutes because our Cucumbers took so long, so we parallelized the cukes." One reason for this may be that teams treat their acceptance tests as "Write-Once": create for a new story, get them to pass, and leave them be. In this talk we'll explore the technical and social forces that cause an acceptance test suite to grow, and discuss some ways to refactor and improve these tests. We'll touch on hexagonal architecture, declarative vs. imperative style, and alternate Cucumber step implementations.

TRANSCRIPT

Page 1: Living with Acceptance Tests: Beyond Write-Once

Living with!Acceptance Tests: Beyond Write-Once

Daniel Wellman

Twitter: @wellman!E-mail: [email protected]

www.intentmedia.com

Page 2: Living with Acceptance Tests: Beyond Write-Once
Page 3: Living with Acceptance Tests: Beyond Write-Once
Page 4: Living with Acceptance Tests: Beyond Write-Once
Page 5: Living with Acceptance Tests: Beyond Write-Once
Page 6: Living with Acceptance Tests: Beyond Write-Once

Activity

Page 7: Living with Acceptance Tests: Beyond Write-Once

Goal!Build the least helpful,

most frustrating acceptance test suite

Page 8: Living with Acceptance Tests: Beyond Write-Once
Page 9: Living with Acceptance Tests: Beyond Write-Once

Revise Your Tests With the Same Care as Your Production Code

Page 10: Living with Acceptance Tests: Beyond Write-Once
Page 11: Living with Acceptance Tests: Beyond Write-Once

Check for Duplication

Page 12: Living with Acceptance Tests: Beyond Write-Once

Refactor with New Insights

Page 13: Living with Acceptance Tests: Beyond Write-Once

Monitor for Problems

Page 14: Living with Acceptance Tests: Beyond Write-Once

Problem:!Slow and Flaky Tests

Page 15: Living with Acceptance Tests: Beyond Write-Once

Server

Database QueueTest

End-to-End Tests

Page 16: Living with Acceptance Tests: Beyond Write-Once

New Project

End-to-End!Acceptance Test

Page 17: Living with Acceptance Tests: Beyond Write-Once

New Project

End-to-End!Acceptance Test

End-to-End!Acceptance Test

End-to-End!Acceptance Test

End-to-End!Acceptance Test

End-to-End!Acceptance Test

End-to-End!Acceptance Test

End-to-End!Acceptance Test

End-to-End!Acceptance Test

End-to-End!Acceptance Test

End-to-End!Acceptance Test

End-to-End!Acceptance Test

End-to-End!Acceptance Test

End-to-End!Acceptance Test

End-to-End!Acceptance Test

End-to-End!Acceptance Test

End-to-End!Acceptance Test

End-to-End!Acceptance Test

End-to-End!Acceptance Test

End-to-End!Acceptance Test

Page 18: Living with Acceptance Tests: Beyond Write-Once

How did it get this way?

Page 19: Living with Acceptance Tests: Beyond Write-Once

Run Acceptance Tests in Parallel

Page 20: Living with Acceptance Tests: Beyond Write-Once

Faster Build Same Risk

Page 21: Living with Acceptance Tests: Beyond Write-Once

Failures are Hard to Diagnose

Page 22: Living with Acceptance Tests: Beyond Write-Once

Problem:Flaky Tests

Page 23: Living with Acceptance Tests: Beyond Write-Once

Don’t make every acceptance test run

end-to-end

Page 24: Living with Acceptance Tests: Beyond Write-Once

Ports and Adapters!a.k.a. Hexagonal Architecture

Page 25: Living with Acceptance Tests: Beyond Write-Once

Example: Fraud Protection for an Amazon-like store

Page 26: Living with Acceptance Tests: Beyond Write-Once

Example: Fraud Protection for an Amazon-like store

Example from Gojko Adzic, “Specification by Example”

Page 27: Living with Acceptance Tests: Beyond Write-Once

Example: Fraud Protection for an Amazon-like store

Example from Gojko Adzic, “Specification by Example”

Given a user with no previous transaction history, And the user’s account registration country is the UK, When the user places an order with delivery country U.S., Then the transaction is marked as suspicious, But the user sees order status as “Pending.”

Page 28: Living with Acceptance Tests: Beyond Write-Once

AdaptersPorts

Domain

Page 29: Living with Acceptance Tests: Beyond Write-Once

AdaptersPorts

Domain

GUI

DB

Page 30: Living with Acceptance Tests: Beyond Write-Once

AdaptersPorts

Domain

GUI

DB

Memory

Page 31: Living with Acceptance Tests: Beyond Write-Once

AdaptersPorts

Domain

GUI

DB

Memory

End-to-End Test

Page 32: Living with Acceptance Tests: Beyond Write-Once

AdaptersPorts

Domain

GUI

DB

Memory

End-to-End Test

Test Using!Domain

Page 33: Living with Acceptance Tests: Beyond Write-Once
Page 34: Living with Acceptance Tests: Beyond Write-Once

Before: End-to-EndWhen(/^the user places an order with delivery country (.*),$/) do |country_name| !!!!!!!!!!end

Page 35: Living with Acceptance Tests: Beyond Write-Once

Before: End-to-EndWhen(/^the user places an order with delivery country (.*),$/) do |country_name| !!!!!!!!!!end

# ... fill_in 'Address', with: ... select country_name, from: 'Country Selector’ click_button 'Ship To This Address' expect(page).to have_content 'Your order has been placed’ # ...

Page 36: Living with Acceptance Tests: Beyond Write-Once

After: Using Domain ModelWhen(/^the user places an order with delivery country (.*),$/) do |country_name|

!!!!!!!!!end

Page 37: Living with Acceptance Tests: Beyond Write-Once

After: Using Domain ModelWhen(/^the user places an order with delivery country (.*),$/) do |country_name|

!!!!!!!!!end

# ... country = Country.new(country_name) order = Order.new( ..., ship_to : Address.new( street: ..., country: country)) sales_clerk.accept(order) # ...

Page 38: Living with Acceptance Tests: Beyond Write-Once

Service

UI

Unit

The Testing Pyramid

Page 39: Living with Acceptance Tests: Beyond Write-Once

Service

UI

Unit

The Testing Pyramid

Page 40: Living with Acceptance Tests: Beyond Write-Once

Create a Separate System Test Suite

Page 41: Living with Acceptance Tests: Beyond Write-Once

Alternate Step Implementations

Page 42: Living with Acceptance Tests: Beyond Write-Once

Alternate Steps: Use Conditionals

use_browser = ENV['USE_BROWSER_FOR_TESTS']

Page 43: Living with Acceptance Tests: Beyond Write-Once

Alternate Steps: Use Conditionals

!!When(/^the user places an order with delivery country (.*),$/) do |country_name| if use_browser # drive a browser using Capybara; slower else # use domain objects; faster end end

use_browser = ENV['USE_BROWSER_FOR_TESTS']

Page 44: Living with Acceptance Tests: Beyond Write-Once
Page 45: Living with Acceptance Tests: Beyond Write-Once
Page 46: Living with Acceptance Tests: Beyond Write-Once

Write Tests Without Specifying

Implementation Details

Page 47: Living with Acceptance Tests: Beyond Write-Once

Imperative!vs.!

Declarative!Style

Page 48: Living with Acceptance Tests: Beyond Write-Once

Imperative Style

Page 49: Living with Acceptance Tests: Beyond Write-Once

Imperative Style

Given I am on the registration page, And I enter "UK" as my billing country, And I press the "Register" button, And I add an item to my cart, When I checkout And I enter "U.S." as my shipping country, And ...

Page 50: Living with Acceptance Tests: Beyond Write-Once

Declarative Style

Page 51: Living with Acceptance Tests: Beyond Write-Once

Declarative Style

Given a user with no previous transaction history, And the user’s account registration country is the UK, When the user places an order with delivery country U.S., Then the transaction is marked as suspicious, But the user sees order status as “Pending.”

Example by Gojko Adzic, “Specification by Example”

Page 52: Living with Acceptance Tests: Beyond Write-Once

Declarative Style

Given a user with no previous transaction history, And the user’s account registration country is the UK, When the user places an order with delivery country U.S., Then the transaction is marked as suspicious, But the user sees order status as “Pending.”

Example by Gojko Adzic, “Specification by Example”

You can’t tell how these are implemented!

Page 53: Living with Acceptance Tests: Beyond Write-Once

Use Declarative Style to Help Keep

Implementation Details Out

of Tests

Page 54: Living with Acceptance Tests: Beyond Write-Once

Problem: I’m not sure what is actually

tested…

Page 55: Living with Acceptance Tests: Beyond Write-Once

How did it get this way?

Page 56: Living with Acceptance Tests: Beyond Write-Once

Periodically Evaluate Your Test Suite

Page 57: Living with Acceptance Tests: Beyond Write-Once

Have you introduced a new domain concept?

Page 58: Living with Acceptance Tests: Beyond Write-Once

Are you in a new stage of your product’s life?

Page 59: Living with Acceptance Tests: Beyond Write-Once
Page 60: Living with Acceptance Tests: Beyond Write-Once

Suggestions• Maintain your acceptance tests just like

production code

• Write tests which describe what, not how, so their implementations can easily evolve later

• Decide when you can start moving acceptance tests from end-to-end to using the domain directly

• Re-evaluate your product’s tests and testing strategy as your system grows

Page 61: Living with Acceptance Tests: Beyond Write-Once

Resources

Page 62: Living with Acceptance Tests: Beyond Write-Once

Photo Credits“HighLine_061409_2779” by Jessica Sheridan is licensed under CC BY 2.0

“Highline New York” by Rebecca Krebs is licensed under CC BY 2.0

“NYC: The Highline" by Wally Gobetz is licensed under CC BY-NC-ND 2.0

“Rot”, “Yellow Cart”, and “Many Hands Make Light Work” by Oliver Rich are licensed under CC BY-NC-ND 2.0

“highline-1010384.jpg” by keroism is licensed under CC BY-SA 2.0

“highline” by Elijah Porter is licensed under CC BY-NC-SA 2.0

“Highline_030” by Timothy Vogel is licensed under CC BY-NC 2.0

“201106073451_DSC_0014_1.JPG” by Leonel Ponce is licensed under CC BY-NC 2.0

“193” by verndogs is licensed under CC BY-NC 2.0

Page 63: Living with Acceptance Tests: Beyond Write-Once

Living with!Acceptance Tests: Beyond Write-Once

Daniel Wellman

Twitter: @wellman!E-mail: [email protected]

www.intentmedia.com