Can
You
Trust
Your
Tests?
2015 Vaidas Pilkauskas & Tadas Ščerbinskas
Van Halen
Band Tour Rider
Van Halen’s 1982
Tour Rider
Vaidas Pilkauskas and Tadas Ščerbinskas - Can you trust your tests?
Agenda
1. Test quality & code coverage
2. Mutation testing in theory
3. Mutation testing in practice
Prod vs. Test code quality
Code has bugs.
Tests are code.
Tests have bugs.
Vaidas Pilkauskas and Tadas Ščerbinskas - Can you trust your tests?
Test quality
Readable
Focused
Concise
Well named
“Program testing can be used to show
the presence of bugs, but never to
show their absence!”
- Edsger W. Dijkstra
Code Coverage
Types of Code Coverage
Lines
Branches
Instructions
Cyclomatic Complexity
Methods
& more
Vaidas Pilkauskas and Tadas Ščerbinskas - Can you trust your tests?
Vaidas Pilkauskas and Tadas Ščerbinskas - Can you trust your tests?
Lines
boolean doFoo() {
return "a" + "b";
}
assertThat(a.doFoo(), is("ab"));
Lines
boolean doFoo(boolean arg) {
return arg ? "a" : "b";
}
assertThat(a.doFoo(true), is("a"));
SUCCESS: 26/26 (100%) Tests passed
Branches
boolean doFoo(boolean arg) {
return arg ? "a" : "b";
}
assertThat(a.doFoo(true), is("a"));
assertThat(a.doFoo(false), is("b"));
Can you trust 100% coverage?
Code coverage can only show what is not tested.
For interpreted languages 100% code coverage is
kind of like full compilation.
Code Coverage can be gamed
On purpose or by accident
Mutation testing
Mutation testing
Changes your program code and
expects your tests to fail.
Wait! What exactly is a mutation?
def isFoo(a) {
return a == foo;
}
def isFoo(a) {
return a != foo;
}
def isFoo(a) {
return true;
}
def isFoo(a) {
return null;
}
>
>
>
Terminology
Applying a mutation to some code creates a mutant.
If test passes - mutant has survived.
If test fails - mutant is killed.
Failing is the new
passing
array = [a, b, c]
max(array) == ???
//test
max([0]) == 0
//implementation
max(a) {
return 0
}
//test
max([0]) == 0
max([1]) == 1
//implementation
max(a) {
return a.first
}
//test
max([0]) == 0
max([1]) == 1
max([0, 2] == 2
//implementation
max(a) {
m = a.first
for (e in a)
if (e > m)
m = e
return m
}
Coverage
Mutation
// test
max([0]) == 0
max([1]) == 1
max([0, 2] == 2
// implementation
max(a) {
m = a.first
for (e in a)
if (e > m)
m = e
return m
}
Mutation
// test
max([0]) == 0
max([1]) == 1
max([0, 2] == 2
// implementation
max(a) {
m = a.first
for (e in a)
if (true)
m = e
return m
}
// test
max([0]) == 0
max([1]) == 1
max([0, 2]) == 2
// implementation
max(a) {
return a.last
}
Baby steps matter
// test
max([0]) == 0
max([1]) == 1
max([0, 2]) == 2
max([2, 1]) == 2
// implementation
max(a) {
m = a.first
for (e in a)
if (e > m)
m = e
}
Tests’ effectiveness is measured by
number of killed mutants by your
test suite.
It’s like hiring a white-hat hacker to try to break into
your server and making sure you detect it.
What if mutant survives
● Simplify your code
● Add additional tests
● TDD - minimal amount of code to pass the
test
Challenges
1. High computation cost - slow
2. Equivalent mutants - false negatives
3. Infinite loops
Equivalent mutations
// Original
int i = 0;
while (i != 10) {
doSomething();
i += 1;
}
// Mutant
int i = 0;
while (i < 10) {
doSomething();
i += 1;
}
Infinite Runtime
// Original
while (expression)
doSomething();
// Mutant
while (true)
doSomething();
Disadvantages
● Can slow down your TDD rhythm
● May be very noisy
Let’s say we have codebase with:
● 300 classes
● around 10 tests per class
● 1 test runs around 1ms
● total test suite runtime is about 3s
Is it really slow?
Let’s do 10 mutations per class
● We get 3000 (300 * 10) mutations
● runtime with all mutations is
150 minutes (3s * 3000)
Speeding it up
Run only tests that cover the mutation
● 300 classes
● 10 tests per class
● 10 mutations per class
● 1ms test runtime
● total mutation runtime
10 * 10 * 1 * 300 = 30s
Speeding it up
During development run tests that cover
only your current changes
Usage scenarios
● Continuous integration
● TDD with mutation testing only on new
changes
● Add mutation testing to your legacy
project, but do not fail a build -
produce warning report
Tools
● Ruby - Mutant
● Java - PIT
● And many tools for other languages
Summary
● Code coverage highlights code that is
definitely not tested
● Mutation testing highlights code that
definitely is tested
● Given non equivalent mutations, good test
suite should work the same as a hash
function
About us
Vaidas Pilkauskas
@liucijus
● Vilnius JUG co-
founder
● Vilnius Scala leader
● Coderetreat facilitator
● Mountain bicycle
rider
● Snowboarder
Tadas Ščerbinskas
@tadassce
● VilniusRB co-
organizer
● RubyConfLT co-
organizer
● RailsGirls Vilnius &
Berlin coach
● Various board sports’
enthusiast
Credits
A lot of presentation content is based on
work by these guys
● Markus Schirp - author of Mutant
● Henry Coles - author of PIT
● Filip Van Laenen - working on a book
Q&A

More Related Content

PDF
Can You Trust Your Tests? (Agile Tour 2015 Kaunas)
PDF
Best practices for unit testing RxJava
PDF
F.Nalivaika - Kodėl testavimas yra bevertis ir kaip tai galima pakeisti
PDF
Baby steps to Domain-Driven Design
PPTX
ConFoo - Improve your tests with mutation testing
PPTX
Voxxed Days Athens - Improve your tests with Mutation Testing
PPTX
Javantura v3 - Mutation Testing for everyone – Nicolas Fränkel
PDF
Must.kill.mutants. TopConf Tallinn 2016
Can You Trust Your Tests? (Agile Tour 2015 Kaunas)
Best practices for unit testing RxJava
F.Nalivaika - Kodėl testavimas yra bevertis ir kaip tai galima pakeisti
Baby steps to Domain-Driven Design
ConFoo - Improve your tests with mutation testing
Voxxed Days Athens - Improve your tests with Mutation Testing
Javantura v3 - Mutation Testing for everyone – Nicolas Fränkel
Must.kill.mutants. TopConf Tallinn 2016

Similar to Vaidas Pilkauskas and Tadas Ščerbinskas - Can you trust your tests? (20)

PDF
Mutation Testing
PPTX
Kill the mutants and test your tests - Roy van Rijn
PDF
Kill the mutants - A better way to test your tests
PPTX
Craft-Conf - Improve your Tests with Mutation Testing
PPTX
Joker - Improve your tests with mutation testing
PPTX
I.T.A.K.E Unconference - Mutation testing to the rescue of your tests
PPTX
GeeCON - Improve your tests with Mutation Testing
PDF
Mutation Testing with PIT (Booster 2014, 2014-MAR-13)
PDF
From jUnit to Mutationtesting
PDF
Mutation Testing.pdf
PPT
Mutation Testing and MuJava
PDF
SAIConference_PAPER
PPTX
Mateusz Bryła - Mutation testing
PPTX
Codemash - Mutation testing to the rescue of your tests
PPTX
TestCon Europe - Mutation Testing to the Rescue of Your Tests
PDF
Introduction to Mutation Testing
PDF
50120140502017
PPTX
Mutation Testing - Ruby Edition
PPTX
DevExperience - Improve your tests with mutation testing
PDF
Mutation Testing: Start Hunting The Bugs
Mutation Testing
Kill the mutants and test your tests - Roy van Rijn
Kill the mutants - A better way to test your tests
Craft-Conf - Improve your Tests with Mutation Testing
Joker - Improve your tests with mutation testing
I.T.A.K.E Unconference - Mutation testing to the rescue of your tests
GeeCON - Improve your tests with Mutation Testing
Mutation Testing with PIT (Booster 2014, 2014-MAR-13)
From jUnit to Mutationtesting
Mutation Testing.pdf
Mutation Testing and MuJava
SAIConference_PAPER
Mateusz Bryła - Mutation testing
Codemash - Mutation testing to the rescue of your tests
TestCon Europe - Mutation Testing to the Rescue of Your Tests
Introduction to Mutation Testing
50120140502017
Mutation Testing - Ruby Edition
DevExperience - Improve your tests with mutation testing
Mutation Testing: Start Hunting The Bugs
Ad

More from Agile Lietuva (20)

PPTX
Agile Pusryčiai 2023 - „Skaitmeninė transformacija viešajame sektoriuje: nuo ...
PPTX
Agile Pusryčiai 2023 - „Kaip užsitikrinti projekto sėkmę dar iki projekto pra...
PPTX
Agile pusryčiai 2023 - „Pirštas ant projekto pulso: CPO LT Agile patirtis ir ...
PPTX
Agile Pusryčiai 2023 - „Viešasis sektorius – neatskleistas inovacijų paklauso...
PPTX
M. Kaminskas ir A. K. Remeikienė. LEAN projektas: sėkmės istorijos, iššūkiai ...
PDF
B. den Haak. How to make OKRs Lean Again
PDF
D. Aitcheson. How to make forecasts that are actually accurate.
PDF
Aleksandra Černiauskienė. Misija Bloomberg: Agile pagal amerikiečius
PDF
Maija Aniskovič. Agile įtaka komandos motyvacijai.
PDF
dr. E. Janiūnienė. Asociacijos Agile Lietuva atlikto Agile tyrimo pristatymas
PPTX
M. Aniskovič. Laužome stereotipus: Agile gali drąsiai taikyti visi
PPTX
R. Krukonis. Reikalingas greitas rezultatas – pakeiskime projekto darbų organ...
PPTX
M. Jovaišas. Viešojo sektoriaus lankstumas įgyvendinant transformacijas
PPTX
A. Kovaliov. Kas nėra Agile jaunystėje, tas neturi širdies. Kas nėra Watefall...
PDF
V. Vasiliauskas. Nestandartinis atvejis: nuo Kanban prie Scrum
PDF
Leonard Vorobej. Agile projektų valdymas pradedantiesiems
PDF
Giedrė Žemulaitytė. Agile personalo skyriaus valdyme
PDF
Gabija Fatėnaitė. Agile ir Scrum turinio kūrimo ir marketingo komandose
PPTX
Gediminas Milieška. Agile kelionės: nuo transformacijos iki planavimo dideliu...
PPT
Denis Vanpoucke. Agile kelionės:nuo transformacijos iki planavimo dideliu mastu
Agile Pusryčiai 2023 - „Skaitmeninė transformacija viešajame sektoriuje: nuo ...
Agile Pusryčiai 2023 - „Kaip užsitikrinti projekto sėkmę dar iki projekto pra...
Agile pusryčiai 2023 - „Pirštas ant projekto pulso: CPO LT Agile patirtis ir ...
Agile Pusryčiai 2023 - „Viešasis sektorius – neatskleistas inovacijų paklauso...
M. Kaminskas ir A. K. Remeikienė. LEAN projektas: sėkmės istorijos, iššūkiai ...
B. den Haak. How to make OKRs Lean Again
D. Aitcheson. How to make forecasts that are actually accurate.
Aleksandra Černiauskienė. Misija Bloomberg: Agile pagal amerikiečius
Maija Aniskovič. Agile įtaka komandos motyvacijai.
dr. E. Janiūnienė. Asociacijos Agile Lietuva atlikto Agile tyrimo pristatymas
M. Aniskovič. Laužome stereotipus: Agile gali drąsiai taikyti visi
R. Krukonis. Reikalingas greitas rezultatas – pakeiskime projekto darbų organ...
M. Jovaišas. Viešojo sektoriaus lankstumas įgyvendinant transformacijas
A. Kovaliov. Kas nėra Agile jaunystėje, tas neturi širdies. Kas nėra Watefall...
V. Vasiliauskas. Nestandartinis atvejis: nuo Kanban prie Scrum
Leonard Vorobej. Agile projektų valdymas pradedantiesiems
Giedrė Žemulaitytė. Agile personalo skyriaus valdyme
Gabija Fatėnaitė. Agile ir Scrum turinio kūrimo ir marketingo komandose
Gediminas Milieška. Agile kelionės: nuo transformacijos iki planavimo dideliu...
Denis Vanpoucke. Agile kelionės:nuo transformacijos iki planavimo dideliu mastu
Ad

Recently uploaded (20)

PPT
L2 - Determinants and Dimensions of Culture.ppt
PPTX
Org SmartArt Infographics: a simple way to create your org chart
PPTX
HLA_Poland presentation about HOP training
PPTX
Company Presentation for a company for you to look at the company for ppt
PDF
Sales Enablement in the Age of AI: Unlocking Growth
PPT
30.-Communication-skills-for-highly-effective-teachers.ppt
PDF
Peter Oeij - Industry 5.0 for shaping sustainable and inclusive futures
PPTX
Women talent hub 😁🥳🤗🤗🎉♊😭👌😊😍🤣😆😁🥰😍
PPTX
People Strategies LXP BTS Readiness Plan
PPTX
AWAKE-Redefining-Luxury-Through-Sustainability - Copy (1).pptx
PDF
The Pearson Complete Course for CISM Certification: Unit 4
PDF
Improvement_Proposal_DMAIC dan Tools yang digunakan
PPTX
Supply Chain Management in Leadership.pptx
PPTX
Tracii Hutsona A Story Of Business Success, Resilience, And Giving Back
PDF
5 Insights from Maharashtrian of the Year Award Winners
PDF
Jim Kaskade CV Resume multidisciplinary business leader 082825
PDF
wadi marwa ppt in detail,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
PPTX
4 Ways To Create More Value For Meetings.pptx
PPTX
EXT.-EDU-809-ACCOUNTABILITY-IN-EXTENSION.pptx
PDF
Patrick Wentland_ The Power of Leadership in Driving Organizational Growth.pdf
L2 - Determinants and Dimensions of Culture.ppt
Org SmartArt Infographics: a simple way to create your org chart
HLA_Poland presentation about HOP training
Company Presentation for a company for you to look at the company for ppt
Sales Enablement in the Age of AI: Unlocking Growth
30.-Communication-skills-for-highly-effective-teachers.ppt
Peter Oeij - Industry 5.0 for shaping sustainable and inclusive futures
Women talent hub 😁🥳🤗🤗🎉♊😭👌😊😍🤣😆😁🥰😍
People Strategies LXP BTS Readiness Plan
AWAKE-Redefining-Luxury-Through-Sustainability - Copy (1).pptx
The Pearson Complete Course for CISM Certification: Unit 4
Improvement_Proposal_DMAIC dan Tools yang digunakan
Supply Chain Management in Leadership.pptx
Tracii Hutsona A Story Of Business Success, Resilience, And Giving Back
5 Insights from Maharashtrian of the Year Award Winners
Jim Kaskade CV Resume multidisciplinary business leader 082825
wadi marwa ppt in detail,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4 Ways To Create More Value For Meetings.pptx
EXT.-EDU-809-ACCOUNTABILITY-IN-EXTENSION.pptx
Patrick Wentland_ The Power of Leadership in Driving Organizational Growth.pdf

Vaidas Pilkauskas and Tadas Ščerbinskas - Can you trust your tests?