SlideShare a Scribd company logo
Mutation Testing

   Chris Sinjakli
Testing is a good thing

But how do we know our tests are
            good?
Code coverage is a start

But it can give a “good” score with
        really dreadful tests
Really dreadful tests
public int addTwoNumbers(int a, int b) {
  return a – b;
}

...

@Test
public void shouldAddTwoNumbers() {
  int result = addTwoNumbers(1, 1);
  assertTrue(true);
}
                                           Coverage: 100%
                                           Usefulness: 0
Mutation Testing
A contrived example

But how could we detect it?
Mutation Testing!

“Who watches the watchmen?”
If you can change the code, and a
test doesn’t fail, either the code is
 never run or the tests are wrong.
Going with our previous example
public int addTwoNumbers(int a, int b) {
  return a – b;
}
            Let’s change something
...

@Test
public void shouldAddTwoNumbers() {
  int result = addTwoNumbers(1, 1);
  assertTrue(true);
}
Going with our previous example
public int addTwoNumbers(int a, int b) {
  return a + b;
}

...
                      This still passes

@Test
public void shouldAddTwoNumbers() {
  int result = addTwoNumbers(1, 1);
  assertTrue(true);
}
So it caught a really rubbish test

   How about something slightly less
              obvious?
Slightly less obvious (and I mean slightly)
public int checkConditions(boolean a, boolean b) {
    if (a && b) {
          return 42;
    }
    else {
          return 0;
    }
}

@Test
public void testBothFalse() {
    int result = checkConditions(false, false);
    assertEquals(0, result);
}
@Test
public void testBothTrue () {
    int result = checkConditions(true, true);
    assertEquals(42, result);

}                                                    Coverage: 100%
                                                     Usefulness: >0
                                                     But still wrong
Slightly less obvious (and I mean slightly)
public int checkConditions(boolean a, boolean b) {
    if (a && b) {
          return 42;
    }                   Mutate
    else {
          return 0;
    }
}

@Test
public void testBothFalse() {
    int result = checkConditions(false, false);
    assertEquals(0, result);
}
@Test
public void testBothTrue () {
    int result = checkConditions(true, true);
    assertEquals(42, result);

}
Slightly less obvious (and I mean slightly)
public int checkConditions(boolean a, boolean b) {
    if (a || b) {
          return 42;
    }
    else {
          return 0;
    }
}

@Test
public void testBothFalse() {                        Passing tests
    int result = checkConditions(false, false);
    assertEquals(0, result);
}
@Test
public void testBothTrue () {
    int result = checkConditions(true, true);
    assertEquals(42, result);

}
Mutation testing caught our
         mistake
            :D
Useful technique

 But still has its flaws
The downfall of mutation
                       (Equivalent Mutants)
int index = 0

while (someCondition) {
     doStuff();
     index++;
     if (index == 100) {
              break;
     }
}
  Mutates to
int index = 0

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

 But the programs are equivalent, so no test will fail
Tools

Some Java, then some Ruby
Java
• Loads of tools to choose from
• Bytecode vs source mutation
• Will look at PIT (seems like one of the better
  ones)
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)
Ruby
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-head
    rvm use rbx-head
    gem install mutant
• And easy to use
    mutate “ClassName#method_to_test” spec
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
Questions?

More Related Content

PPTX
Mutation Testing - Ruby Edition
PDF
Kill the mutants - A better way to test your tests
PDF
Mutation Testing
PPTX
Mutation Testing
ODP
Unit testing with Easymock
PDF
JUnit & Mockito, first steps
PPTX
Kill the mutants and test your tests - Roy van Rijn
PDF
Mutation Testing with PIT (Booster 2014, 2014-MAR-13)
Mutation Testing - Ruby Edition
Kill the mutants - A better way to test your tests
Mutation Testing
Mutation Testing
Unit testing with Easymock
JUnit & Mockito, first steps
Kill the mutants and test your tests - Roy van Rijn
Mutation Testing with PIT (Booster 2014, 2014-MAR-13)

What's hot (20)

