SlideShare a Scribd company logo
1 of 54
Download to read offline
Can
You
Trust
Your
Tests?
2015 Vaidas Pilkauskas & Tadas Ščerbinskas
Van Halen
Band Tour Rider
Van Halen’s 1982
Tour Rider
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.
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
Lines
string foo() {
return "a" + "b"
}
assertThat(a.foo(), is("ab"))
Lines
string foo(boolean arg) {
return arg ? "a" : "b"
}
assertThat(a.foo(true), is("a"))
Branches
string foo(boolean arg) {
return arg ? "a" : "b"
}
assertThat(a.foo(true), is("a"))
assertThat(a.foo(false), is("b"))
SUCCESS: 26/26 (100%) Tests passed
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.
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 ✘
// test
max([0]) == 0 ✔
// implementation
max(a) {
return 0
}
// test
max([0]) == 0 ✔
max([1]) == 1 ✘
// 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) {
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
}
// test
max([0]) == 0 ✔
max([1]) == 1 ✔
max([0, 2]) == 2 ✔
max([2, 1]) == 2 ✘
// implementation
max(a) {
return a.last
}
// implementation
max(a) {
m = a.first
for (e in a)
if (e > m)
m = e
return m
}
// test
max([0]) == 0 ✔
max([1]) == 1 ✔
max([0, 2]) == 2 ✔
max([2, 1]) == 2 ✔
Baby steps matter
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
● 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
Usage scenarios
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
Vaidas Pilkauskas
@liucijus
● Vilnius JUG co-founder
● Vilnius Scala leader
● Coderetreat facilitator
● Mountain bicycle rider
● Snowboarder
About us
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

Viewers also liked

Beat the Red Eye - 100+ IDEAS!
Beat the Red Eye - 100+ IDEAS!Beat the Red Eye - 100+ IDEAS!
Beat the Red Eye - 100+ IDEAS!
ethelkatrina
 
Mahindra & Mahindra Tractors In Usa
Mahindra & Mahindra Tractors In UsaMahindra & Mahindra Tractors In Usa
Mahindra & Mahindra Tractors In Usa
ANSHU TIWARI
 

Viewers also liked (20)

Group 8. part a
Group 8. part aGroup 8. part a
Group 8. part a
 
Performance indicators for different levels of management
Performance indicators for different levels of managementPerformance indicators for different levels of management
Performance indicators for different levels of management
 
Time management
Time managementTime management
Time management
 
Comp 220 lab 3
Comp 220 lab 3Comp 220 lab 3
Comp 220 lab 3
 
kelly marulanda
kelly marulandakelly marulanda
kelly marulanda
 
Performance indicators for different levels of management
Performance indicators for different levels of managementPerformance indicators for different levels of management
Performance indicators for different levels of management
 
norhane ramdani
norhane ramdaninorhane ramdani
norhane ramdani
 
Závěrečný úkol KPI
Závěrečný úkol KPIZávěrečný úkol KPI
Závěrečný úkol KPI
 
Coderetreat introduction
Coderetreat introductionCoderetreat introduction
Coderetreat introduction
 
Week 6 project
Week 6 projectWeek 6 project
Week 6 project
 
Collaborative Composition Histories
Collaborative Composition HistoriesCollaborative Composition Histories
Collaborative Composition Histories
 
Croutons.org
Croutons.orgCroutons.org
Croutons.org
 
Venture Lab Creativity: Paying Attention
Venture Lab Creativity: Paying Attention Venture Lab Creativity: Paying Attention
Venture Lab Creativity: Paying Attention
 
Wallopball
WallopballWallopball
Wallopball
 
Beat the Red Eye - 100+ IDEAS!
Beat the Red Eye - 100+ IDEAS!Beat the Red Eye - 100+ IDEAS!
Beat the Red Eye - 100+ IDEAS!
 
CIS 247C iLab 4 of 7: Composition and Class Interfaces
CIS 247C iLab 4 of 7: Composition and Class Interfaces  CIS 247C iLab 4 of 7: Composition and Class Interfaces
CIS 247C iLab 4 of 7: Composition and Class Interfaces
 
