SlideShare a Scribd company logo
1 of 55
KILL THE MUTANTS 
a better way to test your tests
ABOUT ME 
• Roy van Rijn 
• Mutants: 
• Nora 
• Lucas 
• Works for
let's do a 
SHOW OF HANDS
WHO DOES 
• Unit testing 
• Test-driven development (TDD) 
• Continuous integration 
• Measure code coverage 
• Mutation testing
UNIT TESTING 
• Prove your code works 
• Instant regression tests 
• Improve code design 
• Has become a mainstream practise over the last 10 years
CONTINUOUS INTEGRATION 
• Automate testing 
• Maintain a single source repository 
• Collect statistics
CODE COVERAGE 
• Measure the lines (or branches) that are executed during 
testing
CODE COVERAGE 
• How did they test your car?
CODE COVERAGE 
• Who has seen (or written?) tests 
• without verifications or assertions? 
• just to fake and boost coverage? 
• 100% branch coverage proves nothing
QUIS CUSTODIET IPSOS 
CUSTODES? Who watches the watchmen?
MUTATION TESTING 
• Proposed by Richard J. Lipton in 1971 (winner of 2014 Knuth 
Prize) 
• A better way to measure the quality of your tests 
• Surge of interest in the 1980s 
• Time to revive this interest!
TERMINOLOGY: MUTATION 
• A mutation is a (small) change in your codebase, for example:
TERMINOLOGY: MUTANT 
• A mutant is a mutated version of your class
MUTATION TESTING 
• Generate (a lot of) mutants of your codebase 
• Run (some of) your unit tests 
• Check the outcome!
OUTCOME #1: KILLED 
• A mutant is killed if a test fails (detecting the mutated code) 
• This proves the mutated code is properly tested
OUTCOME #2: LIVED 
• A mutant didn’t trigger a failing test…
OUTCOME #3: TIMED OUT 
• The mutant caused the program loop, get stuck
OTHER OUTCOMES 
• NON-VIABLE 
• JVM could not load the mutant bytecode 
• MEMORY ERROR 
• JVM ran out of memory during test 
• RUN ERROR 
• An error but none of the above.
FAULT INJECTION? 
• With fault injection you test code 
• Inject faults/mutations and see how the system reacts 
• With mutation testing you test your tests 
• Inject faults/mutations and see how the tests react
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/
USING PIT 
• PIT uses configurable ‘mutators' 
• ASM (bytecode manipulation) is used to mutate your code 
• No mutated code is stored, it can't interfere with your code 
• Generates reports with test results
MUTATORS: CONDITION BOUNDARY 
> into >= 
< into <= 
>= into > 
<= into <
MUTATORS: NEGATE CONDITIONALS 
== into != 
!= into == 
<= into > 
>= into < 
< into >= 
> into <=
MUTATORS: REMOVE 
CONDITIONALS 
into 
if(true) { 
//something 
} 
if(a == b) { 
//something 
}
MUTATORS: MATH 
+ into - 
- into + 
* into / 
/ into * 
% into * 
& into | 
<< into >> 
>> into << 
>>> into <<< 
a++ into a-- 
a-- into a++
MUTATORS: MANY MORE 
• Replacing return values (return a; becomes return 0;) 
• Removal of void invocations (doSomething(); is removed) 
• Some enabled by default, others are optional/configurable
MUTATION TESTING IS SLOW? 
• Speed was unacceptable in the 80's 
• Mutation testing is still CPU intensive 
• But PIT has a lot of methods to speed it up!
WHICH TESTS TO RUN? 
• PIT uses code coverage to decide which tests to run: 
• A mutation is on a line covered by 3 tests? Only run those.
SIMPLE EXAMPLE 
• 100 classes 
• 10 unit tests per class 
• 2 ms per unit test 
• Total time (all tests): 100 x 10 x 2ms = 2s
SIMPLE EXAMPLE 
• Total time (all tests): 100 x 10 x 2ms = 2s 
• 8 mutants per class, 100 classes x 8 = 800 mutants 
• Brute force: 800 x 2s = 26m40s 
• Smart testing: 800 x 10 x 2ms = 16s
LONGER EXAMPLE 
• Total time (all tests): 1000 x 10 x 2ms = 20s 
• 8 mutants per class, 1000 classes x 8 = 8000 mutants 
• Brute force: 8000 x 20s = 1d20h26m40s…!!! 
• Smart testing: 8000 x 10 x 2ms = 2m40s
PERFORMANCE TIPS 
• Write fast tests 
• Good separation or concerns 
• Use small classes, keep amount of unit tests per class low
INCREMENTAL ANALYSIS 
• Experimental feature 
• Incremental analysis keeps track of: 
• Changes in the codebase 
• Previous results
HOW ABOUT MOCKING? 
• PIT has support for: 
• Mockito, EasyMock, JMock, PowerMock and JMockit
HOW TO USE PIT? 
• Standalone Java process 
• Build: Ant task, Maven plugin 
• CI: Sonarqube plugin, Gradle plugin 
• IDE: Eclipse plugin (Pitclipse), IntelliJ Plugin
STANDALONE JAVA 
java -cp <your classpath including pit jar and dependencies> 
org.pitest.mutationtest.commandline.MutationCoverageReport 
--reportDir /somePath/ 
--targetClasses com.your.package.tobemutated* 
--targetTests com.your.package.* 
--sourceDirs /sourcePath/
MAVEN PLUGIN 
<plugin> 
<groupId>org.pitest</groupId> 
<artifactId>pitest-maven</artifactId> 
<version>1.0.0</version> 
<configuration> 
<targetClasses> 
<param>com.your.package.tobemutated*</param> 
</targetClasses> 
<jvmArgs>…</jvmArgs> 
</configuration> 
</plugin> 
Run as: mvn clean package org.pitest:pitest-maven:mutationCoverage
EXAMPLE 
Let’s kill some mutants… or be killed.
USE CASE 
The price of an item is 17 euro 
If you buy 20 or more, all items cost 15 euro 
If you have a coupon, all items cost 15 euro
CODE 
public int getPrice(int amountOfThings, boolean coupon) { 
if (amountOfThings >= 20 || coupon) { 
return amountOfThings * 15; 
} 
return amountOfThings * 17; 
}
TEST #1 
@Test 
public void testNormalPricing() { 
//Not enough for discount: 
int amount = 1; 
Assert.assertEquals(17, businessLogic.getPrice(amount, false)); 
}
BRANCH COVERAGE 
public int getPrice(int amountOfThings, boolean coupon) { 
if (amountOfThings >= 20 || coupon) { 
return amountOfThings * 15; 
} 
return amountOfThings * 17; 
}
TEST #2 
@Test 
public void testDiscountPricingByAmount() { 
//Enough for discount: 
int amount = 100; 
Assert.assertEquals(1500, businessLogic.getPrice(amount, false)); 
}
BRANCH COVERAGE 
public int getPrice(int amountOfThings, boolean coupon) { 
if (amountOfThings >= 20 || coupon) { 
return amountOfThings * 15; 
} 
return amountOfThings * 17; 
}
TEST #3 
@Test 
public void testDiscountWithCoupon() { 
//Not enough for discount, but coupon: 
int amount = 1; 
Assert.assertEquals(15, businessLogic.getPrice(amount, true)); 
}
BRANCH COVERAGE 
public int getPrice(int amountOfThings, boolean coupon) { 
if (amountOfThings >= 20 || coupon) { 
return amountOfThings * 15; 
} 
return amountOfThings * 17; 
}
PIT RESULT
PIT RESULT 
> org.pitest.mutationtest…ConditionalsBoundaryMutator 
>> Generated 1 Killed 0 (0%) 
> KILLED 0 SURVIVED 1 TIMED_OUT 0 NON_VIABLE 0 
> MEMORY_ERROR 0 NOT_STARTED 0 STARTED 0 RUN_ERROR 0 
> NO_COVERAGE 0 
PIT tells us: Changing >= into > doesn’t trigger a failing test
TEST #4 
@Test 
public void testDiscountAmountCornerCase() { 
//Just enough for discount, mutation into > should fail this test 
int amount = 20; 
Assert.assertEquals(300, businessLogic.getPrice(amount, true)); 
}
BRANCH COVERAGE 
public int getPrice(int amountOfThings, boolean coupon) { 
if (amountOfThings >= 20 || coupon) { 
return amountOfThings * 15; 
} 
return amountOfThings * 17; 
}
PIT RESULT
PIT RESULT 
> org.pitest.mutationtest…ConditionalsBoundaryMutator 
>> Generated 1 Killed 0 (0%) 
> KILLED 0 SURVIVED 1 TIMED_OUT 0 NON_VIABLE 0 
> MEMORY_ERROR 0 NOT_STARTED 0 STARTED 0 RUN_ERROR 0 
> NO_COVERAGE 0 
STILL WRONG!?
DID YOU SPOT THE BUG? 
@Test 
public void testDiscountAmountCornerCase() { 
//Just enough for discount, mutation into > should fail this test 
int amount = 20; 
Assert.assertEquals(300, businessLogic.getPrice(amount, true)); 
}
SUMMARY 
• Mutation testing automatically tests your tests 
• Mutation testing can find bugs in your tests 
• Code coverage is wrong, gives a false sense of security 
• Mutation testing with PIT is easy to implement
QUESTIONS?

