Slide 1

Slide 1 text

Let’s just test that Property-based testing
 in practice with Jan Stępień real quick

Slide 2

Slide 2 text

JAN STĘPIEŃ Senior Consultant [email protected] Functional Programming Property-Based Testing Architecture and Development servus!

Slide 3

Slide 3 text

better at testing I want you to become

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

double stepsPerHour(int steps, double seconds) { return steps / (seconds * 60 * 60); }

Slide 6

Slide 6 text

assertThat(stepsPerHour(1, 2), equalTo(0.5)); assertThat(stepsPerHour(2, 2), equalTo(1)); assertThat(stepsPerHour(0, 3), equalTo(0)); @Test(expected = ArithmeticException.class) public void testZeroSeconds() { stepsPerHour(1, 0.0); }

Slide 7

Slide 7 text

[(1, 2, 0.5), (2, 2, 1), (0, 3, 0)].stream().map((steps, secs, value) B> { assertThat(stepsPerHour(steps, secs), equalTo(value)); });

Slide 8

Slide 8 text

ŏ ų = ŏ ų

Slide 9

Slide 9 text

ŏ ų = ŏ ų ŏ ų · ų = ŏ ų · ų

Slide 10

Slide 10 text

ŏ ų = ŏ ų ŏ ų · ų = ŏ ų · ų ŏ ų · ų = ŏ

Slide 11

Slide 11 text

[(1, 2, 0.5), (2, 2, 1), (0, 3, 0)].stream().map((steps, secs, value) B> { assertThat(stepsPerHour(steps, secs), equalTo(value)); });

Slide 12

Slide 12 text

[(1, 2), (2, 2), (0, 3)].stream().map((steps, secs) B> { assertThat(3600 * secs * stepsPerHour(steps, secs), equalTo(steps)); });

Slide 13

Slide 13 text

QuickCheck

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

junit-quickcheck github.com/pholser/junit-quickcheck

Slide 16

Slide 16 text

@RunWith(JUnitQuickcheck.class) public class StepsPerSecondProperties { @Property void stepsProperty(int steps, double secs) { assertThat(3600 * secs * stepsPerHour(steps, secs), equalTo(steps)); } }

Slide 17

Slide 17 text

QuickTheories github.com/ncredinburgh/QuickTheories

Slide 18

Slide 18 text

public class StepsTest { @Test public void stepsProp() { qt() .forAll(integers().allPositive(), doubles().any()) .checkAssert((steps, secs) B> assertThat( 3600 * secs * stepsPerHour(steps, secs), equalTo(steps))); } }

Slide 19

Slide 19 text

------------------------------------------------------- T E S T S ------------------------------------------------------- Running cc.stepien.qc.StepsTest Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.541 sec <<< FAILURE! stepsProp(cc.stepien.qc.StepsTest) Time elapsed: 0.426 sec <<< FAILURE! java.lang.AssertionError: Property falsified after 1 example(s) Smallest found falsifying value(s) S- {1, 0.0} Cause was S- java.lang.AssertionError: Expected: <1> but: was at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:18) at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:6) at cc.stepien.qc.StepsTest.lambda$stepsProp$0(StepsTest.java:16) ...

Slide 20

Slide 20 text

public class StepsTest { @Test public void stepsProp() { qt() .forAll(integers().allPositive(), doubles().any()) .checkAssert((steps, secs) B> assertThat( 3600 * secs * stepsPerHour(steps, secs), equalTo(steps))); } }

Slide 21

Slide 21 text

public class StepsTest { @Test public void stepsProp() { qt() .forAll(integers().allPositive(), doubles().any()) .assuming((steps, secs) B> secs != 0) .checkAssert((steps, secs) B> assertThat( 3600 * secs * stepsPerHour(steps, secs), equalTo(steps))); } }

Slide 22

Slide 22 text

------------------------------------------------------- T E S T S ------------------------------------------------------- Running cc.stepien.qc.StepsTest Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.62 sec <<< FAILURE! stepsProp(cc.stepien.qc.StepsTest) Time elapsed: 0.521 sec <<< FAILURE! java.lang.AssertionError: Property falsified after 1 example(s) Smallest found falsifying value(s) S- {1, 4.9E-324} Cause was S- java.lang.AssertionError: Expected: <1> but: was at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:18) at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:6) at cc.stepien.qc.StepsTest.lambda$stepsProp$1(StepsTest.java:17) ...

Slide 23

Slide 23 text

int[] reverse(int[] array) { ... }

Slide 24

Slide 24 text

@RunWith(JUnitQuickcheck.class) public class ReverseProperties { @Property void reverseProperty(int[] array) { ... } }

Slide 25

Slide 25 text

@RunWith(JUnitQuickcheck.class) public class ReverseProperties { @Property void reverseProperty(int[] array) { assertThat(reverse(reverse(array)), equalTo(array)); } }

Slide 26

Slide 26 text

{ name: "Orson Welles", cinematography: [ { title: "Citizen Kane”, year: 1941, director: { name: "Orson Welles" }, screenwriter: { name: "Orson Welles" }, starring: [ { name: "Orson Welles" }, { name: "Joseph Cotten" }, { name: "Dorothy Comingore" } ] } ] }

Slide 27

Slide 27 text

GET /hosen/lee/farbe-hellblau/40-70-euro/seite-2

Slide 28

Slide 28 text

perty-based testing
 ctice with Jan Stępień al quick wat

Slide 29

Slide 29 text

GET /hosen/lee/farbe-hellblau/40-70-euro/seite-2

Slide 30

Slide 30 text

GET /hosen/lee/farbe-hellblau/40-70-euro/seite-2 { category: "pants", brand: "lee", color: "light-blue", page: 2, price: { from: 40, to: 70 } }

Slide 31

Slide 31 text

State happens

Slide 32

Slide 32 text

RedisCache accompanied by an oracle java.util.HashMap

Slide 33

Slide 33 text

RedisCache accompanied by an oracle java.util.HashMap

Slide 34

Slide 34 text

RedisCache accompanied by an oracle java.util.HashMap

Slide 35

Slide 35 text

[“put”, “foo”, “bar”] [“clear”] [“put”, “foo”, “123”] [“get”, “foo”] [“count”]

Slide 36

Slide 36 text

Testing the Hard Stuff
 and Staying Sane John Hughes

Slide 37

Slide 37 text

[ α β γ δ ] [ 1 2 3 4 ] [ α β γ δ 1 2 3 4 ]

Slide 38

Slide 38 text

[ α β γ δ ] [ 1 2 3 4 ] [ α β γ δ 1 2 3 4 ]

Slide 39

Slide 39 text

[ α β γ δ ] [ 1 2 3 4 ] [ α β 1 2 3 4 γ δ ]

Slide 40

Slide 40 text

[ α β γ δ ] [ 1 2 3 4 ] [ α 1 β 2 γ 3 δ 4 ]

Slide 41

Slide 41 text

Jepsen: On the perils
 of network partitions Kyle “Aphyr” Kingsbury

Slide 42

Slide 42 text

better at testing I want you to become

Slide 43

Slide 43 text

Let’s just test that with Jan Stępień @janstepien — [email protected] real quick