SlideShare a Scribd company logo
MUTATION TESTING
TO THE RESCUE OF
YOUR TESTS
NICOLAS FRÄNKEL
@nicolas_frankel #mutationtesting
2
HYBRIS, AN SAP COMPANY
@nicolas_frankel #mutationtesting
3
ME, MYSELF AND I
@nicolas_frankel #mutationtesting
By day
• Consultant
By night
• Teacher
• Book author
• Conference speaker
• Blogger
4
MANY KINDS OF TESTING
@nicolas_frankel #mutationtesting
Unit Testing
Integration Testing
End-to-end Testing
Performance Testing
Penetration Testing
Exploratory Testing
etc.
5
THEIR ONLY SINGLE GOAL
@nicolas_frankel #mutationtesting
Ensure the quality of the
production code
6
THE PROBLEM
@nicolas_frankel #mutationtesting
How to check the quality of
the testing code?
7
CODE COVERAGE
@nicolas_frankel #mutationtesting
“Code coverage is a
measure used to describe the
degree to which the source
code of a program is tested”
--Wikipedia
http://en.wikipedia.org/wiki/Co
de_coverage
8
MEASURING CODE COVERAGE
@nicolas_frankel #mutationtesting
Check whether a source
code line is executed during a
test
• Or Branch Coverage
9
COMPUTING CODE COVERAGE
CC =
Lexecuted
Ltotal
*100
CC: Code Coverage
(in percent)
Lexecuted: Number of
executed lines of code
Ltotal: Number of total
lines of code
@nicolas_frankel #mutationtesting
10
JAVA TOOLS FOR CODE COVERAGE
@nicolas_frankel #mutationtesting
JaCoCo
Clover
Cobertura
etc.
11
100% CODE COVERAGE?
@nicolas_frankel #mutationtesting
“Is 100% code coverage
realistic? Of course it is. If you
can write a line of code, you can
write another that tests it.”
Robert Martin (Uncle Bob)
https://twitter.com/unclebobmarti
n/status/55966620509667328
12
ASSERT-LESS TESTING
@Test
public void add_should_add() {
new Math().add(1, 1);
}
@nicolas_frankel #mutationtesting
But, where is the
assert?
As long as the Code Coverage is
OK…
13
CODE COVERAGE AS A MEASURE OF QUALITY
@nicolas_frankel #mutationtesting
Any metric can be gamed!
Code coverage is a metric…
⇒ Code coverage can be
gamed
• On purpose
• Or by accident
14
CODE COVERAGE AS A MEASURE OF QUALITY
@nicolas_frankel #mutationtesting
Code Coverage lulls you into
a false sense of security…
15
THE PROBLEM STILL STANDS
@nicolas_frankel #mutationtesting
Code coverage cannot
ensure test quality
• Is there another way?
Mutation Testing to the
rescue!
16
THE CAST
@nicolas_frankel #mutationtesting
William Stryker
Original Source Code
Jason Stryker
Modified Source Code
a.k.a “The Mutant”
17
public class Math {
public int add(int i1, int i2) {
return i1 + i2;
}
}
@nicolas_frankel #mutationtesting
public class Math {
public int add(int i1, int i2) {
return i1 - i2;
}
}
18
STANDARD TESTING
@nicolas_frankel #mutationtesting
✔Execute Test
19
MUTATION TESTING
@nicolas_frankel #mutationtesting
?Execute SAME Test
MUTATION
20
MUTATION TESTING
@nicolas_frankel #mutationtesting
✗
✔Execute SAME Test
Execute SAME Test
Mutant
Killed
Mutant
Survived
21
KILLED OR SURVIVING?
@nicolas_frankel #mutationtesting
Surviving means changing the
source code did not change the test
result
• It’s bad!
Killed means changing the source
code changed the test result
• It’s good
22
TEST THE CODE
@nicolas_frankel #mutationtesting
✔Execute Test
public class Math {
public int add(int i1, int i2) {
return i1 - i2;
}
}
@Test
public void add_should_add() {
new Math().add(1, 1);
}
23
✔Execute Test
SURVIVING MUTANT
@nicolas_frankel #mutationtesting
public class Math {
public int add(int i1, int i2) {
return i1 - i2;
}
}
@Test
public void add_should_add() {
new Math().add(1, 1);
}
24
TEST THE CODE
@nicolas_frankel #mutationtesting
@Test
public void add_should_add() {
int sum = new Math().add(1, 1);
Assert.assertEquals(sum, 2);
}
✔Execute Test
public class Math {
public int add(int i1, int i2) {
return i1 - i2;
}
}
25
KILLED MUTANT
@nicolas_frankel #mutationtesting
✗Execute SAME Test
@Test
public void add_should_add() {
int sum = new Math().add(1, 1);
Assert.assertEquals(sum, 2);
}
public class Math {
public int add(int i1, int i2) {
return i1 - i2;
}
}
26
MUTATION TESTING IN JAVA
@nicolas_frankel #mutationtesting
PIT is a tool for Mutation
testing
Available as
• Command-line tool
• Ant target
• Maven plugin
27
MUTATORS
@nicolas_frankel #mutationtesting
Mutators are patterns
applied to source code to
produce mutations
28
PIT MUTATORS SAMPLE
Name Example source Result
Conditionals Boundary > >=
Negate Conditionals == !=
Remove Conditionals foo == bar true
Math + -
Increments foo++ foo--
Invert Negatives -foo foo
Inline Constant static final FOO= 42 static final FOO = 43
Return Values return true return false
Void Method Call System.out.println("foo")
Non Void Method Call long t = System.currentTimeMillis() long t = 0
Constructor Call Date d = new Date() Date d = null;
@nicolas_frankel #mutationtesting
29
IMPORTANT MUTATORS
@nicolas_frankel #mutationtesting
Conditionals Boundary
• Potential serious bug hiding
there
• if (foo > bar)
30
IMPORTANT MUTATORS
@nicolas_frankel #mutationtesting
 Void Method Call
