Mutation Testing

BLOG
X
min read

Have you ever thought about creating unit tests with good coverage but thought that it was hard or choosing the unit test data stopped you from making the optimum tests?

If only there was a method to test my test?

What is mutation testing?

Mutation testing is a software testing type that is based on changes or mutations. Miniscule changes are introduced into the source code to check whether the defined test cases can detect errors in the code. These Miniscule changes mimic software errors so that the bugs they introduce are validated by research.

The miniscule changes are Mutations
The changed source code is Mutant

In other words, It is A Test for Your Test.

The objective of Mutation Testing:

  • To identify pieces of code that are not tested properly.
  • To identify hidden defects that cannot be detected using other testing methods.
  • To discover new kinds of errors or bugs.
  • To assess the quality of the test cases(by calculating the mutation score).

Mutation Score = (killed mutants )/(total number of mutatnts ) *100

Advantages of Mutation Testing :

  • It brings a good level of error detection to the program.
  • It discovers ambiguities in the source code.
  • High coverage of the source program is attained.
  • Loopholes in test data can be identified.

Disadvantages of Mutation Testing:

  • It is highly costly and time-consuming.
  • Mutation testing is not applicable for black-box testing as it involves a lot of source code changes.

Note: Automation is necessary for mutation testing as it is very time-consuming.

Types of mutants & Mutations :

  • Killed mutant: mutants that were killed after mutation testing (which means that our test suit can detect changes in the code and will fail accordingly )
  • Survived mutant: mutants that are alive after running the test suit on the mutated source code.
  • Equivalent mutant: mutants that are alive even after running test data through them, but they have the same meaning as the original source code even though they may have different syntax.

Mutations:

Value Mutations:
The values are changed to detect errors in the program. Basically, a small value is changed to a larger value, or a larger value is changed to a smaller value. basically, constants are changed.
Ex : Int x = 10000000254 -> int x = 10254

Decision Mutations:
Logical or arithmetic operators are changed to detect errors in the program.
Ex : If (a < b)  -> if (a > b)

Statement Mutations:
A statement is deleted, or it is replaced by some other statements.
Ex : C = 10 -> d = 10

How to Apply Mutation Testing:

  1. Write a couple of unit tests for your module
  2. Run mutation testing automaton on it (like Pitest).
  3. Check the result and change the test suit accordingly by adding tests or changing test’s data.
  4. Repeat steps 2 and 3 until reaching good coverage.

Optional Steps:

  1. Try to remove the unit test and run the mutation testing automaton again
  2. If the coverage didn’t change Remove the test.
  3. Repeat steps 1 and 2 till you reach the optimum test suite.

Example:

In this example, we are using Pitest which Is a mutation testing system to automate the process.
Having a small function as

public int add(int a , int b){
        if(a < 2)
        {
            return (a + b)* -1;
        }
        else{

            return a+b;
       }
   }


we could start with a couple of small tests as

 public void testAdd1() {
         int result = new MutationTesting().add(0,0);
         assertEquals(0,result);
      }
 public void testAdd2() {
         int result = new MutationTesting().add(3,0);
         assertEquals(3,result);
      }


Then run Pitest & view the report.

Pitest created 7 mutations and our test suit killed only 3, so we can calculate the mutation score as  3/7*100 = 43%

Or we can view it in the report as the percentage next to the progress bar.

Mutation Testing. java

The report displays :
The number next to the line of code is the number of mutations (changes) for each line
The mutations section indicates which mutation was used on what lines and whether it survived or died.

The report also indicates which mutators were used

Note: You can find all about the available mutators and the default mutators on Pitest website https://pitest.org/quickstart/mutators/

Using the report as our guide we start to see the lack of coverage of our test suite. For example, seeing that we didn’t have a test for the edge value of 2 or we didn’t try the addition with a number other than zero to check both the branches of the "if condition".

Test yourself whether you can reach the maximum coverage for this case.

Feel free to contact us if you get stuck or look for the solution in here https://github.com/sandraerian16/MutationTestingExample.git

Conclusion

