Skip to main content

judge.run_pytest

judge.run_pytest(worktree, tests, python, source_dir, timeout)

Run tests in worktree against its source directory.

The environment is fixed: PYTHONPATH (the architecture’s source_dir), a fixed PATH, PYTHONDONTWRITEBYTECODE, PYTHONHASHSEED and PYTEST_DISABLE_PLUGIN_AUTOLOAD (no plugin of the environment is loaded).

Parameters

Name Type Description Default
worktree Path Directory to run in. required
tests Sequence[str] Test paths relative to worktree. required
python Path Interpreter to run pytest with. required
source_dir str Directory (relative to worktree) that holds the package. required
timeout float Seconds the run may take. There is no default. required

Returns

Name Type Description
tuple[Tally, str] tuple[Tally, str]: The counts and the combined output, which depends only on the run: the path of worktree is shown as <worktree> and the duration in pytest’s summary as <time>. With no tests, returns Tally(0, 0, 0) and "no tests declared\n".

Raises

Name Type Description
subprocess.TimeoutExpired If the run takes longer than timeout.

Examples

import sys
from pathlib import Path
from tempfile import TemporaryDirectory

from spotregen.judge import run_pytest

with TemporaryDirectory() as tmp:
    (Path(tmp) / "test_a.py").write_text("def test_a():\n    assert True\n")
    print(run_pytest(Path(tmp), ["test_a.py"], Path(sys.executable), "src", 60)[0])
Tally(passed=1, failed=0, errors=0)