More Related Content

What's hot

What's hot (20)

Mutation testing in Java
Mutation testing in JavaMutation testing in Java
Mutation testing in Java
 
An introduction to Google test framework
An introduction to Google test frameworkAn introduction to Google test framework
An introduction to Google test framework
 
C++ Unit Test with Google Testing Framework
C++ Unit Test with Google Testing FrameworkC++ Unit Test with Google Testing Framework
C++ Unit Test with Google Testing Framework
 
3 j unit
3 j unit3 j unit
3 j unit
 
Advanced junit and mockito
Advanced junit and mockitoAdvanced junit and mockito
Advanced junit and mockito
 
JUnit Kung Fu: Getting More Out of Your Unit Tests
JUnit Kung Fu: Getting More Out of Your Unit TestsJUnit Kung Fu: Getting More Out of Your Unit Tests
JUnit Kung Fu: Getting More Out of Your Unit Tests
 
JUnit & Mockito, first steps
JUnit & Mockito, first stepsJUnit & Mockito, first steps
JUnit & Mockito, first steps
 
Unit testing with java
Unit testing with javaUnit testing with java
Unit testing with java
 
Junit 4.0
Junit 4.0Junit 4.0
Junit 4.0
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Junit
JunitJunit
Junit
 
Junit
JunitJunit
Junit
 