PPTX
Building unit tests correctly
PPTX
Unit testing patterns for concurrent code
PPTX
Writing and using Hamcrest Matchers
PDF
Sample Chapter of Practical Unit Testing with TestNG and Mockito
ODP
Dependency Injection
PDF
Rechecking SharpDevelop: Any New Bugs?
PPTX
Mutation testing - the way to improve unit tests quality
PPTX
Mutation Testing: Testing your tests
PDF
From Elixir to Akka (and back) - ElixirConf Mx 2017
PDF
2013 DevFest Vienna - Bad Tests, Good Tests
PDF
Date Processing Attracts Bugs or 77 Defects in Qt 6
PPTX
Qunit Java script Un
PDF
Developer Test - Things to Know
PDF
property-based testing (FrOsCon 9, 2014, August 23)
PDF
Stop Making Excuses and Start Testing Your JavaScript
PDF
GeeCON 2012 Bad Tests, Good Tests
PDF
Confitura 2012 Bad Tests, Good Tests
PPTX
07 flow control
PDF
Junit Recipes - Elementary tests (1/2)
PDF
Checking Clang 11 with PVS-Studio
Building unit tests correctly
Unit testing patterns for concurrent code
Writing and using Hamcrest Matchers
Sample Chapter of Practical Unit Testing with TestNG and Mockito
Dependency Injection
Rechecking SharpDevelop: Any New Bugs?
Mutation testing - the way to improve unit tests quality
Mutation Testing: Testing your tests
From Elixir to Akka (and back) - ElixirConf Mx 2017
2013 DevFest Vienna - Bad Tests, Good Tests
Date Processing Attracts Bugs or 77 Defects in Qt 6
Qunit Java script Un
Developer Test - Things to Know
property-based testing (FrOsCon 9, 2014, August 23)
Stop Making Excuses and Start Testing Your JavaScript
GeeCON 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good Tests
07 flow control
Junit Recipes - Elementary tests (1/2)
Checking Clang 11 with PVS-Studio
Ad

Similar to Mutation Testing (20)

PPTX
Best practices unit testing
PPTX
Mutation testing
PDF
Mutation Testing with PIT
PDF
Mutation testing with PIT
PDF
MUTANTS KILLER - PIT: state of the art of mutation testing system
PDF
STAMP Descartes Presentation
PDF
Test driven development
PDF
MUTANTS KILLER (Revised) - PIT: state of the art of mutation testing system
PPTX
Property Based Testing
PPTX
Easy practice with if statement and boolean type
PDF
White Box Testing (Introduction to)
PDF
Mutation Testing: Start Hunting The Bugs
PPTX
Navigating the xDD Alphabet Soup
PPT
2012 JDays Bad Tests Good Tests
PDF
Transferring Software Testing Tools to Practice (AST 2017 Keynote)
PDF
DSR Testing (Part 1)
PDF
How to write clean tests
PDF
Introduction to web programming for java and c# programmers by @drpicox
PPTX
130706266060138191
PDF
Mutation @ Spotify
Best practices unit testing
Mutation testing
Mutation Testing with PIT
Mutation testing with PIT
MUTANTS KILLER - PIT: state of the art of mutation testing system
STAMP Descartes Presentation
Test driven development
MUTANTS KILLER (Revised) - PIT: state of the art of mutation testing system
Property Based Testing
Easy practice with if statement and boolean type
White Box Testing (Introduction to)
Mutation Testing: Start Hunting The Bugs
Navigating the xDD Alphabet Soup
2012 JDays Bad Tests Good Tests
Transferring Software Testing Tools to Practice (AST 2017 Keynote)
DSR Testing (Part 1)
How to write clean tests
Introduction to web programming for java and c# programmers by @drpicox
130706266060138191
Mutation @ Spotify
Ad

Recently uploaded (20)

PDF
Encapsulation theory and applications.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
1. Introduction to Computer Programming.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Spectroscopy.pptx food analysis technology
PPTX
Machine Learning_overview_presentation.pptx
PPTX
Big Data Technologies - Introduction.pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Electronic commerce courselecture one. Pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Empathic Computing: Creating Shared Understanding
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Spectral efficient network and resource selection model in 5G networks
Encapsulation theory and applications.pdf
MYSQL Presentation for SQL database connectivity
1. Introduction to Computer Programming.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Advanced methodologies resolving dimensionality complications for autism neur...
Spectroscopy.pptx food analysis technology
Machine Learning_overview_presentation.pptx
Big Data Technologies - Introduction.pptx
MIND Revenue Release Quarter 2 2025 Press Release
Dropbox Q2 2025 Financial Results & Investor Presentation
Mobile App Security Testing_ A Comprehensive Guide.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Electronic commerce courselecture one. Pdf
Digital-Transformation-Roadmap-for-Companies.pptx
Unlocking AI with Model Context Protocol (MCP)
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Encapsulation_ Review paper, used for researhc scholars
Empathic Computing: Creating Shared Understanding
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Spectral efficient network and resource selection model in 5G networks

