Skip to main content

mutation.mutation_score

mutation.mutation_score(
    spec,
    name,
    *,
    python=None,
    max_mutants=None,
    timeout,
    progress=None,
)

Run the unit’s evaluations against every mutant of its module.

Parameters

Name Type Description Default
spec RegenSpec The loaded spec. required
name str | Unit The unit, as a dotted module name, its unique suffix or a babregen.Unit. required
python Path | None Interpreter for pytest. Defaults to <root>/.venv/bin/python if it exists, else the running interpreter. None
max_mutants int | None Run only the first max_mutants mutants (in source order). Defaults to all. None
timeout float Seconds one evaluation run may take; a slower mutant counts as killed. Positive and finite; there is no default (--timeout of the command line defaults to 120). required
progress Callable[[MutantResult], None] | None Called with each result as soon as it is known. None

Returns

Name Type Description
ScoreReport ScoreReport The results, in source order.

Raises

Name Type Description
SpecError If name matches no unit or more than one.
ConfigurationError If the unit declares no evaluation file, the evaluations do not all pass on the unchanged module, or spec is not in a git repository with a commit (the report records the base commit).
ExternalToolError If git is not installed.

Examples

import subprocess
import sys
from pathlib import Path
from tempfile import TemporaryDirectory

from babregen import load_spec, make_sample_repo
from babregen.mutation import mutation_score

def git(root, *args):
    subprocess.run(
        ["git", "-c", "user.name=t", "-c", "user.email=t@example.org", *args],
        cwd=root, check=True, capture_output=True,
    )

with TemporaryDirectory() as tmp:
    root = make_sample_repo(Path(tmp) / "demo")
    git(root, "init", "-q")
    git(root, "add", ".")
    git(root, "commit", "-q", "-m", "init")
    spec = load_spec(root)
    report = mutation_score(spec, "core.calc", python=Path(sys.executable), timeout=60)
    for r in report.results:
        print(r.mutant.id, r.mutant.original, "->", r.mutant.mutated, r.detail)
    print(f"{report.killed}/{report.planted}")
M001 return a + b -> return None 1 failed
M002 a + b -> a - b 1 failed
M003 return 1 -> return None survived
M004 1 -> 2 survived
2/4