Skip to main content

unittest-parallel

PyPI - Status PyPI GitHub PyPI - Python Version

unittest-parallel is a parallel unit test runner for Python with coverage support.

Run Tests in Parallel

To run tests in parallel with unittest-parallel, specify the directory containing your unit tests with the -s argument and your package's top-level directory using the -t argument:

unittest-parallel -t . -s tests

By default, unittest-parallel runs tests using all CPU cores.

Test Coverage

To run tests with coverage, add either the --coverage option (for line coverage) or the --coverage-branch for line and branch coverage.

unittest-parallel -t . -s tests --coverage-branch

Parallelism Level

By default, unittest-parallel runs test modules in parallel (--level=module). Here is the list of all parallelism options:

  • --level=module - Run test modules in parallel (default)

  • --level=class - Run test classes in parallel. Use this option if you have class fixtures.

  • --level=test - Run individual tests in parallel. Using this option will likely fail if you have any class or module fixtures.

Free-Threaded Python

By default, unittest-parallel uses a process pool to run tests in parallel. If you are using free-threaded Python, you can reduce test-running overhead by using the --thread argument to use a thread pool for parallelization. Note that if you use unittest.mock in your tests, --thread likely won't work since mocking modifies global state shared with all threads. -b / --buffer is ignored with --thread.

Speedup Potential

Generally speaking, unittest-parallel will run your unit tests faster by a factor of the number of CPU cores you have, as compared to unittest discover.

In other words, if you have 4 CPU cores, unittest-parallel will run your tests 4 times faster. If you have 8 CPU cores, it will run 8 times faster, and so on.

Note that you may see less benefit from unittest-parallel if your average test duration is short compared to the underlying cost of parallelization.

I/O-Bound Tests

If your tests are I/O-bound (e.g., call web services), you may benefit from using a higher number of test workers (-j). In the following case, the I/O-bound tests run 100 times faster.

unittest-parallel -j 100 -t . -s tests

Real-World Speedups

I wrote unittest-parallel for a large production backend API application with thousands of unit tests. As expected, unittest-parallel ran tests 4 times faster using 4 cores, compared to unittest discover.

A user reports that their tests ran 20 times faster on their development machine and 6 times faster on their test machine.

Another user reports that "it shaved 70% off the runtime of my painfully long integration tests."

Another user reports that "tests take 2x less times to run."

Usage

usage: unittest-parallel [-h] [-v] [-q] [-f] [-b] [-k TESTNAMEPATTERNS]
                         [-s START] [-p PATTERN] [-t TOP] [--runner RUNNER]
                         [--result RESULT] [-j COUNT]
                         [--level {module,class,test}]
                         [--disable-process-pooling] [--thread] [--coverage]
                         [--coverage-branch] [--coverage-rcfile RCFILE]
                         [--coverage-include PAT] [--coverage-omit PAT]
                         [--coverage-source SRC] [--coverage-html DIR]
                         [--coverage-xml FILE] [--coverage-fail-under MIN]

options:
  -h, --help            show this help message and exit
  -v, --verbose         Verbose output
  -q, --quiet           Quiet output
  -f, --failfast        Stop on first fail or error
  -b, --buffer          Buffer stdout and stderr during tests
  -k TESTNAMEPATTERNS   Only run tests which match the given substring
  -s, --start-directory START
                        Directory to start discovery ('.' default)
  -p, --pattern PATTERN
                        Pattern to match tests ('test*.py' default)
  -t, --top-level-directory TOP
                        Top level directory of project (defaults to start
                        directory)
  --runner RUNNER       Custom unittest runner class <module>.<class>
  --result RESULT       Custom unittest result class <module>.<class>

parallelization options:
  -j, --jobs COUNT      The number of test workers (default is 0, all cores)
  --level {module,class,test}
                        Set the test parallelism level (default is 'module')
  --disable-process-pooling
                        Do not reuse processes used to run test suites
  --thread              Use a thread pool for parallelization

coverage options:
  --coverage            Run tests with coverage
  --coverage-branch     Run tests with branch coverage
  --coverage-rcfile RCFILE
                        Specify coverage configuration file
  --coverage-include PAT
                        Include only files matching one of these patterns.
                        Accepts shell-style (quoted) wildcards.
  --coverage-omit PAT   Omit files matching one of these patterns. Accepts
                        shell-style (quoted) wildcards.
  --coverage-source SRC
                        A list of packages or directories of code to be
                        measured
  --coverage-html DIR   Generate coverage HTML report
  --coverage-xml FILE   Generate coverage XML report
  --coverage-fail-under MIN
                        Fail if coverage percentage under min

Development

This package is developed using python-build. It was started using python-template as follows:

template-specialize python-template/template/ unittest-parallel/ -k package unittest-parallel -k name 'Craig A. Hobbs' -k email 'craigahobbs@gmail.com' -k github 'craigahobbs' -k noapi 1

Download files

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

Source Distribution

unittest_parallel-1.8.4.tar.gz (11.1 kB view details)

Uploaded Source

Built Distribution

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

unittest_parallel-1.8.4-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file unittest_parallel-1.8.4.tar.gz.

File metadata

  • Download URL: unittest_parallel-1.8.4.tar.gz
  • Upload date:
  • Size: 11.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.7

File hashes

Hashes for unittest_parallel-1.8.4.tar.gz
Algorithm Hash digest
SHA256 b4ded8b53cfa7e29e74c4571e19d8e6e87849572f111998e47e84169cccd6a98
MD5 9cb0613b98ba9e122c32a7f4857d1e19
BLAKE2b-256 6c42894650b1fe1a9dec5883e78bd1fad30c38597ce885fbe0a5d15f83cac223

See more details on using hashes here.

File details

Details for the file unittest_parallel-1.8.4-py3-none-any.whl.

File metadata

File hashes

Hashes for unittest_parallel-1.8.4-py3-none-any.whl
Algorithm Hash digest
SHA256 8f93e98bfe04944306ebd43ef2ec77f8c1bc357141edafef0656c4d508f5a2a3
MD5 5ece1a6cb9ff222838199b6b37d2a8b1
BLAKE2b-256 8b205358af7c4ca333e1d08d680b288a66d77ff66f528060321f4a35c7d77d9d

See more details on using hashes here.

Release history Release notifications | RSS feed

1.8.6

2 files

1.8.5

2 files

This release

1.8.4 This release

2 files

1.8.3

2 files

1.8.2

2 files

1.8.1

2 files

1.8.0

2 files

1.7.6

2 files

1.7.5

2 files

1.7.4

2 files

1.7.3

2 files

1.7.2

2 files

1.7.1

2 files

1.7.0

2 files

1.6.2

2 files

1.6.1

2 files

1.6.0

2 files

1.5.4

2 files

1.5.3

2 files

1.5.2

1 file

1.5.1

1 file

1.5.0

1 file

1.4.6

1 file

1.4.5

1 file

1.4.4

1 file

1.4.3

1 file

1.4.1

1 file

1.4.0

1 file

1.3.1

1 file

1.3.0

1 file

1.2.1

1 file

1.2.0

1 file

1.1.3

1 file

1.1.2

1 file

1.1.1

1 file

1.1.0

1 file

1.0.7

1 file

1.0.6

1 file

1.0.5

1 file

1.0.4

1 file

1.0.3

1 file

1.0.2

1 file

1.0.1

1 file

1.0

1 file

0.8.7

1 file

0.8.2

1 file

0.8.1

1 file

0.8

1 file

0.5

1 file

0.4

1 file

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