Let's just test that real quick

Let's just test that real quick

Property-based testing changes the way you look at your test suites. Instead of specifying test cases one after another, this advanced testing technique enables you to use logic to automatically generate a stream of inputs. Despite its potential, property-based testing still remains under the radar within the Java community. Let's try to uncover its strengths together.

The talk will discuss how property-based testing compares to traditional testing methods and demonstrate its principles with simple examples. We will also see how it fits into a TDD workflow and a CD pipeline. In order to bust the myth of the inapplicability of property-based testing in a real-world setting we'll bring up some use cases from industry. Finally, we'll wander into the world of concurrency and the automation of race condition detection.

As seen at Berlin Expert Days 2017.

Ae7a42fb716793697b1d222f3cc753b8?s=128

Jan Stępień

August 29, 2017
Tweet

Transcript

  1. Let’s just test that Property-based testing
 in practice with Jan

    Stępień real quick
  2. JAN STĘPIEŃ Senior Consultant jan.stepien@innoq.com Functional Programming Property-Based Testing Architecture

    and Development servus!
  3. better at testing I want you to become

  4. None
  5. double stepsPerHour(int steps, double seconds) { return steps / (seconds

    * 60 * 60); }
  6. 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); }
  7. [(1, 2, 0.5), (2, 2, 1), (0, 3, 0)].stream().map((steps, secs,

    value) B> { assertThat(stepsPerHour(steps, secs), equalTo(value)); });
  8. ŏ ų = ŏ ų

  9. ŏ ų = ŏ ų ŏ ų · ų =

    ŏ ų · ų
  10. ŏ ų = ŏ ų ŏ ų · ų =

    ŏ ų · ų ŏ ų · ų = ŏ
  11. [(1, 2, 0.5), (2, 2, 1), (0, 3, 0)].stream().map((steps, secs,

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

    * secs * stepsPerHour(steps, secs), equalTo(steps)); });
  13. QuickCheck

  14. None
  15. junit-quickcheck github.com/pholser/junit-quickcheck

  16. @RunWith(JUnitQuickcheck.class) public class StepsPerSecondProperties { @Property void stepsProperty(int steps, double

    secs) { assertThat(3600 * secs * stepsPerHour(steps, secs), equalTo(steps)); } }
  17. QuickTheories github.com/ncredinburgh/QuickTheories

  18. 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))); } }
  19. ------------------------------------------------------- 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 <NaN> 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) ...
  20. 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))); } }
  21. 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))); } }
  22. ------------------------------------------------------- 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 <Infinity> 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) ...
  23. int[] reverse(int[] array) { ... }

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

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

    assertThat(reverse(reverse(array)), equalTo(array)); } }
  26. { 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" } ] } ] }
  27. GET /hosen/lee/farbe-hellblau/40-70-euro/seite-2

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

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

  30. GET /hosen/lee/farbe-hellblau/40-70-euro/seite-2 { category: "pants", brand: "lee", color: "light-blue", page:

    2, price: { from: 40, to: 70 } }
  31. State happens

  32. RedisCache accompanied by an oracle java.util.HashMap

  33. RedisCache accompanied by an oracle java.util.HashMap

  34. RedisCache accompanied by an oracle java.util.HashMap

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

  36. Testing the Hard Stuff
 and Staying Sane John Hughes

  37. [ α β γ δ ] [ 1 2 3

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

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

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

    4 ] [ α 1 β 2 γ 3 δ 4 ]
  41. Jepsen: On the perils
 of network partitions Kyle “Aphyr” Kingsbury

  42. better at testing I want you to become

  43. Let’s just test that with Jan Stępień @janstepien — jan@stepien.cc

    real quick