Introduzione al TDD
Introduzione al TDDIntroduzione al TDD
Introduzione al TDD
 
Mutation-Testing mit PIT
Mutation-Testing mit PITMutation-Testing mit PIT
Mutation-Testing mit PIT
 
GeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good TestsGeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good Tests
 
05 junit
05 junit05 junit
05 junit
 
Confitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good TestsConfitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good Tests
 
Unit Testing Presentation
Unit Testing PresentationUnit Testing Presentation
Unit Testing Presentation
 
Testing with Junit4
Testing with Junit4Testing with Junit4
Testing with Junit4
 
Sample Chapter of Practical Unit Testing with TestNG and Mockito
Sample Chapter of Practical Unit Testing with TestNG and MockitoSample Chapter of Practical Unit Testing with TestNG and Mockito
Sample Chapter of Practical Unit Testing with TestNG and Mockito
 

Similar to Kill the mutants and test your tests - Roy van Rijn

Cpp Testing Techniques Tips and Tricks - Cpp Europe
Cpp Testing Techniques Tips and Tricks - Cpp EuropeCpp Testing Techniques Tips and Tricks - Cpp Europe
Cpp Testing Techniques Tips and Tricks - Cpp EuropeClare Macrae
 
Qt test framework
Qt test frameworkQt test framework
Qt test frameworkICS
 
How good are your tests?
How good are your tests?How good are your tests?
How good are your tests?Noam Shaish
 
Building unit tests correctly with visual studio 2013
Building unit tests correctly with visual studio 2013Building unit tests correctly with visual studio 2013
Building unit tests correctly with visual studio 2013Dror Helper
 
Testing the waters of iOS
Testing the waters of iOSTesting the waters of iOS
Testing the waters of iOSKremizas Kostas
 
Testing As A Bottleneck - How Testing Slows Down Modern Development Processes...
Testing As A Bottleneck - How Testing Slows Down Modern Development Processes...Testing As A Bottleneck - How Testing Slows Down Modern Development Processes...
Testing As A Bottleneck - How Testing Slows Down Modern Development Processes...TEST Huddle
 
Wix Automation - The False Positive Paradox
Wix Automation - The False Positive ParadoxWix Automation - The False Positive Paradox
Wix Automation - The False Positive ParadoxEfrat Attas
 
I.T.A.K.E Unconference - Mutation testing to the rescue of your tests
I.T.A.K.E Unconference - Mutation testing to the rescue of your testsI.T.A.K.E Unconference - Mutation testing to the rescue of your tests
I.T.A.K.E Unconference - Mutation testing to the rescue of your testsNicolas Fränkel
 
New types of tests for Java projects
New types of tests for Java projectsNew types of tests for Java projects
New types of tests for Java projectsVincent Massol
 
Implementing Quality on a Java Project
Implementing Quality on a Java ProjectImplementing Quality on a Java Project
Implementing Quality on a Java ProjectVincent Massol
 
Renaissance of JUnit - Introduction to JUnit 5
Renaissance of JUnit - Introduction to JUnit 5Renaissance of JUnit - Introduction to JUnit 5
Renaissance of JUnit - Introduction to JUnit 5Jimmy Lu
 
We Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End DevelopmentWe Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End DevelopmentAll Things Open
 
Mutation testing
Mutation testingMutation testing
Mutation testing기영 이
 
Level Up Your Salesforce Unit Testing
Level Up Your Salesforce Unit TestingLevel Up Your Salesforce Unit Testing
Level Up Your Salesforce Unit TestingGordon Bockus
 
