mutation testing

24
Mutation Testing Chris Sinjakli

Upload: chris-sinjakli

Post on 28-Jun-2015

736 views

Category:

Technology


0 download

DESCRIPTION

Brown Bag lightning talk I gave on 31st May 2012

TRANSCRIPT

Page 1: Mutation Testing

Mutation Testing

Chris Sinjakli

Page 2: Mutation Testing

Testing is a good thing

But how do we know our tests are good?

Page 3: Mutation Testing

Code coverage is a start

But it can give a “good” score with really dreadful tests

Page 4: Mutation Testing

Really dreadful testspublic int addTwoNumbers(int a, int b) {

return a – b;}

...

@Testpublic void shouldAddTwoNumbers() {

int result = addTwoNumbers(1, 1);assertTrue(true);

}Coverage: 100%Usefulness: 0

Page 5: Mutation Testing
Page 6: Mutation Testing

A contrived example

But how could we detect it?

Page 7: Mutation Testing

Mutation Testing!

“Who watches the watchmen?”

Page 8: Mutation Testing

If you can change the code, and a test doesn’t fail, either the code is never run or the tests are

wrong.

Page 9: Mutation Testing

Going with our previous examplepublic int addTwoNumbers(int a, int b) {

return a – b;}

...

@Testpublic void shouldAddTwoNumbers() {

int result = addTwoNumbers(1, 1);assertTrue(true);

}

Let’s change something

Page 10: Mutation Testing

Going with our previous examplepublic int addTwoNumbers(int a, int b) {

return a + b;}

...

@Testpublic void shouldAddTwoNumbers() {

int result = addTwoNumbers(1, 1);assertTrue(true);

}

This still passes

Page 11: Mutation Testing

So it caught a really rubbish test

How about something slightly less obvious?

Page 12: Mutation Testing

Slightly less obvious (and I mean slightly)

public int checkConditions(boolean a, boolean b) { if (a && b) {

return 42;}else {

return 0;}

}

@Testpublic void testBothFalse() {

int result = checkConditions(false, false);assertEquals(0, result);

}@Testpublic void testBothTrue () {

int result = checkConditions(true, true);assertEquals(42, result);

}

Coverage: 100%Usefulness: >0But still wrong

Page 13: Mutation Testing

Slightly less obvious (and I mean slightly)

public int checkConditions(boolean a, boolean b) { if (a && b) {

return 42;}else {

return 0;}

}

@Testpublic void testBothFalse() {

int result = checkConditions(false, false);assertEquals(0, result);

}@Testpublic void testBothTrue () {

int result = checkConditions(true, true);assertEquals(42, result);

}

Mutate

Page 14: Mutation Testing

Slightly less obvious (and I mean slightly)

public int checkConditions(boolean a, boolean b) { if (a || b) {

return 42;}else {

return 0;}

}

@Testpublic void testBothFalse() {

int result = checkConditions(false, false);assertEquals(0, result);

}@Testpublic void testBothTrue () {

int result = checkConditions(true, true);assertEquals(42, result);

}

Passing tests

Page 15: Mutation Testing

Mutation testing caught our mistake

:D

Page 16: Mutation Testing

Useful technique

But still has its flaws

Page 17: Mutation Testing

The downfall of mutation(Equivalent Mutants)

int index = 0

while (someCondition) {doStuff();index++;if (index == 100) {

break;}

}

int index = 0

while (someCondition) {doStuff();index++;if (index >= 100) {

break;}

}

Mutates to

But the programs are equivalent, so no test will fail

Page 18: Mutation Testing

Tools

Some Java, then some Ruby

Page 19: Mutation Testing

Java

• Loads of tools to choose from• Bytecode vs source mutation• Will look at PIT (seems like one of the better

ones)

Page 20: Mutation Testing

PIT - pitest.org

• Works with “everything”– Command line– Ant– Maven

• Bytecode level mutations (faster)• Very customisable

– Exclude classes/packages from mutation– Choose which mutations you want– Timeouts

• Makes pretty HTML reports (line/mutation coverage)

Page 21: Mutation Testing

Ruby

Page 22: Mutation Testing

Ruby

• Mutant seems to be the new favourite• Runs in Rubinius (1.8 or 1.9 mode)• Only supports RSpec• Easy to set up

rvm install rbx-headrvm use rbx-headgem install mutant

• And easy to usemutate “ClassName#method_to_test” spec

Page 23: Mutation Testing

Summary

• Seems like it could identify areas of weakness in our tests

• At the same time, could be very noisy• Might be worth just trying it against an

existing project and seeing what happens

Page 24: Mutation Testing

Questions?