Skip to main content

Reorder tests depending on their paths and names.

Project description

Reorder tests depending on their nodeids (strings of test file path plus test name plus parametrization, like: test/test_prefix_reordering.py::test_reordering_default[test_names5-expected_test_order5]).

Normally tests are sorted alphabetically. That makes integration tests run before unit tests.

With pytest_reorder you can install a hook that will change the order of tests in the suite. By default pytest_reorder will seek for unit, integration and ui tests and put them in the following order:

  1. unit

  2. all tests with names not indicating unit, integration, nor UI tests

  3. integration

  4. ui

The default regular expressions can find unit, integration and UI tests both laid flat and deeply nested. You can also specify your custom order.

Pythons supported

CPythons 2.7, 3.2, 3.3, 3.4, 3.5, 3.5-dev, nightly. PyPy and PyPy3. Development happens mostly under Python 2.7 (for no good reason) but Travis builds them all.

HOWTO

It’s possible to customize the ordering. To do so, you have to specify your custom tests order by passing a list of regular expresions that match tests’ nodeids. If more than one regex matches some test nodeid, the first one wins.

Command line interface

pytest_reorder hooks in a --reorder command line option that takes zero arguments or an ordering spec list.

  1. If no arguments are given, default reordering will be applied.

  2. If a list is passed, e.g. --reorder '(^|.*/)(test_)?unit' '*' '(^|.*/)(test_)?web', tests are reordered to go as the matches in the list do. Regular list items are treated as Python regexes. The special '*' match is required and specifies where to put tests that don’t match any of the regexes. A single asterisk was chosen for that because it’s not a valid regular expression.

Programmatic interface

Modify your main conftest file (e.g. tests/conftest.py) to include:

from pytest_reorder import default_reordering_hook as pytest_collection_modifyitems  # add noqa here if you use pyflakes

or specify a custom test order:

from pytest_reorder import make_reordering_hook
# Make unit tests run before 'db' tests, which run before 'web' tests. Other tests will run at
# the very beginning of the suite:
pytest_collection_modifyitems = make_reordering_hook(
    [None, r'(^|.*/)(test_)?unit', r'(^|.*/)(test_)?db', r'(^|.*/)(test_)?web'])

Without pytest_reorder

Flat:

sample_test_suites/flat/test_sample.py ...
sample_test_suites/flat/integration/test_some_integration.py ..
sample_test_suites/flat/ui/test_some_ui.py .
sample_test_suites/flat/unit/test_some_unit.py ..

Nested:

sample_test_suites/nested/app_1/tests/integration/test_some_integration.py ..
sample_test_suites/nested/app_1/tests/ui/test_some_ui.py .
sample_test_suites/nested/app_1/tests/unit/test_some_unit.py ..
sample_test_suites/nested/app_2/tests/test_sth.py ...
sample_test_suites/nested/app_2/tests/test_unit.py .

With pytest_reorder

Flat:

sample_test_suites/flat/unit/test_some_unit.py ..
sample_test_suites/flat/test_sample.py ...
sample_test_suites/flat/integration/test_some_integration.py ..
sample_test_suites/flat/ui/test_some_ui.py .

Nested:

sample_test_suites/nested/app_1/tests/unit/test_some_unit.py ..
sample_test_suites/nested/app_2/tests/test_unit.py .
sample_test_suites/nested/app_2/tests/test_sth.py ...
sample_test_suites/nested/app_1/tests/integration/test_some_integration.py ..
sample_test_suites/nested/app_1/tests/ui/test_some_ui.py .

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

pytest_reorder-0.1.0.tar.gz (5.1 kB view details)

Uploaded Source

File details

Details for the file pytest_reorder-0.1.0.tar.gz.

File metadata

File hashes

Hashes for pytest_reorder-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3efae0bd26d464362a57c8f6d0b03e1905c696d5a00533abc12e52e835dcd842
MD5 fa4952fd30376aa801ab647f80c5fb11
BLAKE2b-256 ea51050ecf04d75c300b4321fb5cf6c82206c5577d45edb0f9082942f759ecb5

See more details on using hashes here.

Supported by

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