Principles and patterns for test driven development
Principles and patterns for test driven developmentPrinciples and patterns for test driven development
Principles and patterns for test driven developmentStephen Fuqua
 
Test Driven Development with Sql Server
Test Driven Development with Sql ServerTest Driven Development with Sql Server
Test Driven Development with Sql ServerDavid P. Moore
 
Oop 2015 – Mutation Testing
Oop 2015 – Mutation TestingOop 2015 – Mutation Testing
Oop 2015 – Mutation TestingFilip Van Laenen
 
An Introduction to unit testing
An Introduction to unit testingAn Introduction to unit testing
An Introduction to unit testingSteven Casey
 

Similar to Kill the mutants and test your tests - Roy van Rijn (20)

Cpp Testing Techniques Tips and Tricks - Cpp Europe
Cpp Testing Techniques Tips and Tricks - Cpp EuropeCpp Testing Techniques Tips and Tricks - Cpp Europe
Cpp Testing Techniques Tips and Tricks - Cpp Europe
 
Qt test framework
Qt test frameworkQt test framework
Qt test framework
 
How good are your tests?
How good are your tests?How good are your tests?
How good are your tests?
 
Building unit tests correctly with visual studio 2013
Building unit tests correctly with visual studio 2013Building unit tests correctly with visual studio 2013
Building unit tests correctly with visual studio 2013
 
Testing the waters of iOS
Testing the waters of iOSTesting the waters of iOS
Testing the waters of iOS
 
Testing As A Bottleneck - How Testing Slows Down Modern Development Processes...
Testing As A Bottleneck - How Testing Slows Down Modern Development Processes...Testing As A Bottleneck - How Testing Slows Down Modern Development Processes...
Testing As A Bottleneck - How Testing Slows Down Modern Development Processes...
 
Wix Automation - The False Positive Paradox
Wix Automation - The False Positive ParadoxWix Automation - The False Positive Paradox
Wix Automation - The False Positive Paradox
 
I.T.A.K.E Unconference - Mutation testing to the rescue of your tests
I.T.A.K.E Unconference - Mutation testing to the rescue of your testsI.T.A.K.E Unconference - Mutation testing to the rescue of your tests
I.T.A.K.E Unconference - Mutation testing to the rescue of your tests
 
New types of tests for Java projects
New types of tests for Java projectsNew types of tests for Java projects
New types of tests for Java projects
 
Implementing Quality on a Java Project
Implementing Quality on a Java ProjectImplementing Quality on a Java Project
Implementing Quality on a Java Project
 
Renaissance of JUnit - Introduction to JUnit 5
Renaissance of JUnit - Introduction to JUnit 5Renaissance of JUnit - Introduction to JUnit 5
Renaissance of JUnit - Introduction to JUnit 5
 
We Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End DevelopmentWe Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End Development
 
Mutation testing
Mutation testingMutation testing
Mutation testing
 
Level Up Your Salesforce Unit Testing
Level Up Your Salesforce Unit TestingLevel Up Your Salesforce Unit Testing
Level Up Your Salesforce Unit Testing
 
des mutants dans le code.pdf
des mutants dans le code.pdfdes mutants dans le code.pdf
des mutants dans le code.pdf
 
Unit testing basics
Unit testing basicsUnit testing basics
Unit testing basics
 
Principles and patterns for test driven development
Principles and patterns for test driven developmentPrinciples and patterns for test driven development
Principles and patterns for test driven development
 
Test Driven Development with Sql Server
Test Driven Development with Sql ServerTest Driven Development with Sql Server
Test Driven Development with Sql Server
 
Oop 2015 – Mutation Testing
Oop 2015 – Mutation TestingOop 2015 – Mutation Testing
Oop 2015 – Mutation Testing
 
An Introduction to unit testing
An Introduction to unit testingAn Introduction to unit testing
An Introduction to unit testing
 

More from NLJUG

The future of Web-Scale - Johan Tillema, Rene Boere & Chris Quach
The future of Web-Scale - Johan Tillema, Rene Boere & Chris QuachThe future of Web-Scale - Johan Tillema, Rene Boere & Chris Quach
The future of Web-Scale - Johan Tillema, Rene Boere & Chris QuachNLJUG
 
Speedy perception trumps speedy reception–smart asynchronous interactions - L...
Speedy perception trumps speedy reception–smart asynchronous interactions - L...Speedy perception trumps speedy reception–smart asynchronous interactions - L...
Speedy perception trumps speedy reception–smart asynchronous interactions - L...NLJUG
 
Decoding the airspace above you with Java and $7 hardware - Bert Jan Schrijver
Decoding the airspace above you with Java and $7 hardware - Bert Jan SchrijverDecoding the airspace above you with Java and $7 hardware - Bert Jan Schrijver
Decoding the airspace above you with Java and $7 hardware - Bert Jan SchrijverNLJUG
 