Mutation Testing

  • 1. Mutation Testing Chris Sinjakli
  • 2. Testing is a good thing But how do we know our tests are good?
  • 3. Code coverage is a start But it can give a “good” score with really dreadful tests
  • 4. Really dreadful tests public int addTwoNumbers(int a, int b) { return a – b; } ... @Test public void shouldAddTwoNumbers() { int result = addTwoNumbers(1, 1); assertTrue(true); } Coverage: 100% Usefulness: 0
  • 6. A contrived example But how could we detect it?
  • 8. If you can change the code, and a test doesn’t fail, either the code is never run or the tests are wrong.
  • 9. Going with our previous example public int addTwoNumbers(int a, int b) { return a – b; } Let’s change something ... @Test public void shouldAddTwoNumbers() { int result = addTwoNumbers(1, 1); assertTrue(true); }
  • 10. Going with our previous example public int addTwoNumbers(int a, int b) { return a + b; } ... This still passes @Test public void shouldAddTwoNumbers() { int result = addTwoNumbers(1, 1); assertTrue(true); }
  • 11. So it caught a really rubbish test How about something slightly less obvious?
  • 12. Slightly less obvious (and I mean slightly) public int checkConditions(boolean a, boolean b) { if (a && b) { return 42; } else { return 0; } } @Test public void testBothFalse() { int result = checkConditions(false, false); assertEquals(0, result); } @Test public void testBothTrue () { int result = checkConditions(true, true); assertEquals(42, result); } Coverage: 100% Usefulness: >0 But still wrong
  • 13. Slightly less obvious (and I mean slightly) public int checkConditions(boolean a, boolean b) { if (a && b) { return 42; } Mutate else { return 0; } } @Test public void testBothFalse() { int result = checkConditions(false, false); assertEquals(0, result); } @Test public void testBothTrue () { int result = checkConditions(true, true); assertEquals(42, result); }
  • 14. Slightly less obvious (and I mean slightly) public int checkConditions(boolean a, boolean b) { if (a || b) { return 42; } else { return 0; } } @Test public void testBothFalse() { Passing tests int result = checkConditions(false, false); assertEquals(0, result); } @Test public void testBothTrue () { int result = checkConditions(true, true); assertEquals(42, result); }
  • 15. Mutation testing caught our mistake :D
  • 16. Useful technique But still has its flaws
  • 17. The downfall of mutation (Equivalent Mutants) int index = 0 while (someCondition) { doStuff(); index++; if (index == 100) { break; } } Mutates to int index = 0 while (someCondition) { doStuff(); index++; if (index >= 100) { break; } } But the programs are equivalent, so no test will fail
  • 19. Java • Loads of tools to choose from • Bytecode vs source mutation • Will look at PIT (seems like one of the better ones)
  • 20. 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)
  • 21. Ruby
  • 22. 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-head rvm use rbx-head gem install mutant • And easy to use mutate “ClassName#method_to_test” spec
  • 23. 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

Editor's Notes

  • #18: Difficult to identify equivalent mutants. There are some papers which suggest methods (but I didn’t have time to read them).
  • #19: Since most of the team do Ruby, I’ve had a look into that too
  • #20: Bytecode is faster to mutate as it avoids recompilationsJumble and Jester also seem quite popular
  • #21: Exclude 3rd party frameworks
  • #22: Looked into Heckle – since that was what the original topic of this talk was. Turns out it’s been dead for a long time.
  • #23: Largely based on Heckle, rewritten on top of RubiniusOnly supports RSpec, but is that what’s used in the team? Author is looking to extend to other frameworks.Not sure if you need rubinius-head any more, but you did as of February 2012 (perhaps there’s a more stable version with support now)
  • #25: If I’ve not hit the time limit, are there any questions?