Skip to content

Abort when the forced fail test hits a pytest internal error or collects no tests - #649

Open
mccalpink wants to merge 1 commit into
boxed:mainfrom
mccalpink:fix/647-forced-fail-exit-code
Open

mccalpink wants to merge 1 commit into
boxed:mainfrom
mccalpink:fix/647-forced-fail-exit-code

Conversation

@mccalpink

Copy link
Copy Markdown

Summary

The forced-failure control (run_forced_fail_test) now aborts the run, with a clear message and exit status 1, when pytest exits with code 3 (internal error) or 5 (no tests collected). Codes 1 (tests failed) and 2 (collection interrupted by the forced fail) are still accepted, so a genuine forced failure proceeds as before.

Closes #647

Root cause

run_forced_fail_test() in src/mutmut/__main__.py only rejected a runner result of 0. A pytest INTERNALERROR (exit code 3, e.g. a conftest.py hook raising during pytest_sessionstart) was therefore treated as a successful control: mutmut went on to "Running mutation testing" and exited 0, even though no test had run and the control could not be verified. The same holds for exit code 5 (no tests collected).

Fix

After the forced fail run, exit code 0 keeps its existing message. Exit codes 3 and 5 dump the captured output, print FAILED: Forced fail test could not run the tests (exit code N) and exit with status 1.

Exit code 2 is deliberately still accepted: with the forced fail enabled, a test module that calls mutated code while it is imported (for example @pytest.mark.parametrize("x", cases())) fails collection, and pytest reports that as code 2. With process_isolation = "forkserver" this is a normal outcome of the control, because the parent does not import the test modules beforehand.

For a mutant, exit code 3 already counts as killed (stats.py); in the control it means that no test ran, so detectability was never checked.

Known limitation, out of scope here: exit code 4 aborts the run only in fork mode. In forkserver mode run_in_fork turns the BadTestExecutionCommandsException raised in the child into exit code 1, which the control accepts.

How I verified it

  • Added tests for the two codes that must abort (3, 5) and one for code 2 being accepted. On main the two abort tests fail (DID NOT RAISE SystemExit) and the code 2 test passes; with the change all pass.
    uv run pytest tests/mutation/test_mutation.py -k forced_fail -> 2 failed, 4 passed before; 6 passed after
  • Reproduced the report's minimal example (conftest raising when MUTANT_UNDER_TEST == "fail"): on main, mutmut run proceeds to mutation testing and exits 0; with the change it prints FAILED: ... (exit code 3) and exits 1.
  • Checked process_isolation = "forkserver" with a test using @pytest.mark.parametrize("x", cases()) where cases() is project code: exits 0 and runs the mutants on main and with the change.
  • uv run pytest -> 482 passed, 4 skipped
  • uv run pre-commit run --all-files (ruff, ruff-format, mypy) -> all passed

This PR was prepared with AI assistance (Claude Code). I have reviewed the change and the test results.

…cts no tests

The forced fail control only rejected exit code 0, so a pytest
INTERNALERROR (3) or a run without collected tests (5) was accepted as
a successful control and mutation testing continued. Both now abort the
run. Exit codes 1 (tests failed) and 2 (collection interrupted by the
forced fail) are still accepted.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Forced-failure control accepts pytest INTERNAL_ERROR and continues mutating

1 participant