Using Docker to Develop, Test and Run Maven Projects - Wouter Danes
Using Docker to Develop, Test and Run Maven Projects - Wouter DanesUsing Docker to Develop, Test and Run Maven Projects - Wouter Danes
Using Docker to Develop, Test and Run Maven Projects - Wouter DanesNLJUG
 
Real-time user interfaces - sosm gewoon makkelijker - Allard Buijze
Real-time user interfaces - sosm gewoon makkelijker - Allard BuijzeReal-time user interfaces - sosm gewoon makkelijker - Allard Buijze
Real-time user interfaces - sosm gewoon makkelijker - Allard BuijzeNLJUG
 
The end of traditional enterprise IT - ING's journey to the next generation I...
The end of traditional enterprise IT - ING's journey to the next generation I...The end of traditional enterprise IT - ING's journey to the next generation I...
The end of traditional enterprise IT - ING's journey to the next generation I...NLJUG
 
Performance van Java 8 en verder - Jeroen Borgers
Performance van Java 8 en verder - Jeroen BorgersPerformance van Java 8 en verder - Jeroen Borgers
Performance van Java 8 en verder - Jeroen BorgersNLJUG
 
Introduction to Reactive with Play and Akka - Markus Jura
Introduction to Reactive with Play and Akka - Markus JuraIntroduction to Reactive with Play and Akka - Markus Jura
Introduction to Reactive with Play and Akka - Markus JuraNLJUG
 
Web-scale op basis van Hadoop en Akka Reactive Streams - Johan Tillema, Rene ...
Web-scale op basis van Hadoop en Akka Reactive Streams - Johan Tillema, Rene ...Web-scale op basis van Hadoop en Akka Reactive Streams - Johan Tillema, Rene ...
Web-scale op basis van Hadoop en Akka Reactive Streams - Johan Tillema, Rene ...NLJUG
 
Workshop angular dart presentatie - Atos
Workshop angular dart presentatie - AtosWorkshop angular dart presentatie - Atos
Workshop angular dart presentatie - AtosNLJUG
 
Workshop spring boot presentatie - Atos
Workshop spring boot presentatie - AtosWorkshop spring boot presentatie - Atos
Workshop spring boot presentatie - AtosNLJUG
 
Cultivating the jenkins job jungle with groovy - Patrick van Dissel
Cultivating the jenkins job jungle with groovy - Patrick van DisselCultivating the jenkins job jungle with groovy - Patrick van Dissel
Cultivating the jenkins job jungle with groovy - Patrick van DisselNLJUG
 
Rethink your architecture - Marten Deinum
Rethink your architecture - Marten DeinumRethink your architecture - Marten Deinum
Rethink your architecture - Marten DeinumNLJUG
 
Evolutionary Algorithms: the key to solving complex Java puzzles! - Bas knopper
Evolutionary Algorithms: the key to solving complex Java puzzles! - Bas knopperEvolutionary Algorithms: the key to solving complex Java puzzles! - Bas knopper
Evolutionary Algorithms: the key to solving complex Java puzzles! - Bas knopperNLJUG
 
Modularity and Domain Driven Design; a killer Combination? - Tom de Wolf & St...
Modularity and Domain Driven Design; a killer Combination? - Tom de Wolf & St...Modularity and Domain Driven Design; a killer Combination? - Tom de Wolf & St...
Modularity and Domain Driven Design; a killer Combination? - Tom de Wolf & St...NLJUG
 
Apache Wicket: 10 jaar en verder - Martijn Dashorst
Apache Wicket: 10 jaar en verder - Martijn DashorstApache Wicket: 10 jaar en verder - Martijn Dashorst
Apache Wicket: 10 jaar en verder - Martijn DashorstNLJUG
 
Opening - Bert Ertman
Opening - Bert ErtmanOpening - Bert Ertman
Opening - Bert ErtmanNLJUG
 
Returning the right results - Jettro Coenradie
Returning the right results - Jettro CoenradieReturning the right results - Jettro Coenradie
Returning the right results - Jettro CoenradieNLJUG
 
Reactive programming met Java 8 en Java EE 7 - Martijn Blankestijn
Reactive programming met Java 8 en Java EE 7 - Martijn BlankestijnReactive programming met Java 8 en Java EE 7 - Martijn Blankestijn
Reactive programming met Java 8 en Java EE 7 - Martijn BlankestijnNLJUG
 
Event-sourced architectures with Akka - Sander Mak
Event-sourced architectures with Akka - Sander MakEvent-sourced architectures with Akka - Sander Mak
Event-sourced architectures with Akka - Sander MakNLJUG
 

More from NLJUG (20)