London Sightseeing Tour Places of interest
London Sightseeing Tour Places of interestLondon Sightseeing Tour Places of interest
London Sightseeing Tour Places of interest
 
Negotiation fundamentals
Negotiation fundamentalsNegotiation fundamentals
Negotiation fundamentals
 
Akka Unit Testing
Akka Unit TestingAkka Unit Testing
Akka Unit Testing
 
Mahindra & Mahindra Tractors In Usa
Mahindra & Mahindra Tractors In UsaMahindra & Mahindra Tractors In Usa
Mahindra & Mahindra Tractors In Usa
 

Similar to Can You Trust Your Tests? (Agile Tour 2015 Kaunas)

Unit Testing Presentation
Unit Testing PresentationUnit Testing Presentation
Unit Testing Presentation
nicobn
 
Test Driven Development in Python
Test Driven Development in PythonTest Driven Development in Python
Test Driven Development in Python
Anoop Thomas Mathew
 

Similar to Can You Trust Your Tests? (Agile Tour 2015 Kaunas) (20)

Mutation Testing: Start Hunting The Bugs
Mutation Testing: Start Hunting The BugsMutation Testing: Start Hunting The Bugs
Mutation Testing: Start Hunting The Bugs
 
STAMP Descartes Presentation
STAMP Descartes PresentationSTAMP Descartes Presentation
STAMP Descartes Presentation
 
Refactoring Ruby Code
Refactoring Ruby CodeRefactoring Ruby Code
Refactoring Ruby Code
 
Shift-Left Testing: QA in a DevOps World by David Laulusa
Shift-Left Testing: QA in a DevOps World by David LaulusaShift-Left Testing: QA in a DevOps World by David Laulusa
Shift-Left Testing: QA in a DevOps World by David Laulusa
 
Mutation @ Spotify
Mutation @ Spotify Mutation @ Spotify
Mutation @ Spotify
 
Change Software Like a Scientist
Change Software Like a ScientistChange Software Like a Scientist
Change Software Like a Scientist
 
Optimization in Programming languages
Optimization in Programming languagesOptimization in Programming languages
Optimization in Programming languages
 
Unit Testing Presentation
Unit Testing PresentationUnit Testing Presentation
Unit Testing Presentation
 
Simple rules for building robust machine learning models
Simple rules for building robust machine learning modelsSimple rules for building robust machine learning models
Simple rules for building robust machine learning models
 
Actor Concurrency
Actor ConcurrencyActor Concurrency
Actor Concurrency
 
Test Driven Development in Python
Test Driven Development in PythonTest Driven Development in Python
Test Driven Development in Python
 
Advanced patterns in asynchronous programming
Advanced patterns in asynchronous programmingAdvanced patterns in asynchronous programming
Advanced patterns in asynchronous programming
 
Mutation Testing: Testing your tests
Mutation Testing: Testing your testsMutation Testing: Testing your tests
Mutation Testing: Testing your tests
 
Tdd guide
Tdd guideTdd guide
Tdd guide
 
tictactoe groovy
tictactoe groovytictactoe groovy
tictactoe groovy
 
Golang dot-testing-lite
Golang dot-testing-liteGolang dot-testing-lite
Golang dot-testing-lite
 
DSR Testing (Part 1)
DSR Testing (Part 1)DSR Testing (Part 1)
DSR Testing (Part 1)
 
The Great Scala Makeover
The Great Scala MakeoverThe Great Scala Makeover
The Great Scala Makeover
 
TDD Training
TDD TrainingTDD Training
TDD Training
 
Why on Earth would I test if I have to just "Let it crash"?
Why on Earth would I test if I have to just "Let it crash"?Why on Earth would I test if I have to just "Let it crash"?
Why on Earth would I test if I have to just "Let it crash"?
 

Recently uploaded

%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 

Recently uploaded (20)

Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 

Can You Trust Your Tests? (Agile Tour 2015 Kaunas)