Mutation Testing is one of the most effective ways to ensure good coverage. It is very easy to apply and there are many mutation testing systems that help.

If you want to start making a better test suit or want to be sure of your test suit data; mutation testing is your choice, Though it might be time-consuming and costly, It would be worthwhile if done correctly.

Modernizing Legacy Apps​

Maecenas mollis sagittis ante, eleifend ultricies sapien. Nam ultricies risus et augue auctor vulputate gravida eget sem. Quisque mollis gravida magna, eu semper eros pharetra in. Sed et elit sit amet odio rutrum consectetur vel vel ante. Praesent vitae elementum lacus. Vivamus efficitur nunc tortor, cursus lobortis purus placerat ut. Maecenas ut aliquet ante, vel finibus lorem. Nulla facilisi. Donec maximus elementum pulvinar.

test heading

h1 text

h3

Impact

Sample article featured image
Pellentesque posuere sem in ipsum venenatis, at bibendum lorem aliquam. Nullam condimentum tempus orci nec commodo. Maecenas malesuada elementum metus, non aliquam est elementum sed. Integer ac finibus ligula, id venenatis lectus. Mauris non eleifend enim. Pellentesque eu congue justo. In ornare dapibus nisi, sit amet feugiat neque. Vivamus mollis, lectus quis gravida viverra, risus ligula congue felis, ut laoreet sem nisi in tortor. Sed vel ligula nulla.
“Quisque mollis purus nec pulvinar rutrum. Duis faucibus sed orci vel pellentesque. Interdum et malesuada fames ac ante ipsum primis in faucibus. Donec non volutpat eros, nec placerat mi. Praesent porta felis ut urna sagittis, sit amet placerat nisl porttitor.”

Nunc tempor molestie velit id dictum. Aenean ac venenatis ipsum, sit amet sodales tortor. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Pellentesque posuere sem in ipsum venenatis, at bibendum lorem aliquam.

Nullam condimentum tempus orci nec commodo. Maecenas malesuada elementum metus, non aliquam est elementum sed. Integer ac finibus ligula, id venenatis lectus. Mauris non eleifend enim. Pellentesque eu congue justo. In ornare dapibus nisi, sit amet feugiat neque. Vivamus mollis, lectus quis gravida viverra, risus ligula congue felis, ut laoreet sem nisi in tortor. Sed vel ligula nulla.

data-acc-source-start

Ensure that Modernizing your Legacy Application is the Right Decision

Our expert consultants work closely with you to understand you organization's business drivers, then conduct an in-depth business goals and that every dollar invested is directed towards the right solution

Depend on a Tailored, Phased Application Modernization Strategy

Our expert consultants work closely with you to understand you organization's business drivers, then conduct an in-depth business goals and that every dollar invested is directed towards the right solution

Streamline the Transition from Old to New

Our expert consultants work closely with you to understand you organization's business drivers, then conduct an in-depth business goals and that every dollar invested is directed towards the right solution

data-acc-source-end

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur elementum, elit a pellentesque placerat, nisl quam blandit orci, at maximus eros nunc nec lacus. Nullam euismod consequat libero, eget suscipit ligula lacinia nec. Nunc finibus dapibus quam, eu convallis magna. Nulla finibus ut risus in sodales. Cras tristique nisi non mattis volutpat. Nullam venenatis varius nisl, dictum ornare lorem dictum rhoncus. Nulla sem nunc, lobortis et massa sed, ultrices convallis justo. Quisque laoreet nibh sit amet arcu rhoncus accumsan. Proin at elementum lacus, at maximus mi. Curabitur vulputate urna mollis lacinia auctor. Donec venenatis finibus magna id tempor. Duis at mattis odio. Aenean eu tempus justo. Donec est arcu, vulputate quis risus et, pharetra imperdiet velit.

Vivamus ut dignissim quam.

No items found.
Article carousel image 1
Article carousel image 2
Article carousel image 3
Author
Yomna Anwar
Posted on
25 Oct 2021
We’re your partner in addressing

real human needs.

Align IT Initiatives with Strategic Business Goals
Plus sign iconMinus sign icon
10X
Increase in transactions
per second