• Assert.checkNotNull()
• connection.close()
31
REMEMBER
@nicolas_frankel #mutationtesting
 It’s not because the IDE
generates code safely that
it will never change
• equals()
• hashCode()
32
ENOUGH TALK…
@nicolas_frankel #mutationtesting
33
SH… HAPPENS
@nicolas_frankel #mutationtesting
 False positives
 Imperfect
 Sloooooooooooooooow
34
PIT IS IMPERFECT
if (p < 0)
...
// changed condition boundary -> survived:
if (p > 0)
...
return 0;
@nicolas_frankel #mutationtesting
35
PIT IS IMPERFECT
void rebootMachine() {
// removed method call:
checkUserPermissions();
Runtime.getRuntime().exec("reboot");
}
@nicolas_frankel #mutationtesting
36
WHY SO SLOW?
@nicolas_frankel #mutationtesting
Analyze test code
For each class under test
• For each mutator
• Create mutation
• For each mutation
• Run test
• Analyze result
• Aggregate results
37
WORKAROUNDS
Increase number of threads  
Set a limited a set of mutators
Limit scope of target classes
Limit number of tests
Limit dependency distance
Use incremental analysis  
Don’t bind to the test phase  
Use scmMutationCoverage
@nicolas_frankel #mutationtesting
38
INCREMENTAL ANALYSIS
 Metadata stored between runs
 Mutant will not be checked again, if:
• killed, and neither class nor test have changed
• survived, and there are no new/changed tests for it
@nicolas_frankel #mutationtesting
39
THE SILVER BULLET?
@nicolas_frankel #mutationtesting
Checks the relevance of
your unit tests
Points out potential bugs
40
THE REST IS UP TO YOU
@nicolas_frankel #mutationtesting
Validate the assembled application
• Integration Testing
Check the performance
• Performance Testing
Look out for display bugs
• End-to-end testing
Etc.
41
TESTING IS ABOUT ROI
@nicolas_frankel #mutationtesting
Don’t test to achieve 100%
coverage
Test because it saves money
in the long run
Prioritize:
• Business-critical code
• Complex code
42
Q&A
@nicolas_frankel #mutationtesting
http://blog.frankel.ch/
@nicolas_frankel
https://git.io/vznQK
43

More Related Content

PPTX
Voxxed Days Athens - Improve your tests with Mutation Testing
PPTX
Craft-Conf - Improve your Tests with Mutation Testing
PPTX
Javantura v3 - Mutation Testing for everyone – Nicolas Fränkel
PPTX
TestCon Europe - Mutation Testing to the Rescue of Your Tests
PPTX
DevExperience - Improve your tests with mutation testing
PPTX
Codemotion Berlin - Improve your tests with Mutation Testing
PPTX
Java Dominicano - Mutation testing
PPTX
GeeCON - Improve your tests with Mutation Testing
Voxxed Days Athens - Improve your tests with Mutation Testing
Craft-Conf - Improve your Tests with Mutation Testing
Javantura v3 - Mutation Testing for everyone – Nicolas Fränkel
TestCon Europe - Mutation Testing to the Rescue of Your Tests
DevExperience - Improve your tests with mutation testing
Codemotion Berlin - Improve your tests with Mutation Testing
Java Dominicano - Mutation testing
GeeCON - Improve your tests with Mutation Testing

Similar to ConFoo - Improve your tests with mutation testing (10)