The future of Web-Scale - Johan Tillema, Rene Boere & Chris Quach
The future of Web-Scale - Johan Tillema, Rene Boere & Chris QuachThe future of Web-Scale - Johan Tillema, Rene Boere & Chris Quach
The future of Web-Scale - Johan Tillema, Rene Boere & Chris Quach
 
Speedy perception trumps speedy reception–smart asynchronous interactions - L...
Speedy perception trumps speedy reception–smart asynchronous interactions - L...Speedy perception trumps speedy reception–smart asynchronous interactions - L...
Speedy perception trumps speedy reception–smart asynchronous interactions - L...
 
Decoding the airspace above you with Java and $7 hardware - Bert Jan Schrijver
Decoding the airspace above you with Java and $7 hardware - Bert Jan SchrijverDecoding the airspace above you with Java and $7 hardware - Bert Jan Schrijver
Decoding the airspace above you with Java and $7 hardware - Bert Jan Schrijver
 
Using Docker to Develop, Test and Run Maven Projects - Wouter Danes
Using Docker to Develop, Test and Run Maven Projects - Wouter DanesUsing Docker to Develop, Test and Run Maven Projects - Wouter Danes
Using Docker to Develop, Test and Run Maven Projects - Wouter Danes
 
Real-time user interfaces - sosm gewoon makkelijker - Allard Buijze
Real-time user interfaces - sosm gewoon makkelijker - Allard BuijzeReal-time user interfaces - sosm gewoon makkelijker - Allard Buijze
Real-time user interfaces - sosm gewoon makkelijker - Allard Buijze
 
The end of traditional enterprise IT - ING's journey to the next generation I...
The end of traditional enterprise IT - ING's journey to the next generation I...The end of traditional enterprise IT - ING's journey to the next generation I...
The end of traditional enterprise IT - ING's journey to the next generation I...
 
Performance van Java 8 en verder - Jeroen Borgers
Performance van Java 8 en verder - Jeroen BorgersPerformance van Java 8 en verder - Jeroen Borgers
Performance van Java 8 en verder - Jeroen Borgers
 
Introduction to Reactive with Play and Akka - Markus Jura
Introduction to Reactive with Play and Akka - Markus JuraIntroduction to Reactive with Play and Akka - Markus Jura
Introduction to Reactive with Play and Akka - Markus Jura
 
Web-scale op basis van Hadoop en Akka Reactive Streams - Johan Tillema, Rene ...
Web-scale op basis van Hadoop en Akka Reactive Streams - Johan Tillema, Rene ...Web-scale op basis van Hadoop en Akka Reactive Streams - Johan Tillema, Rene ...
Web-scale op basis van Hadoop en Akka Reactive Streams - Johan Tillema, Rene ...
 
Workshop angular dart presentatie - Atos
Workshop angular dart presentatie - AtosWorkshop angular dart presentatie - Atos
Workshop angular dart presentatie - Atos
 
Workshop spring boot presentatie - Atos
Workshop spring boot presentatie - AtosWorkshop spring boot presentatie - Atos
Workshop spring boot presentatie - Atos
 
Cultivating the jenkins job jungle with groovy - Patrick van Dissel
Cultivating the jenkins job jungle with groovy - Patrick van DisselCultivating the jenkins job jungle with groovy - Patrick van Dissel
Cultivating the jenkins job jungle with groovy - Patrick van Dissel
 
Rethink your architecture - Marten Deinum
Rethink your architecture - Marten DeinumRethink your architecture - Marten Deinum
Rethink your architecture - Marten Deinum
 
Evolutionary Algorithms: the key to solving complex Java puzzles! - Bas knopper
Evolutionary Algorithms: the key to solving complex Java puzzles! - Bas knopperEvolutionary Algorithms: the key to solving complex Java puzzles! - Bas knopper
Evolutionary Algorithms: the key to solving complex Java puzzles! - Bas knopper
 
Modularity and Domain Driven Design; a killer Combination? - Tom de Wolf & St...
Modularity and Domain Driven Design; a killer Combination? - Tom de Wolf & St...Modularity and Domain Driven Design; a killer Combination? - Tom de Wolf & St...
Modularity and Domain Driven Design; a killer Combination? - Tom de Wolf & St...
 
Apache Wicket: 10 jaar en verder - Martijn Dashorst
Apache Wicket: 10 jaar en verder - Martijn DashorstApache Wicket: 10 jaar en verder - Martijn Dashorst
Apache Wicket: 10 jaar en verder - Martijn Dashorst
 
Opening - Bert Ertman
Opening - Bert ErtmanOpening - Bert Ertman
Opening - Bert Ertman
 
Returning the right results - Jettro Coenradie
Returning the right results - Jettro CoenradieReturning the right results - Jettro Coenradie
Returning the right results - Jettro Coenradie
 
