| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
MutPy is a mutation testing tool for Python 3.3+ source code. MutPy supports standard unittest module, generates YAML/HTML reports and has colorful output. It applies mutation on AST level. You could boost your mutation testing process with high order mutations (HOM) and code coverage analysis.
From article at Wikipedia:
Mutation testing (or Mutation analysis or Program mutation) evaluates the quality of software tests. Mutation testing involves modifying a program's source code or byte code in small ways. A test suite that does not detect and reject the mutated code is considered defective. These so-called mutations, are based on well-defined mutation operators that either mimic typical programming errors (such as using the wrong operator or variable name) or force the creation of valuable tests (such as driving each expression to zero). The purpose is to help the tester develop effective tests or locate weaknesses in the test data used for the program or in sections of the code that are seldom or never accessed during execution.
You can easily install MutPy from PyPi:
$ pip install mutpy
... or if you want to have latest changes you can clone this repository and install MutPy from sources:
$ git clone git@github.com:mutpy/mutpy.git $ cd mutpy/ $ python3 setup.py install
Main code (calculator.py) - we will mutate it:
def mul(x, y):
return x * yTest (test_calculator.py) - we will check its quality:
from unittest import TestCase
from calculator import mul
class CalculatorTest(TestCase):
def test_mul(self):
self.assertEqual(mul(2, 2), 4)Now we can run MutPy in the same directory where we have our sources files:
$ mut.py --target calculator --unit-test test_calculator -m
This command will produce the following output:
[*] Start mutation process: - targets: calculator - tests: test_calculator [*] All tests passed: - test_calculator [0.00031 s] [*] Start mutants generation and execution: - [# 1] AOR calculator.py:2 : -------------------------------------------------------------------------------- 1: def mul(x, y): ~2: return x / y -------------------------------------------------------------------------------- [0.02944 s] killed by test_mul (test_calculator.CalculatorTest) - [# 2] AOR calculator.py:2 : -------------------------------------------------------------------------------- 1: def mul(x, y): ~2: return x // y -------------------------------------------------------------------------------- [0.02073 s] killed by test_mul (test_calculator.CalculatorTest) - [# 3] AOR calculator.py:2 : -------------------------------------------------------------------------------- 1: def mul(x, y): ~2: return x ** y -------------------------------------------------------------------------------- [0.01152 s] survived - [# 4] SDL calculator.py:2 : -------------------------------------------------------------------------------- 1: def mul(x, y): ~2: pass -------------------------------------------------------------------------------- [0.01437 s] killed by test_mul (test_calculator.CalculatorTest) [*] Mutation score [0.21818 s]: 75.0% - all: 4 - killed: 3 (75.0%) - survived: 1 (25.0%) - incompetent: 0 (0.0%) - timeout: 0 (0.0%)
First of all we run MutPy with few parameters. The most important are:
There are few phases in mutation process which we can see on printed by MutPy output (marked by star [*]):
There are 4 mutants generated in main mutation phase - 3 of them are killed and only 1 mutant survived. We can see all stats at the end of MutPy output. In this case MutPy didn't generate any incompetent (raised TypeError) and timeout (generated infinite loop) mutants. Our mutation score (killed to all mutants ratio) is 75%.
To increase mutation score (100% is our target) we need to improve our tests. This is a mutant which survived:
def mul(x, y):
return x ** yThis mutant survived because our test check if 2 * 2 == 4. Also 2 ** 2 == 4, so this data aren't good to specify multiplication operation. We should change it, eg:
from unittest import TestCase
from calculator import mul
class CalculatorTest(TestCase):
def test_mul(self):
self.assertEqual(mul(2, 3), 6)We can run MutPy again and now mutation score is equal 100%.
List of all arguments with which you can run MutPy:
List of MutPy mutation operators sorted by alphabetical order:
Experimental mutation operators:
Currently the following test runners are supported by MutPy:
Licensed under the Apache License, Version 2.0. See LICENSE file.
MutPy was developed as part of engineer's and master’s thesis at Institute of Computer Science, Faculty of Electronics and Information Technology, Warsaw University of Technology.
| Back | FazBrowse Home | New Git URL |