Kill the Mutants - a better way to test your tests by Roy van Rijn

20
KILL THE MUTANTS a better way to test your tests

description

Devoxx Ignite 2014 session

Transcript of Kill the Mutants - a better way to test your tests by Roy van Rijn

Page 1: Kill the Mutants - a better way to test your tests by Roy van Rijn

KILL THE MUTANTSa better way to test your tests

Page 2: Kill the Mutants - a better way to test your tests by Roy van Rijn

SHOW OF HANDSlet's do a

• Unit testing

• Continuous integration

• Code coverage

• Mutation testing

Page 3: Kill the Mutants - a better way to test your tests by Roy van Rijn

CODE COVERAGE• Measure the lines (or branches) that are executed during testing

Page 4: Kill the Mutants - a better way to test your tests by Roy van Rijn

CODE COVERAGE

Page 5: Kill the Mutants - a better way to test your tests by Roy van Rijn

QUIS CUSTODIET IPSOS CUSTODES?Who watches the watchmen?

Page 6: Kill the Mutants - a better way to test your tests by Roy van Rijn

MUTATION TESTING

• Proposed by Richard J. Lipton in 1971

• Surge of interest in the 1980s

• Time to revive this interest!

Page 7: Kill the Mutants - a better way to test your tests by Roy van Rijn

TERMINOLOGY: MUTATION

Page 8: Kill the Mutants - a better way to test your tests by Roy van Rijn

TERMINOLOGY: MUTANT

• A mutant is a mutated version of your class

Page 9: Kill the Mutants - a better way to test your tests by Roy van Rijn

MUTATION TESTING• Generate (a lot of) mutants of your codebase

• Run (some of) your unit tests

• Check the outcome!

Page 10: Kill the Mutants - a better way to test your tests by Roy van Rijn

OUTCOME #1: KILLED

• A mutant is killed if a test fails (detecting the mutated code)

• This proves the mutated code is properly tested

Page 11: Kill the Mutants - a better way to test your tests by Roy van Rijn

OUTCOME #2: LIVED

• A mutant didn’t trigger a failing test…

Page 12: Kill the Mutants - a better way to test your tests by Roy van Rijn

OTHER OUTCOMES

• TIMED OUT: Infinite loop

• NON-VIABLE: Illegal bytecode

• MEMORY ERROR: Out of memory

• RUN ERROR: Other…

Page 13: Kill the Mutants - a better way to test your tests by Roy van Rijn

TOOLING• µJava: http://cs.gmu.edu/~offutt/mujava/ (inactive)

• Jester : http://jester.sourceforge.net/ (inactive)

• Jumble: http://jumble.sourceforge.net/ (inactive)

• javaLanche: http://www.st.cs.uni-saarland.de/mutation/ (inactive)

• PIT: http://pitest.org/

Page 14: Kill the Mutants - a better way to test your tests by Roy van Rijn

USING PIT

• Manipulates bytecode with ASM (at runtime)

• Mutants are never stored

Page 15: Kill the Mutants - a better way to test your tests by Roy van Rijn

MUTATORS

• Example of mutators:

> into >=< into <=>= into ><= into <

Page 16: Kill the Mutants - a better way to test your tests by Roy van Rijn

MUTATION TESTING IS SLOW?

Page 17: Kill the Mutants - a better way to test your tests by Roy van Rijn

WHICH TESTS TO RUN?

• PIT uses code coverage!

Page 18: Kill the Mutants - a better way to test your tests by Roy van Rijn

WHICH TESTS TO RUN?

• A mutation is on a line covered by 3 tests? Only run those.

Page 19: Kill the Mutants - a better way to test your tests by Roy van Rijn

EASY TO USE:

• Standalone Java process

• Build: Ant and Maven plugin

• CI: Sonarqube and Gradle plugin

• IDE: Eclipse and IntelliJ plugin

Page 20: Kill the Mutants - a better way to test your tests by Roy van Rijn

SUMMARY

• Code coverage: BAD

• Mutation testing: GOOD!

• Automatic, easy to add, and meaningful!