Reactive programming met Java 8 en Java EE 7 - Martijn Blankestijn
Reactive programming met Java 8 en Java EE 7 - Martijn BlankestijnReactive programming met Java 8 en Java EE 7 - Martijn Blankestijn
Reactive programming met Java 8 en Java EE 7 - Martijn Blankestijn
 
Event-sourced architectures with Akka - Sander Mak
Event-sourced architectures with Akka - Sander MakEvent-sourced architectures with Akka - Sander Mak
Event-sourced architectures with Akka - Sander Mak
 

Recently uploaded

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 

Recently uploaded (20)

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 

Kill the mutants and test your tests - Roy van Rijn

  • 1. KILL THE MUTANTS a better way to test your tests
  • 2. ABOUT ME • Roy van Rijn • Mutants: • Nora • Lucas • Works for
  • 3. let's do a SHOW OF HANDS
  • 4. WHO DOES • Unit testing • Test-driven development (TDD) • Continuous integration • Measure code coverage • Mutation testing
  • 5. UNIT TESTING • Prove your code works • Instant regression tests • Improve code design • Has become a mainstream practise over the last 10 years
  • 6. CONTINUOUS INTEGRATION • Automate testing • Maintain a single source repository • Collect statistics
  • 7. CODE COVERAGE • Measure the lines (or branches) that are executed during testing
  • 8. CODE COVERAGE • How did they test your car?
  • 9. CODE COVERAGE • Who has seen (or written?) tests • without verifications or assertions? • just to fake and boost coverage? • 100% branch coverage proves nothing
  • 10. QUIS CUSTODIET IPSOS CUSTODES? Who watches the watchmen?
  • 11. MUTATION TESTING • Proposed by Richard J. Lipton in 1971 (winner of 2014 Knuth Prize) • A better way to measure the quality of your tests • Surge of interest in the 1980s • Time to revive this interest!
  • 12. TERMINOLOGY: MUTATION • A mutation is a (small) change in your codebase, for example:
  • 13. TERMINOLOGY: MUTANT • A mutant is a mutated version of your class
  • 14. MUTATION TESTING • Generate (a lot of) mutants of your codebase • Run (some of) your unit tests • Check the outcome!
  • 15. OUTCOME #1: KILLED • A mutant is killed if a test fails (detecting the mutated code) • This proves the mutated code is properly tested
  • 16. OUTCOME #2: LIVED • A mutant didn’t trigger a failing test…
  • 17. OUTCOME #3: TIMED OUT • The mutant caused the program loop, get stuck
  • 18. OTHER OUTCOMES • NON-VIABLE • JVM could not load the mutant bytecode • MEMORY ERROR • JVM ran out of memory during test • RUN ERROR • An error but none of the above.
  • 19. FAULT INJECTION? • With fault injection you test code • Inject faults/mutations and see how the system reacts • With mutation testing you test your tests • Inject faults/mutations and see how the tests react
  • 20. 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/
  • 21. USING PIT • PIT uses configurable ‘mutators' • ASM (bytecode manipulation) is used to mutate your code • No mutated code is stored, it can't interfere with your code • Generates reports with test results
  • 22. MUTATORS: CONDITION BOUNDARY > into >= < into <= >= into > <= into <
  • 23. MUTATORS: NEGATE CONDITIONALS == into != != into == <= into > >= into < < into >= > into <=
  • 24. MUTATORS: REMOVE CONDITIONALS into if(true) { //something } if(a == b) { //something }
  • 25. MUTATORS: MATH + into - - into + * into / / into * % into * & into | << into >> >> into << >>> into <<< a++ into a-- a-- into a++
  • 26. MUTATORS: MANY MORE • Replacing return values (return a; becomes return 0;) • Removal of void invocations (doSomething(); is removed) • Some enabled by default, others are optional/configurable
  • 27. MUTATION TESTING IS SLOW? • Speed was unacceptable in the 80's • Mutation testing is still CPU intensive • But PIT has a lot of methods to speed it up!
  • 28. WHICH TESTS TO RUN? • PIT uses code coverage to decide which tests to run: • A mutation is on a line covered by 3 tests? Only run those.
  • 29. SIMPLE EXAMPLE • 100 classes • 10 unit tests per class • 2 ms per unit test • Total time (all tests): 100 x 10 x 2ms = 2s
  • 30. SIMPLE EXAMPLE • Total time (all tests): 100 x 10 x 2ms = 2s • 8 mutants per class, 100 classes x 8 = 800 mutants • Brute force: 800 x 2s = 26m40s • Smart testing: 800 x 10 x 2ms = 16s
  • 31. LONGER EXAMPLE • Total time (all tests): 1000 x 10 x 2ms = 20s • 8 mutants per class, 1000 classes x 8 = 8000 mutants • Brute force: 8000 x 20s = 1d20h26m40s…!!! • Smart testing: 8000 x 10 x 2ms = 2m40s
  • 32. PERFORMANCE TIPS • Write fast tests • Good separation or concerns • Use small classes, keep amount of unit tests per class low
  • 33. INCREMENTAL ANALYSIS • Experimental feature • Incremental analysis keeps track of: • Changes in the codebase • Previous results
  • 34. HOW ABOUT MOCKING? • PIT has support for: • Mockito, EasyMock, JMock, PowerMock and JMockit
  • 35. HOW TO USE PIT? • Standalone Java process • Build: Ant task, Maven plugin • CI: Sonarqube plugin, Gradle plugin • IDE: Eclipse plugin (Pitclipse), IntelliJ Plugin
  • 36. STANDALONE JAVA java -cp <your classpath including pit jar and dependencies> org.pitest.mutationtest.commandline.MutationCoverageReport --reportDir /somePath/ --targetClasses com.your.package.tobemutated* --targetTests com.your.package.* --sourceDirs /sourcePath/
  • 37. MAVEN PLUGIN <plugin> <groupId>org.pitest</groupId> <artifactId>pitest-maven</artifactId> <version>1.0.0</version> <configuration> <targetClasses> <param>com.your.package.tobemutated*</param> </targetClasses> <jvmArgs>…</jvmArgs> </configuration> </plugin> Run as: mvn clean package org.pitest:pitest-maven:mutationCoverage
  • 38. EXAMPLE Let’s kill some mutants… or be killed.
  • 39. USE CASE The price of an item is 17 euro If you buy 20 or more, all items cost 15 euro If you have a coupon, all items cost 15 euro
  • 40. CODE public int getPrice(int amountOfThings, boolean coupon) { if (amountOfThings >= 20 || coupon) { return amountOfThings * 15; } return amountOfThings * 17; }
  • 41. TEST #1 @Test public void testNormalPricing() { //Not enough for discount: int amount = 1; Assert.assertEquals(17, businessLogic.getPrice(amount, false)); }
  • 42. BRANCH COVERAGE public int getPrice(int amountOfThings, boolean coupon) { if (amountOfThings >= 20 || coupon) { return amountOfThings * 15; } return amountOfThings * 17; }
  • 43. TEST #2 @Test public void testDiscountPricingByAmount() { //Enough for discount: int amount = 100; Assert.assertEquals(1500, businessLogic.getPrice(amount, false)); }
  • 44. BRANCH COVERAGE public int getPrice(int amountOfThings, boolean coupon) { if (amountOfThings >= 20 || coupon) { return amountOfThings * 15; } return amountOfThings * 17; }
  • 45. TEST #3 @Test public void testDiscountWithCoupon() { //Not enough for discount, but coupon: int amount = 1; Assert.assertEquals(15, businessLogic.getPrice(amount, true)); }
  • 46. BRANCH COVERAGE public int getPrice(int amountOfThings, boolean coupon) { if (amountOfThings >= 20 || coupon) { return amountOfThings * 15; } return amountOfThings * 17; }
  • 48. PIT RESULT > org.pitest.mutationtest…ConditionalsBoundaryMutator >> Generated 1 Killed 0 (0%) > KILLED 0 SURVIVED 1 TIMED_OUT 0 NON_VIABLE 0 > MEMORY_ERROR 0 NOT_STARTED 0 STARTED 0 RUN_ERROR 0 > NO_COVERAGE 0 PIT tells us: Changing >= into > doesn’t trigger a failing test
  • 49. TEST #4 @Test public void testDiscountAmountCornerCase() { //Just enough for discount, mutation into > should fail this test int amount = 20; Assert.assertEquals(300, businessLogic.getPrice(amount, true)); }
  • 50. BRANCH COVERAGE public int getPrice(int amountOfThings, boolean coupon) { if (amountOfThings >= 20 || coupon) { return amountOfThings * 15; } return amountOfThings * 17; }
  • 52. PIT RESULT > org.pitest.mutationtest…ConditionalsBoundaryMutator >> Generated 1 Killed 0 (0%) > KILLED 0 SURVIVED 1 TIMED_OUT 0 NON_VIABLE 0 > MEMORY_ERROR 0 NOT_STARTED 0 STARTED 0 RUN_ERROR 0 > NO_COVERAGE 0 STILL WRONG!?
  • 53. DID YOU SPOT THE BUG? @Test public void testDiscountAmountCornerCase() { //Just enough for discount, mutation into > should fail this test int amount = 20; Assert.assertEquals(300, businessLogic.getPrice(amount, true)); }
  • 54. SUMMARY • Mutation testing automatically tests your tests • Mutation testing can find bugs in your tests • Code coverage is wrong, gives a false sense of security • Mutation testing with PIT is easy to implement