Slide 1

Slide 1 text

Acceptance Testing! with Cucumber

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

Given a Fibonacci Sequence When I query the sequence at index 5 Then the number I get is 8

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

Tests from the User’s Point of View ACCEPTANCE TESTS

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

discuss distill develop review / demo

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

What are the discounts? How do multiple possible discounts apply? What is a series?

Slide 20

Slide 20 text

Discount Rules I II III IV V Formula $ 1 1 * 8 8.00 1 1 2 * 8 * 0.95 15.20 1 1 1 3 * 8 * 0.9 21.60 1 1 1 1 4 * 8 * 0.8 25.60 1 1 1 1 1 5 * 8 * 0.75 30.00 2 2 * 8 16.00 2 1 2 * 8 * 0.95 + 1 * 8 23.20 2 2 2 1 1 4 * 8 * 0.8 + 4 * 8 * 0.8 51.20

Slide 21

Slide 21 text

Discount Rules I II III IV V Formula $ 1 1 * 8 8.00 1 1 2 * 8 * 0.95 15.20 1 1 1 3 * 8 * 0.9 21.60 1 1 1 1 4 * 8 * 0.8 25.60 1 1 1 1 1 5 * 8 * 0.75 30.00 2 2 * 8 16.00 2 1 2 * 8 * 0.95 + 1 * 8 23.20 2 2 2 1 1 4 * 8 * 0.8 + 4 * 8 * 0.8 51.20 Optimal Customer Discount

Slide 22

Slide 22 text

Scenario: buy one book from the series
 
 When I buy 1 copy of “Harry Potter I”
 Then I must pay $8 Scenario: buy two books from the series
 
 When I buy 1 copy of “Harry Potter I” And I buy 1 copy of “Harry Potter II” Then I must pay $15.20

Slide 23

Slide 23 text

Scenario Outline: buy Harry Potter books
 When I buy copies of “Harry Potter I”
 And I buy copies of “Harry Potter II”
 And I buy copies of “Harry Potter III”
 And I buy copies of “Harry Potter IV”
 And I buy copies of “Harry Potter V”
 Then I must pay $ Examples:
 | I | II | III | IV | V | Total | | 1 | 0 | 0 | 0 | 0 | 8.00 | | 1 | 1 | 0 | 0 | 0 | 15.20 | # .. | 2 | 1 | 0 | 0 | 0 | 23.20 | | 2 | 2 | 2 | 1 | 1 | 51.20 |

Slide 24

Slide 24 text

UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU ! 9 Scenarios (9 undefined) 54 Steps (54 undefined) 0m0.000s ! ! You can implement missing steps with the snippets below: ! @When("^I buy (\\d+) copies of \"([^\"]*)\"$") public void I_buy_copies_of(int arg1, String arg2) throws Throwable { // Express the Regexp above with the code you wish you had throw new PendingException(); } ! @Then("^I must pay \\$(\\d+).(\\d+)$") public void I_must_pay_$_(int arg1, int arg2) throws Throwable { // Express the Regexp above with the code you wish you had throw new PendingException(); }

Slide 25

Slide 25 text

UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU ! 9 Scenarios (9 undefined) 54 Steps (54 undefined) 0m0.000s ! ! You can implement missing steps with the snippets below: ! @When(“^I buy (\\d+) copies of \"([^\"]*)\"$") public void I_buy_copies_of(int arg1, String arg2) throws Throwable { // Express the Regexp above with the code you wish you had throw new PendingException(); } ! @Then("^I must pay \\$(\\d+).(\\d+)$") public void I_must_pay_$_(int arg1, int arg2) throws Throwable { // Express the Regexp above with the code you wish you had throw new PendingException(); }

Slide 26

Slide 26 text

P-----P-----P-----P-----P-----P-----P-----P-----P----- ! 9 Scenarios (9 pending) 54 Steps (45 skipped, 9 pending) 0m0.336s ! cucumber.api.PendingException: TODO: implement me at com.github.bjpbakker.potter.cucumber.DiscountStepdefs.I_buy_copies_of(Discou ntStepdefs.java:11) at ✽.When I buy 1 copies of "Harry Potter I"(features/discount.feature: 16)

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

public class DiscountStepdefs { private List cart = new LinkedList<>(); ! @When("^I buy (\\d+) copies of \”([^\”]*)\"$") public void I_buy_copies_of(int numberOfCopies, String title) { Book book = bookByTitle(title) .orElseThrow(() -> new UnknownTitle(title)); range(0, numberOfCopies) .forEach(n -> this.cart.add(book)); } ! // .. } ! class Book { // .. }

Slide 29

Slide 29 text

.....P.....P.....P.....P.....P.....P.....P.....P.....P ! 9 Scenarios (9 pending) 54 Steps (9 pending, 45 passed) 0m0.234s

Slide 30

Slide 30 text

public class DiscountStepdefs { private List cart = new LinkedList<>(); ! @When("^I buy (\\d+) copies of \”([^\”]*)\"$") public void I_buy_copies_of(int numberOfCopies, String title) { Book book = bookByTitle(title) .orElseThrow(() -> new UnknownTitle(title)); range(0, numberOfCopies) .forEach(n -> this.cart.add(book)); } ! @Then("^I must pay \\$(\\d+.?\\d*)$") public void I_must_pay_$_(float amount) { Long expected = Float.valueOf(amount * 100).longValue(); ShoppingCart shoppingCart = new ShoppingCart(this.cart); Long total = shoppingCart.calculateTotal(); assertThat(total, is(expected)); } ! // .. } ! class Book { .. } ! class ShoppingCart { public ShoppingCart(List cart) {} public Long calculateTotal() { return -1L; } }

Slide 31

Slide 31 text

.....F.....F.....F.....F.....F.....F.....F.....F.....F ! 9 Scenarios (9 failed) 54 Steps (9 failed, 45 passed) 0m0.247s ! java.lang.AssertionError: Expected: is <800L> but: was <-1L> at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20) at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8) at com.github.bjpbakker.potter.cucumber.DiscountStepdefs.I_must_pay_ $_(DiscountStepdefs.java:30) at ✽.Then I must pay $8.00(features/discount.feature:21) ! java.lang.AssertionError: Expected: is <1520L> but: was <-1L> at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20) at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8) at com.github.bjpbakker.potter.cucumber.DiscountStepdefs.I_must_pay_ $_(DiscountStepdefs.java:30) at ✽.Then I must pay $15.20(features/discount.feature:21) ! java.lang.AssertionError: Expected: is <2160L> but: was <-1L>

Slide 32

Slide 32 text

public class ShoppingCart { public Long calculateTotal() { return total(new Series(this.cart)); } ! private Long total(Series series) { if (series.isEmpty()) { return 0L; } else { Set books = takeOptimalDiscount(series); Long subtotal = subtotal(books, discount(books.size())); return subtotal + total(series.drop(books)); } } ! // .. } ! public class Series { .. } public class Book { .. }

Slide 33

Slide 33 text

........................................................... ! 11 Scenarios (11 passed) 59 Steps (59 passed) 0m0.299s

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

Scenario Outline: buy Harry Potter books
 When I buy copies of “Harry Potter I”
 And I buy copies of “Harry Potter II”
 And I buy copies of “Harry Potter III”
 And I buy copies of “Harry Potter IV”
 And I buy copies of “Harry Potter V”
 Then I must pay $ Examples:
 | I | II | III | IV | V | Total | | 1 | 0 | 0 | 0 | 0 | 8.00 | | 1 | 1 | 0 | 0 | 0 | 15.20 | # .. | 2 | 1 | 0 | 0 | 0 | 23.20 | | 2 | 2 | 2 | 1 | 1 | 51.20 |

Slide 36

Slide 36 text

github.com/bjpbakker/harry-potter-kata-atdd WORKING EXAMPLE

Slide 37

Slide 37 text

No content