Heading 1

Heading 2

Heading 3

Heading 4

Heading 5
Heading 6

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

Block quote

Ordered list

  1. Item 1
  2. Item 2
  3. Item 3

Unordered list

  • Item A
  • Item B
  • Item C

Text link

Bold text

Emphasis

Superscript

Subscript

Modernizing Legacy Apps​

Maecenas mollis sagittis ante, eleifend ultricies sapien. Nam ultricies risus et augue auctor vulputate gravida eget sem. Quisque mollis gravida magna, eu semper eros pharetra in. Sed et elit sit amet odio rutrum consectetur vel vel ante. Praesent vitae elementum lacus. Vivamus efficitur nunc tortor, cursus lobortis purus placerat ut. Maecenas ut aliquet ante, vel finibus lorem. Nulla facilisi. Donec maximus elementum pulvinar.

Impact

Sample article featured image
Pellentesque posuere sem in ipsum venenatis, at bibendum lorem aliquam. Nullam condimentum tempus orci nec commodo. Maecenas malesuada elementum metus, non aliquam est elementum sed. Integer ac finibus ligula, id venenatis lectus. Mauris non eleifend enim. Pellentesque eu congue justo. In ornare dapibus nisi, sit amet feugiat neque. Vivamus mollis, lectus quis gravida viverra, risus ligula congue felis, ut laoreet sem nisi in tortor. Sed vel ligula nulla.
“Quisque mollis purus nec pulvinar rutrum. Duis faucibus sed orci vel pellentesque. Interdum et malesuada fames ac ante ipsum primis in faucibus. Donec non volutpat eros, nec placerat mi. Praesent porta felis ut urna sagittis, sit amet placerat nisl porttitor.”

Nunc tempor molestie velit id dictum. Aenean ac venenatis ipsum, sit amet sodales tortor. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Pellentesque posuere sem in ipsum venenatis, at bibendum lorem aliquam.

Nullam condimentum tempus orci nec commodo. Maecenas malesuada elementum metus, non aliquam est elementum sed. Integer ac finibus ligula, id venenatis lectus. Mauris non eleifend enim. Pellentesque eu congue justo. In ornare dapibus nisi, sit amet feugiat neque. Vivamus mollis, lectus quis gravida viverra, risus ligula congue felis, ut laoreet sem nisi in tortor. Sed vel ligula nulla.

data-acc-source-start

Ensure that Modernizing your Legacy Application is the Right Decision

Our expert consultants work closely with you to understand you organization's business drivers, then conduct an in-depth business goals and that every dollar invested is directed towards the right solution

Depend on a Tailored, Phased Application Modernization Strategy

Our expert consultants work closely with you to understand you organization's business drivers, then conduct an in-depth business goals and that every dollar invested is directed towards the right solution

Streamline the Transition from Old to New

Our expert consultants work closely with you to understand you organization's business drivers, then conduct an in-depth business goals and that every dollar invested is directed towards the right solution

data-acc-source-end

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur elementum, elit a pellentesque placerat, nisl quam blandit orci, at maximus eros nunc nec lacus. Nullam euismod consequat libero, eget suscipit ligula lacinia nec. Nunc finibus dapibus quam, eu convallis magna. Nulla finibus ut risus in sodales. Cras tristique nisi non mattis volutpat. Nullam venenatis varius nisl, dictum ornare lorem dictum rhoncus. Nulla sem nunc, lobortis et massa sed, ultrices convallis justo. Quisque laoreet nibh sit amet arcu rhoncus accumsan. Proin at elementum lacus, at maximus mi. Curabitur vulputate urna mollis lacinia auctor. Donec venenatis finibus magna id tempor. Duis at mattis odio. Aenean eu tempus justo. Donec est arcu, vulputate quis risus et, pharetra imperdiet velit.

Vivamus ut dignissim quam.

No items found.
Article carousel image 1
Article carousel image 2
Article carousel image 3
Author
This is some text inside of a div block.
Posted on
This is some text inside of a div block.
Topics