Slide 1

Slide 1 text

Mutation Test Joseph Yao A Way to Learn How Good Your Unit Test is

Slide 2

Slide 2 text

Who I am? n Agile Coach at Odd-e n Father, Husband... n Software Craftsman

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Mutant

Slide 5

Slide 5 text

Mutation Test Mutant causes any test failed? Killed Mutant Yes Survived Mutant No

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

Expectation of Automated Test Any Possible “Bad” Code Change can be Detected

Slide 8

Slide 8 text

int foo (int x, int y)! {! int z = 0;! if ((x>0) && (y>0)) {! z = x;! }! return z;! } int foo (int x, int y)! {! int z = 0;! if ((x>0) && (y>=0)) {! z = x;! }! return z;! } Unit Tests assertEquals(2, foo(2, 2)) assertEquals(0, foo(2, -1)) assertEquals(0, foo(-1, 2)) To assertEquals(2, foo(2, 2)) assertEquals(0, foo(2, 0)) assertEquals(0, foo(-1, 2))

Slide 9

Slide 9 text

int foo (int x, int y)! {! int z = 0;! if ((x>0) && (y>0)) {! z = x;! }! sideEffect(z);! return z;! } int foo (int x, int y)! {! int z = 0;! if ((x>0) && (y>0)) {! z = x;! }! sideEffect(z);! return z;! } n Either missing a test to prove the removed code is needed n Or the removed code is truly Redundant

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Three Rules of TDD n You are not allowed to write any production code unless it is to make a failing unit test pass. n You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures. n You are not allowed to write any more production code than is sufficient to pass the one failing unit test.

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

Survived Mutant

Slide 14

Slide 14 text

Problematic Test

Slide 15

Slide 15 text

Mutants n Conditionals Boundary n Negate Conditionals n Math n Increments n Invert Negatives n Inline Constant n Return Values n Void Method Calls n Non Void Method Calls n Constructor Calls More detail at http://pitest.org/quickstart/mutators/

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

NOT

Slide 18

Slide 18 text

Equivalent Mutant >= >

Slide 19

Slide 19 text

Mutation Test Tool

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

http://crestweb.cs.ucl.ac.uk/resources/ mutation_testing_repository/TR-09-06.pdf

Slide 22

Slide 22 text

Q & A Email: [email protected] Kata Video: http://tudou.com/home/yaoruozhou Github: https://github.com/JosephYao