Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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.

Jan Stępień

August 29, 2017
Tweet

More Decks by Jan Stępień

Other Decks in Programming

Transcript

  1. [(1, 2, 0.5), (2, 2, 1), (0, 3, 0)].stream().map((steps, secs,

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

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

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

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

    secs) { assertThat(3600 * secs * stepsPerHour(steps, secs), equalTo(steps)); } }
  6. 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))); } }
  7. ------------------------------------------------------- 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) ...
  8. 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))); } }
  9. 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))); } }
  10. ------------------------------------------------------- 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) ...
  11. { 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" } ] } ] }
  12. [ α β γ δ ] [ 1 2 3

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

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

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

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