# Your integration tests pass. mutmut shows what they never check

> Let mutmut plant small bugs in the Python module you just changed, then see which of your tests stay silent before you hand the connector over.

Original: https://fdetimes.net/en/guides/mutmut-mutation-testing-python-integration/

Google once analyzed around 15 million mutants, bugs planted in code on purpose, and drew two conclusions. Surviving mutants point precisely to where a test suite has gaps. And developers who are shown those mutants go on to write more tests.

FDEs need that finding most in integration code, the part usually written in the biggest hurry: mapping a client's webhook onto an internal model, deciding when to retry, normalizing error codes. The tests are green, coverage looks fine, and then a status code at the boundary breaks the pipeline at midnight.

This guide uses mutmut, a mutation testing tool for Python that its author describes as focused on ease of use, to check an integration module. By the end you will have a list of surviving mutants, a few new tests that kill them, and a process light enough to run before every handover.

## What do you need before you start?

mutmut's documentation says the tool must run on a system that supports *fork*. macOS and Linux qualify. If you are on a Windows laptop issued by the client, run it through WSL from the start and save yourself the trouble.

You need a Python project with pytest tests in a `tests` or `test` directory, since that is where mutmut looks for tests by default. You need git so you can undo changes easily. And the code under test must live inside functions; the reason comes later.

The running example below is hypothetical: an integration module that decides when to call a client's API again.

```python
# src/integration/retry.py
def should_retry(status_code):
    return status_code >= 500 or status_code == 429
```

And two passing tests:

```python
# tests/test_retry.py
from integration.retry import should_retry

def test_retry_on_server_error():
    assert should_retry(503)

def test_no_retry_on_bad_request():
    assert not should_retry(400)
```

Coverage for this function is 100%. Every line runs. The question is whether any line is actually checked.

## Step 1: install mutmut in your virtual environment

```bash
pip install mutmut
```

**Check:** run `pytest` once and make sure every test passes. If the original suite is already red, every mutation result that follows is meaningless.

## Step 2: point mutmut only at the module you just changed

Do not let mutmut loose on the whole codebase. Google's paper on large-scale mutation testing describes how the company made the technique practical: by mutating only the code changed during review. FDEs should apply the same approach and pick only the integration module they have just touched.

Configuration goes in `setup.cfg`, or in `pyproject.toml` under `[tool.mutmut]`. With `pyproject.toml`, the documentation requires paths to be declared as an array:

```toml
[tool.mutmut]
source_paths = ["src/integration/"]
```

This is a trimmed configuration. Key names can differ between versions, so check against the documentation for the release you installed.

## Step 3: run it, and interrupt whenever you need to

```bash
mutmut run
```

mutmut creates faulty versions of each function and runs pytest against every one. A mutant that makes at least one test fail counts as killed. A mutant under which the tests stay green survives.

If the module is large, you can stop at any point. According to the README, the next run picks up where the last one left off. That suits the rhythm of working on a client site: run it over lunch, interrupt it when you need the machine for a demo, carry on in the afternoon.

## Step 4: read surviving mutants like bug reports

```bash
mutmut browse
```

This opens a terminal interface listing the mutants alongside the code each one changed. In the example above, the survivors would likely be changes such as turning `>=` into `>`, or replacing `429` with some other number.

Work through each one. If `>=` becomes `>`, `should_retry(503)` is still true and `should_retry(400)` is still false, so both tests pass, even though the system no longer retries on a 500. If 429 changes, no test sends a 429, so the client's rate limit is treated as a permanent failure and nobody notices.

The mutant that turns `or` into `and`, by contrast, gets killed, because `should_retry(503)` then returns false. That is how to read the results: each survivor is a behavior you wrote but never asserted.

**Key point:** A test that runs through a line of code has not necessarily checked it.

## Step 5: write a mutant to disk when the diff is not clear enough

`mutmut apply` writes a mutant straight into the file so you can run and debug it like real code. Because the command modifies files on disk, commit first, then use `git diff` to see the change and `git checkout` to undo it.

```bash
mutmut apply <mutant-name>   # name comes from mutmut browse
git diff
git checkout src/integration/retry.py
```

## Step 6: write tests that kill the survivors, then retest

Focus on the boundary value each survivor exposes:

```python
def test_retry_on_exact_500():
    assert should_retry(500)

def test_retry_on_rate_limit():
    assert should_retry(429)
```

The first test kills the `>=` to `>` mutant, because with `>` a 500 is no longer retried. The second kills the mutant that changes 429. Go back to `mutmut browse` and retest the mutants you just dealt with; the README describes browse as the place to retest after fixing tests. **Check:** the two survivors involving 500 and 429 should now show as killed.

## The traps specific to integration code

The most common trap is cutting `max_stack_depth` to speed things up. The option tells mutmut to count a test as relevant to a function only when the call stack depth stays below a threshold. Runs get faster, but more mutants survive, because integration tests often reach your function through several layers, so you see "gaps" that the tests actually cover.

The next trap is assuming everything gets mutated. mutmut 3 and later only target code inside functions. A mapping table defined at module level, such as a dict that translates a client's error codes into internal ones, will not be touched.

The documentation suggests trying mutmut 2 for that case, since it uses a different execution model; the simpler fix is to wrap the mapping table in a function.

Nor should you read survivors in code the tests never reach. You can restrict mutmut to mutating only the lines coverage.py records as executed; the option is off by default. Turn it on when you want to separate two questions: where there are no tests, and where there are tests that are not strict enough.

## What does this skill look like on a client site?

The best moment is just before handing over a connector. Run mutmut on that module, kill the survivors in the retry, mapping and validation logic, then record three things in the handover notes: the number of survivors before, the number after, and why you accepted each one that remains.

The client's team gets an honest map of the risk in place of a misleading coverage figure.

Those notes are also the best evidence for a portfolio. A sample repo with a "before" commit, a commit adding boundary tests and an accompanying survivor table shows an interviewer that you know what tests are for, more clearly than any self-description on a CV.

Coverage tells you where the tests have been. The survivor list tells you what they forgot to assert, and in integration code that is usually the very error codes that only show up at midnight.

**Try this week:**

- Pick an integration module you changed recently, declare it under [tool.mutmut] in pyproject.toml and run mutmut run
- Open mutmut browse, choose three surviving mutants and write a test that kills each one, then retest them in the interface
- Wrap a module-level mapping table in a function, rerun mutmut and record the survivor counts before and after in your handover notes

## Sources

- [boxed/mutmut (GitHub README)](https://github.com/boxed/mutmut)

- [mutmut - python mutation tester](https://mutmut.readthedocs.io/en/latest/index.html)

- [Does mutation testing improve testing practices?](https://arxiv.org/abs/2103.07189)

- [Practical Mutation Testing at Scale](https://arxiv.org/abs/2102.11378)
