Skip to main content

pytest-grader

A pytest plugin for testing and scoring programming assignments.

Features

  • Assignment Scoring
    • Add point values to test functions using the @points(n) decorator
    • Show a score summary when running pytest --score
  • Test Locking as described in Basu et al., Automated Problem Clarification at Scale (abstract, pdf)
    • Lock doctests using the # LOCK comment before the function.
    • pytest-grader lock [src] [dst] will generate a copy of src with doctests locked.
    • pytest --unlock provides an interactive interface for unlocking locked doctests.
    • A doctest whose output is a function should give FUNCTION as the expected output, which matches any function value. When unlocking, type FUNCTION for such outputs.
    • A doctest that raises an exception should give ERROR as its entire expected output, which matches any raised exception (and fails if none is raised).
    • A doctest statement that displays nothing (e.g. lst.append(2)) may give NOTHING as its expected output, so that unlocking still asks about it.
    • When unlocking, sentinel answers (FUNCTION, ERROR, NOTHING) may be typed in any case.
    • When unlocking, a string answer may be quoted with either single or double quotes (e.g. "hello" unlocks an expected 'hello'); the canonical form Python displays is recorded. An answer wrong only in its presence or absence of quotes is not accepted, but earns a hint saying so.
    • Unlocked outputs are saved in .unlocked.json (see --unlock-file) so that tests stay unlocked across pytest runs.
  • Test Isolation
    • Modules listed under reload_modules in grader.json are reloaded before each test, so a test that mutates a module (e.g. by monkeypatching one of its functions) does not affect later tests.
    • Globals injected by pytest's assertion rewriting (@py_builtins, @pytest_ar) are removed from doctest namespaces.
  • Test Timeouts
    • Each test (including each doctest) is limited to 10 seconds, so an infinite loop fails that test with a clear message instead of hanging the run. The remaining tests still run and are scored.
    • Adjust the limit with --timeout SECONDS; --timeout 0 disables it. The timeout is also disabled under --pdb.
    • Code blocked outside the Python interpreter (e.g. waiting on input() or a hung C call) cannot be interrupted; pure-Python loops always time out.

Usage

Include a conftest.py file in the distribution of your assignment that contains pytest_plugins = ["pytest_grader"].

Optionally describe the assignment in a grader.json file next to it:

{
  "reload_modules": ["hog"]
}

reload_modules lists modules reloaded before each test for isolation.

See the examples directory for more usage info.

License

MIT

Updating versions

  • Change version in pyproject.toml
  • uv build
  • uv publish If your pypi credentials are in ~/.pypirc, then instead run uvx uv-publish.

Metadata

Release files for pytest-grader 0.3.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-grader 0.3.3
File Size Uploaded
pytest_grader-0.3.3.tar.gz 19.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-grader 0.3.3
File Interpreter ABI Platform
pytest_grader-0.3.3-py3-none-any.whl Python 3 none any Details

Total release size: 33.7 kB

Release files / pytest_grader-0.3.3.tar.gz

Download URL pytest_grader-0.3.3.tar.gz
Size 19.8 kB
Tags Source
SHA-256 checksum
How to use checksums
53299d96aea925e9222ad6f6268311d49fe23db60dde5056d8eda7424ed0435c
BLAKE2b-256 checksum
How to use checksums
9cfb41b20e7da6c5b119e3759265b8bf4cc9331ec16c49a481898cd402681552
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.13

Release files / pytest_grader-0.3.3-py3-none-any.whl

Download URL pytest_grader-0.3.3-py3-none-any.whl
Size 14.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fc50489ef9d6f91c9b4dc515799c53b86f336d57d7d273ee30ab38b1a145cdf6
BLAKE2b-256 checksum
How to use checksums
fe9c37d16ab2ee2d608b316db55a24a099ee6aac5b1cf6a58de816e6112987fb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.13

Release history Release notifications | RSS feed

0.4.0

2 release files

This release

0.3.3 This release

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.10

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page