Top Banner
Can You Trust Your Tests? 2015 Vaidas Pilkauskas & Tadas Ščerbinskas
50

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

Apr 16, 2017

Download

Agile Lietuva
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Vaidas Pilkauskas and Tadas Ščerbinskas - Can you trust your tests?

CanYou Trust Your Tests?

2015 Vaidas Pilkauskas & Tadas Ščerbinskas

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

Van Halen

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

Band Tour Rider

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

Van Halen’s 1982 Tour Rider

Page 5: Vaidas Pilkauskas and Tadas Ščerbinskas - Can you trust your tests?
Page 6: Vaidas Pilkauskas and Tadas Ščerbinskas - Can you trust your tests?

Agenda

1. Test quality & code coverage2. Mutation testing in theory3. Mutation testing in practice

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

Prod vs. Test code quality

Code has bugs.Tests are code.

Tests have bugs.

Page 8: Vaidas Pilkauskas and Tadas Ščerbinskas - Can you trust your tests?
Page 9: Vaidas Pilkauskas and Tadas Ščerbinskas - Can you trust your tests?

Test quality

ReadableFocusedConcise

Well named

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

“Program testing can be used to show the presence of bugs, but never to

show their absence!”- Edsger W. Dijkstra

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

Code Coverage

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

Types of Code Coverage

LinesBranches

InstructionsCyclomatic Complexity

Methods& more

Page 13: Vaidas Pilkauskas and Tadas Ščerbinskas - Can you trust your tests?
Page 14: Vaidas Pilkauskas and Tadas Ščerbinskas - Can you trust your tests?
Page 15: Vaidas Pilkauskas and Tadas Ščerbinskas - Can you trust your tests?

Lines

boolean doFoo() { return "a" + "b";}

assertThat(a.doFoo(), is("ab"));

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

Lines

boolean doFoo(boolean arg) { return arg ? "a" : "b";}

assertThat(a.doFoo(true), is("a"));

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

SUCCESS: 26/26 (100%) Tests passed

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

Branches

boolean doFoo(boolean arg) { return arg ? "a" : "b";}

assertThat(a.doFoo(true), is("a"));

assertThat(a.doFoo(false), is("b"));

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

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.

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

Code Coverage can be gamed

On purpose or by accident

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

Mutation testing

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

Mutation testing

Changes your program code and expects your tests to fail.

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

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;}

>>>

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

Terminology

Applying a mutation to some code creates a mutant.If test passes - mutant has survived.

If test fails - mutant is killed.

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

Failing is the new passing

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

array = [a, b, c]

max(array) == ???

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

//testmax([0]) == 0

//implementationmax(a) { return 0}

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

//testmax([0]) == 0max([1]) == 1

//implementationmax(a) { return a.first}

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

//testmax([0]) == 0max([1]) == 1max([0, 2] == 2

//implementationmax(a) { m = a.first for (e in a) if (e > m) m = e return m}

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

Coverage

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

Mutation// testmax([0]) == 0max([1]) == 1max([0, 2] == 2

// implementationmax(a) { m = a.first for (e in a) if (e > m) m = e return m}

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

Mutation// testmax([0]) == 0max([1]) == 1max([0, 2] == 2

// implementationmax(a) { m = a.first for (e in a) if (true) m = e return m}

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

// testmax([0]) == 0max([1]) == 1max([0, 2]) == 2

// implementationmax(a) { return a.last}

Baby steps matter

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

// testmax([0]) == 0max([1]) == 1max([0, 2]) == 2max([2, 1]) == 2

// implementationmax(a) { m = a.first for (e in a) if (e > m) m = e}

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

Tests’ effectiveness is measured by number of killed mutants by your

test suite.

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

It’s like hiring a white-hat hacker to try to break into your server and making sure you detect it.

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

What if mutant survives

● Simplify your code● Add additional tests● TDD - minimal amount of code to pass the

test

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

Challenges

1. High computation cost - slow2. Equivalent mutants - false negatives3. Infinite loops

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

Equivalent mutations

// Originalint i = 0;while (i != 10) { doSomething(); i += 1;}

// Mutantint i = 0;while (i < 10) { doSomething(); i += 1;}

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

Infinite Runtime

// Originalwhile (expression) doSomething();

// Mutantwhile (true) doSomething();

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

Disadvantages

● Can slow down your TDD rhythm● May be very noisy

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

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)

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

Speeding it upRun 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

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

Speeding it upDuring development run tests that cover only your current changes

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

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

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

Tools● Ruby - Mutant● Java - PIT● And many tools for other languages

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

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

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

About usVaidas 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

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

CreditsA 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

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

Q&A