PPTX
Codemash - Mutation testing to the rescue of your tests
PPTX
Joker - Improve your tests with mutation testing
PPTX
I.T.A.K.E Unconference - Mutation testing to the rescue of your tests
PDF
Mutation Testing.pdf
PDF
How to quickly add a safety net to a legacy codebase
PDF
Testing, Learning and Professionalism — 20171214
PDF
Must.Kill.Mutants. Agile Testing Days 2017
PDF
des mutants dans le code.pdf
PDF
The Little Shop of TDD Horrors
PDF
Mutation testing pixels camp 2019
Codemash - Mutation testing to the rescue of your tests
Joker - Improve your tests with mutation testing
I.T.A.K.E Unconference - Mutation testing to the rescue of your tests
Mutation Testing.pdf
How to quickly add a safety net to a legacy codebase
Testing, Learning and Professionalism — 20171214
Must.Kill.Mutants. Agile Testing Days 2017
des mutants dans le code.pdf
The Little Shop of TDD Horrors
Mutation testing pixels camp 2019
Ad

More from Nicolas Fränkel (20)

PPTX
SnowCamp - Adding search to a legacy application
PPTX
Un CV de dévelopeur toujours a jour
PPTX
Zero-downtime deployment on Kubernetes with Hazelcast
PDF
jLove - A Change-Data-Capture use-case: designing an evergreen cache
PPTX
BigData conference - Introduction to stream processing
PPTX
ADDO - Your own Kubernetes controller, not only in Go
PPTX
OSCONF Jaipur - A Hitchhiker's Tour to Containerizing a Java application
PPTX
GeekcampSG 2020 - A Change-Data-Capture use-case: designing an evergreen cache
PPTX
JavaDay Istanbul - 3 improvements in your microservices architecture
PPTX
OSCONF Hyderabad - Shorten all URLs!
PPTX
Devclub.lv - Introduction to stream processing
PPTX
OSCONF Koshi - Zero downtime deployment with Kubernetes, Flyway and Spring Boot
PPTX
JOnConf - A CDC use-case: designing an Evergreen Cache
PPTX
London In-Memory Computing Meetup - A Change-Data-Capture use-case: designing...
PPTX
JUG Tirana - Introduction to data streaming
PPTX
Java.IL - Your own Kubernetes controller, not only in Go!
PPTX
vJUG - Introduction to data streaming
PPTX
London Java Community - An Experiment in Continuous Deployment of JVM applica...
PPTX
OSCONF - Your own Kubernetes controller: not only in Go
PPTX
vKUG - Migrating Spring Boot apps from annotation-based config to Functional
SnowCamp - Adding search to a legacy application
Un CV de dévelopeur toujours a jour
Zero-downtime deployment on Kubernetes with Hazelcast
jLove - A Change-Data-Capture use-case: designing an evergreen cache
BigData conference - Introduction to stream processing
ADDO - Your own Kubernetes controller, not only in Go
OSCONF Jaipur - A Hitchhiker's Tour to Containerizing a Java application
GeekcampSG 2020 - A Change-Data-Capture use-case: designing an evergreen cache
JavaDay Istanbul - 3 improvements in your microservices architecture
OSCONF Hyderabad - Shorten all URLs!
Devclub.lv - Introduction to stream processing
OSCONF Koshi - Zero downtime deployment with Kubernetes, Flyway and Spring Boot
JOnConf - A CDC use-case: designing an Evergreen Cache
London In-Memory Computing Meetup - A Change-Data-Capture use-case: designing...
JUG Tirana - Introduction to data streaming
Java.IL - Your own Kubernetes controller, not only in Go!
vJUG - Introduction to data streaming
London Java Community - An Experiment in Continuous Deployment of JVM applica...
OSCONF - Your own Kubernetes controller: not only in Go
vKUG - Migrating Spring Boot apps from annotation-based config to Functional
Ad

Recently uploaded (20)

PPTX
Big Data Technologies - Introduction.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
Spectroscopy.pptx food analysis technology
PDF
Electronic commerce courselecture one. Pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
KodekX | Application Modernization Development
PPTX
Cloud computing and distributed systems.
PDF
cuic standard and advanced reporting.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Empathic Computing: Creating Shared Understanding
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Understanding_Digital_Forensics_Presentation.pptx
Big Data Technologies - Introduction.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
The AUB Centre for AI in Media Proposal.docx
Building Integrated photovoltaic BIPV_UPV.pdf
Spectroscopy.pptx food analysis technology
Electronic commerce courselecture one. Pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
KodekX | Application Modernization Development
Cloud computing and distributed systems.
cuic standard and advanced reporting.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Spectral efficient network and resource selection model in 5G networks
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Empathic Computing: Creating Shared Understanding
Encapsulation_ Review paper, used for researhc scholars
20250228 LYD VKU AI Blended-Learning.pptx
Understanding_Digital_Forensics_Presentation.pptx

ConFoo - Improve your tests with mutation testing