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. 4.
  2. 7.

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

    value) B> { assertThat(stepsPerHour(steps, secs), equalTo(value)); });
  3. 10.

    ŏ ų = ŏ ų ŏ ų · ų =

    ŏ ų · ų ŏ ų · ų = ŏ
  4. 11.

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

    value) B> { assertThat(stepsPerHour(steps, secs), equalTo(value)); });
  5. 12.

    [(1, 2), (2, 2), (0, 3)].stream().map((steps, secs) B> { assertThat(3600

    * secs * stepsPerHour(steps, secs), equalTo(steps)); });
  6. 14.
  7. 16.

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

    secs) { assertThat(3600 * secs * stepsPerHour(steps, secs), equalTo(steps)); } }
  8. 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))); } }
  9. 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) ...
  10. 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))); } }
  11. 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))); } }
  12. 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) ...
  13. 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" } ] } ] }
  14. 37.

    [ α β γ δ ] [ 1 2 3

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

    [ α β γ δ ] [ 1 2 3

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

    [ α β γ δ ] [ 1 2 3

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

    [ α β γ δ ] [ 1 2 3

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