Mutation Testing Implementation in Python
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Mutation Testing runs in O(n²) time and O(n) space, in Python as in every other language.
- ✓Nothing in the standard library. mutmut is the most widely used tool and works by rewriting source, and cosmic-ray is the more configurable alternative with distributed execution.
- ✓Scope every run with --paths-to-mutate to one package rather than the whole repository, and point mutmut at the fastest test subset that still covers the target.
- ✓The Python trap: mutmut reruns the ENTIRE test suite for each surviving mutant, so the cost is roughly the suite duration multiplied by the number of mutants, and a project with a two-minute suite and two thousand mutants is looking at days rather than minutes.
- ✓The dominant cost is process start-up plus test collection repeated per mutant, and pytest collection on a large suite can exceed the actual test time.
What Mutation Testing actually is
Mutation Testing in Python: what you actually reach for
Python implementation
# pip install mutmut
# setup.cfg
#
# [mutmut]
# paths_to_mutate = src/pricing/
# runner = python -m pytest -x -q --testmon
# tests_dir = tests/
# Scope it. Never point mutmut at a whole repository.
$ mutmut run --paths-to-mutate src/pricing/
# 2 to 3 minutes later:
# Killed 184 Survived 12 Timeout 1 Suspicious 0
$ mutmut results # list survivors
$ mutmut show 47 # the exact diff of mutant 47
# Mutant 47 shows the tests never distinguish these two:
#
# - if total > threshold:
# + if total >= threshold:
#
# That boundary has no test. Write it:
def test_discount_not_applied_exactly_at_threshold():
assert discount(total=100, threshold=100) == 0Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe Python-specific trap
Performance in Python, beyond the O(n²)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which Python type or library should I use for Mutation Testing?
Nothing in the standard library. mutmut is the most widely used tool and works by rewriting source, and cosmic-ray is the more configurable alternative with distributed execution. Both drive whatever runner you already use, pytest, unittest or nose.
What is the most common Python mistake when implementing Mutation Testing?
mutmut reruns the ENTIRE test suite for each surviving mutant, so the cost is roughly the suite duration multiplied by the number of mutants, and a project with a two-minute suite and two thousand mutants is looking at days rather than minutes. People start it on a whole repository, see no output for hours, and conclude the tool is broken. The fix is scoping plus mutmut's cache, not patience.
How does Mutation Testing actually perform in Python?
The dominant cost is process start-up plus test collection repeated per mutant, and pytest collection on a large suite can exceed the actual test time. Passing a narrow -k selection or using pytest-testmon to run only tests that touch the mutated file cuts run time by an order of magnitude. mutmut caches results in .mutmut-cache so an interrupted run resumes rather than restarting. Time complexity is O(n²) and space is O(n).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialIf you are a student, see what Replit offers students for free on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.