Source code for pytest_nbgrader.runner

"""Notebook-side entry point for running pytest with test cases."""

from __future__ import annotations


__all__ = ["TemporarySymlink", "TemporarySymlinks", "main"]

import pathlib
import types as _types

import pytest

from pytest_nbgrader import conftest, harness, loader


[docs] def main( *args: str, task: str | None = None, subtask: str | None = None, case_dir: str = "tests", auto: bool = True, **kwargs: object, ) -> pytest.ExitCode | int: """ Wrap around pytest to inject test cases and set up config. Parameters ---------- *args : str Additional arguments passed to ``pytest.main``. task : str or None, optional Task name subdirectory, by default None. subtask : str or None, optional Subtask name for the YAML file, by default None. case_dir : str, optional Directory containing test case files, by default ``"tests"``. auto : bool, optional Whether to auto-generate test class, by default True. **kwargs : dict Additional keyword arguments passed to ``pytest.main``. Returns ------- int The pytest exit code. """ # ensure existence of submission if not loader.Submission.submission: raise RuntimeError("No submission found. Call submit() first.") pytest_args = ["-p", "no:pytest-nbgrader"] if subtask is not None: cases = pathlib.Path(case_dir) / (task or "") / f"{subtask}.yml" if not cases.is_file(): raise FileNotFoundError("Test cases could not be found.") pytest_args.append(f"--{cases=!s}") pytest_args.extend(args) if auto: pytest_args.append("harness.py::TestClass") with TemporarySymlinks(conftest, harness): return pytest.main(pytest_args, **kwargs)