Skip to main content

Measures

import subprocess
import sys
import tempfile
from pathlib import Path

from babregen import make_sample_repo

tmp = tempfile.TemporaryDirectory()
root = make_sample_repo(Path(tmp.name) / "demo")


def babregen(*args):
    """Run `babregen` in the sample repository; show the call and its output."""
    done = subprocess.run(
        [sys.executable, "-m", "babregen", *args],
        cwd=root,
        capture_output=True,
        text=True,
        check=False,
    )
    print("$ babregen", *args)
    print(done.stdout.rstrip())
    print(f"[exit {done.returncode}]")
    return done

Three measures describe how well a unit is specified. Two are read from the files, the third runs the evaluations. The table of babregen measures also shows the pace, the number of decision records the intent cites and the number of declared consumers, so it is the one overview of the units.

Definition 1 (The three measures)  

  • Intent coverage: the share of a unit’s intent items that are covered. An item is covered if and only if it names at least one evaluation and every evaluation it names is defined in an evaluation file of the unit (a top-level test* function, or a test* method of a Test* class, named TestK::test_k). This is the definition behind EVAL03, so the covered items are exactly the items without INTENT02 or EVAL03.
  • Specification size: the lines of the unit record, of the decision records its intent items cite (each once) and of its evaluation files, compared with the lines of the implementation.
  • Mutation score: the share of planted mutants of the unit’s module that its evaluations kill.

babregen measures prints the first two for every unit, slow units first, with a total row, and always exits 0. It has no options and runs nothing. babregen mutate UNIT measures the third: it exits 0 if every mutant is killed and 1 otherwise.

Example 1 (Coverage and size)  

done = babregen("measures")
assert done.returncode == 0
$ babregen measures
| unit | pace | coverage | decisions | consumers | code | record | decision lines | evaluation lines | spec | ratio |
|---|---|---:|---:|---:|---:|---:|---:|---:|---:|---:|
| samplepkg.core.calc | fast | 1/1 | 1 | 2 | 10 | 23 | 9 | 9 | 41 | 4.1 |
| total | - | 1/1 | 1 | 2 | 10 | 23 | 9 | 9 | 41 | 4.1 |
[exit 0]

Example 2 (Mutation score) mutate writes each mutant into a throw-away copy of the repository and runs the unit’s evaluations against it. A mutant is killed if an evaluation fails or errors, or if the run exceeds --timeout. Progress goes to standard error; the report lists the survivors only. --out FILE archives every result as TOML, with the full SHA of the commit it ran on, whether the working tree differed from it, and the Python and pytest versions; so the repository must be under git, which the next cell makes it.

for command in (["init", "-q"], ["add", "."], ["commit", "-q", "-m", "sample"]):
    subprocess.run(
        ["git", "-c", "user.name=demo", "-c", "user.email=demo@example.org", *command],
        cwd=root,
        check=True,
        capture_output=True,
    )
done = babregen("mutate", "core.calc", "--python", sys.executable)
assert done.returncode == 1  # two mutants of `_helper` survive
$ babregen mutate core.calc --python /Users/bartz/workspace/babregen-0.1.0/.venv/bin/python3
# Mutation score: samplepkg.core.calc

1 evaluations in tests/test_regen_eval_calc.py, 4 mutants.

Base commit `c38900fd8f1bffd41353aaae53782890da97cd64`, working tree clean; Python 3.14.2, pytest 9.2.0.

## Surviving mutants

| mutant | line | operator | original | mutated |
|---|---:|---|---|---|
| M003 | 10 | return | `return 1` | `return None` |
| M004 | 10 | constant | `1` | `2` |

Mutation score: 2/4 (50 %).
[exit 1]

Both survivors change the private helper _helper, which no evaluation calls. That is either a missing evaluation or a helper nobody needs; a survivor asks for a look, it is not a verdict.

NoteOperators

compare (< and <=, == and !=, in and not in, is and is not), boolean (and and or), negation (drop a not), condition (negate the test of an if or while that is not a single comparison or a not), constant (True and False, a number plus one), operator (+ and -, * and /, & and |, also in augmented assignments), statement (delete a call statement, continue or break), raise (replace by pass) and return (return None). Annotations, docstrings, imports, __all__ and if TYPE_CHECKING: blocks are left alone. The mutated module is written with ast.unparse, so comments do not survive; behaviour does.

Next: draft a unit.