gen-test



gen-test

0 0


gen-test


On Github SZoerner / gen-test

Don't write Tests!

Generate them.

An introduction to property based testing.

Agenda

Example based Testing Property based Testing Properties (or Invariants) Generators Shrinking Quickcheck (Haskell)

JUnit Theories

The Problem

The Solution (applause) Future Plans

Example based Testing

Testing division

public class MyTest {

  @Test
  public void testDivision() {
    assertEquals("Integer division", 5, 15 / 3);
    assertEquals("Double division", 0.5, 1.0 / 2);
  }

  @Test(expected = ArithmeticException.class)  
  public void testDivisionException() {
    // should fail
    final int i = 1 / 0;
  }

}

JUnit Theories

@RunWith(Theories.class)
public class MyTheoryTest {

    @DataPoints
    public static int[] data = { 1, 2, 3, 4, 5, 6 };

    @Theory
    public void divisionIsTheReversionOfMultiplication(int a, int b) {
        assertEquals(a, a * b / b);
    }

}

Generators

@RunWith(Theories.class)
public class MyGeneratorTest {

    @Theory
    public void divisionIsTheReversionOfMultiplication(
            @ForAll int a,
            @ForAll @InRange(min = "1") int b) {

        assertEquals(a, a * (b / b));
    }

    @Theory
    public void concatenationLength(
            @ForAll String s1, 
            @ForAll String s2) {

        assertEquals(s1.length() + s2.length(), (s1 + s2).length());
    }
}

Math to the rescue

\begin{align} \frac{a}{b} &= \frac{a}{b}\tag{1} \\ \\ a &= \frac{a}{b} \cdot b\tag{2} \end{align}

Property based Testing

  • Link to John Hughes' paper

Properties (or Invariants)

  • Identity Test / Roundtrip
  • Idempotency
  • Reference Implementation

Generators

Shrinking

Libraries

JUnit Theories

Thank you

New ideas pass through three periods:1) It can’t be done.2) It probably can be done, but it’s not worth doing.3) I knew it was a good idea all along!

-- Arthur C. Clarke

Resources

1
Don't write Tests! Generate them. An introduction to property based testing.