Skip to main content

A replacement/wrapper around doctest to allow tests to be run in parallel

Project description

Parallel Doctest

Parallel Doctest splits doctests up and allows them to be executed in parallel to help investigate concurrency bugs. It is based on Python's standard-library doctest module.

Usage

Preparing tests

Parallel Doctest will first do a (crude) scan of your doctest code to work out which individual tests within a Doctest block depend on each other. For example given a block::

>>> a = 1
>>> print(a)
1
>>> print(2)

The second line print(a) depends on the first line an thus should be run as a sequential series.

This is an automatic process although at this stage is fairly incomplete.

Tests can be marked as unsuitable for parallel execution with:

  • # doctest: +NO_PARALLEL_MODULE disables parallel execution for the whole module.
  • # doctest: +NO_PARALLEL_BLOCK disables parallel execution for a given block (i.e. everything in a function docstring).
  • # doctest: +NO_PARALLEL disables parallel execution for that example only (i.e. just a single line starting with >>>).

All these NO_PARALLEL directives disable parallelization for the tests so that they will be the only test executing when they are run.

Some control of serialization can be achieved with:

  • # doctest: +SEQUENTIAL_BLOCK marks the whole block to be run in series. In this case the block may be run in parallel with other blocks but the individual tests in the block will be run in order.
  • # doctest: +AFTER_PREVIOUS manually adds a dependency for an individual doctest so that it is only run after the previous one (which must be in the same block).

Repeats

The number of repeats can be controlled with the option flags PARALLEL_REPEAT_1, PARALLEL_REPEAT_2, PARALLEL_REPEAT_4, going in factors of 2 up to PARALLEL_REPEAT_1024. These are intended to be enabled at the command-line rather than set in doctested code. To get (e.g.) 5 repeats use both PARALLEL_REPEAT_1 and PARALLEL_REPEAT_4. Parallel repeats only apply to parallel tests and not to any test that's serialized.

Alternatively the environmental variable PARALLEL_DOCTEST_REPEAT can be used to override it (and this takes priority).

High level interface

Parallel Doctest can be used from the command-line in the same way as the Python doctest module::

python -m parallel_doctest -v example.py

Parallel Doctest also exposes the following high-level functions, designed to match the Python doctest interface as much as possible::

testmod(m=None, ...) - pass a module object and test all the docstrings of functions and classes in the module.

run_docstring_examples(f, ...) - pass a function, class, string, or module and run all the doctests associated with that.

testfile(filename, ...) - load examples from a filename.

Unit test interface

Parallel Doctest doesn't expose the full unittest interface of doctest directly, but instead has one function:

load_module_as_unittest_case(module) - loads all doctests from a module into a single unittest.TestSuite.

Commentary

This tool was largely created to investigate thread-safety issues, both with thread sanitizer and Python level. It is intended for that rather than to try to speed up testing by parallelizing (which is probably more likely to be a pessimization, even on free-threaded builds of Python).

Most uses of doctest are for integration-level tests which may not parallelize hugely well so be wary of that. SEQUENTIAL_BLOCK is probably a good default option to apply globally as a reasonable compromise between getting some parallelization and avoiding too many failures due to side-effects.

A lot of doctests are based heavily round console output (it is what it tests, after all!). parallel-doctest solves this by redirecting sys.stdout and sys.stderr into thread-local variables. This isn't expected to be hugely fast, so you may find your tests are not doing too much parallel work. For best results, try to run tests that do real work with a small amount of output.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

parallel_doctest-0.0.2.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

parallel_doctest-0.0.2-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file parallel_doctest-0.0.2.tar.gz.

File metadata

  • Download URL: parallel_doctest-0.0.2.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for parallel_doctest-0.0.2.tar.gz
Algorithm Hash digest
SHA256 c97bd4c9f367aef046db753897e5f1d5a84d33a6473db0e89e35b7d93cbe3cfb
MD5 e5b94a24696d0c62f8c5bdd67aa03820
BLAKE2b-256 7e61d2c4a62942b9e170f376bf127b6e001904e9865e4cb1688c13855eda21cb

See more details on using hashes here.

File details

Details for the file parallel_doctest-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for parallel_doctest-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 27599ca227a7ebd0fd043ce24b78bf8f55f78732592a60b4db4ab15f3ab71374
MD5 c46b9a6661800acd20d824cd6248e9d7
BLAKE2b-256 c96fc95b73cdfc417b91ce87da2378aeeb7eaec549e8b9668ca04700fe4b7509

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page