• Stars
    star
    573
  • Rank 74,815 (Top 2 %)
  • Language
    Python
  • License
    MIT License
  • Created about 8 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🎲 Pytest plugin to randomly order tests and control random.seed

pytest-randomly

image

image

image

image

pre-commit

Pytest plugin to randomly order tests and control random.seed.


Testing a Django project? Check out my book Speed Up Your Django Tests which covers loads of ways to write faster, more accurate tests.


Features

All of these features are on by default but can be disabled with flags.

  • Randomly shuffles the order of test items. This is done first at the level of modules, then at the level of test classes (if you have them), then at the order of functions. This also works with things like doctests.
  • Resets the global random.seed() at the start of every test case and test to a fixed number - this defaults to time.time() from the start of your test run, but you can pass in --randomly-seed to repeat a randomness-induced failure.
  • If factory boy is installed, its random state is reset at the start of every test. This allows for repeatable use of its random 'fuzzy' features.
  • If faker is installed, its random state is reset at the start of every test. This is also for repeatable fuzzy data in tests - factory boy uses faker for lots of data. This is also done if you're using the faker pytest fixture, by defining the faker_seed fixture (docs).
  • If Model Bakery is installed, its random state is reset at the start of every test. This allows for repeatable use of its random fixture field values.
  • If numpy is installed, its legacy global random state in __ is reset at the start of every test.

    __ https://numpy.org/doc/stable/reference/random/index.html

  • If additional random generators are used, they can be registered under the pytest_randomly.random_seeder entry point and will have their seed reset at the start of every test. Register a function that takes the current seed value.
  • Works with pytest-xdist.

About

Randomness in testing can be quite powerful to discover hidden flaws in the tests themselves, as well as giving a little more coverage to your system.

By randomly ordering the tests, the risk of surprising inter-test dependencies is reduced - a technique used in many places, for example Google's C++ test runner googletest. Research suggests that "dependent tests do exist in practice" and a random order of test executions can effectively detect such dependencies1. Alternatively, a reverse order of test executions, as provided by pytest-reverse, may find less dependent tests but can achieve a better benefit/cost ratio.

By resetting the random seed to a repeatable number for each test, tests can create data based on random numbers and yet remain repeatable, for example factory boy's fuzzy values. This is good for ensuring that tests specify the data they need and that the tested system is not affected by any data that is filled in randomly due to not being specified.

I have written a blog post covering the history of pytest-randomly, including how it started life as the nose plugin nose-randomly.

Additionally, I appeared on the Test and Code podcast to talk about pytest-randomly.

Installation

Install with:

python -m pip install pytest-randomly

Python 3.8 to 3.12 supported.

Usage

Pytest will automatically find the plugin and use it when you run pytest. The output will start with an extra line that tells you the random seed that is being used:

$ pytest
...
platform darwin -- Python ...
Using --randomly-seed=1553614239
...

If the tests fail due to ordering or randomly created data, you can restart them with that seed using the flag as suggested:

pytest --randomly-seed=1234

Or more conveniently, use the special value last:

pytest --randomly-seed=last

(This only works if pytest’s cacheprovider plugin has not been disabled.)

Since the ordering is by module, then by class, you can debug inter-test pollution failures by narrowing down which tests are being run to find the bad interaction by rerunning just the module/class:

pytest --randomly-seed=1234 tests/module_that_failed/

You can disable behaviours you don't like with the following flags:

  • --randomly-dont-reset-seed - turn off the reset of random.seed() at the start of every test
  • --randomly-dont-reorganize - turn off the shuffling of the order of tests

The plugin appears to Pytest with the name 'randomly'. To disable it altogether, you can use the -p argument, for example:

pytest -p no:randomly

Avoid reordering some tests

To fix the order of some tests, use the pytest-order plugin. See its documentation section on usage with pytest-randomly.

Entry Point

If you're using a different randomness generator in your third party package, you can register an entrypoint to be called every time pytest-randomly reseeds. Implement the entrypoint pytest_randomly.random_seeder, referring to a function/callable that takes one argument, the new seed (int).

For example in your setup.cfg:

[options.entry_points]
pytest_randomly.random_seeder =
    mypackage = mypackage.reseed

Then implement reseed(new_seed).

References


  1. Sai Zhang, Darioush Jalali, Jochen Wuttke, KΔ±vanΓ§ Muşlu, Wing Lam, Michael D. Ernst, and David Notkin. 2014. Empirically revisiting the test independence assumption. In Proceedings of the 2014 International Symposium on Software Testing and Analysis (ISSTA 2014). Association for Computing Machinery, New York, NY, USA, 385–396. doi:https://doi.org/10.1145/2610384.2610404↩

More Repositories

1

pytest

The pytest framework makes it easy to write small tests, yet scales to support complex functional testing
Python
11,150
star
2

pytest-testinfra

Testinfra test your infrastructures
Python
2,306
star
3

pytest-mock

Thin-wrapper around the mock package for easier use with pytest
Python
1,725
star
4

pytest-cov

Coverage plugin for pytest.
Python
1,647
star
5

pytest-xdist

pytest plugin for distributed testing and loop-on-failures testing modes.
Python
1,318
star
6

pytest-asyncio

Asyncio support for pytest
Python
1,293
star
7

pytest-django

A Django plugin for pytest.
Python
1,286
star
8

pytest-bdd

BDD library for the py.test runner
Python
1,248
star
9

pluggy

A minimalist production ready plugin system
Python
1,136
star
10

pytest-html

Plugin for generating HTML reports for pytest results
Python
649
star
11

pyfakefs

Provides a fake file system that mocks the Python file system modules.
Python
596
star
12

pytest-flask

A set of pytest fixtures to test Flask applications
Python
471
star
13

pytest-qt

pytest plugin for Qt (PyQt5/PyQt6 and PySide2/PySide6) application testing
Python
376
star
14

pytest-rerunfailures

a pytest plugin that re-runs failed tests up to -n times to eliminate flakey failures
Python
351
star
15

pytest-factoryboy

factory_boy integration the pytest runner
Python
348
star
16

pytest-selenium

Plugin for running Selenium with pytest
Python
325
star
17

cookiecutter-pytest-plugin

A Cookiecutter template for pytest plugins πŸ’»
Python
281
star
18

pytest-splinter

pytest splinter and selenium integration for anyone interested in browser interaction in tests
Python
250
star
19

pytest-describe

Describe-style plugin for the pytest framework
Python
201
star
20

pytest-timeout

Python
185
star
21

pytest-subtests

unittest subTest() support and subtests fixture
Python
177
star
22

pytest-repeat

pytest plugin for repeating test execution
Python
158
star
23

pytest-order

pytest plugin that allows to customize the test execution order
Python
145
star
24

pytest-instafail

py.test plugin to show failures instantly
Python
130
star
25

unittest2pytest

helps rewriting Python `unittest` test-cases into `pytest` test-cases
Python
125
star
26

pytest-cpp

Use pytest's runner to discover and execute C++ tests
C++
114
star
27

pytest-github-actions-annotate-failures

Pytest plugin to annotate failed tests with a workflow command for GitHub Actions
Python
109
star
28

pytest-env

pytest plugin to set environment variables in pytest.ini or pyproject.toml file
Python
103
star
29

pytest-xprocess

pytest external process plugin
Python
94
star
30

pytest-reportlog

Replacement for the --resultlog option, focused in simplicity and extensibility
Python
75
star
31

pytest-variables

Plugin for providing variables to pytest tests/fixtures
Python
73
star
32

execnet

distributed Python deployment and communication
Python
72
star
33

pytest-play

pytest plugin that let you automate actions and assertions with test metrics reporting executing plain YAML files
Python
68
star
34

pytest-messenger

Pytest-messenger report plugin for all popular messengers like: Slack, DingTalk, Telegram
Python
67
star
35

py

Python development support library (note: maintenance only)
Python
66
star
36

pytest-random-order

pytest plugin to randomise the order of tests with some control over the randomness
Python
64
star
37

pytest-print

pytest-print adds the printer fixture you can use to print messages to the user (directly to the pytest runner, not stdout)
Python
63
star
38

pytest-mimesis

Mimesis integration with the pytest test runner. This plugin provider useful fixtures based on providers from Mimesis.
Python
62
star
39

pytest-services

Collection of fixtures and utility functions to run service processes for your tests
Python
58
star
40

pytest-forked

extracted --boxed from pytest-xdist to ensure backward compat
Python
58
star
41

pytest-runner

Python
57
star
42

pytest-metadata

Plugin for accessing test session metadata
Python
55
star
43

iniconfig

Python
49
star
44

apipkg

Python
48
star
45

pytest-twisted

test twisted code with pytest
Python
45
star
46

pytest-stress

A Pytest plugin that allows you to loop tests for a user defined amount of time.
Python
40
star
47

pytest-incremental

py-test plugin: an incremental test runner
Python
40
star
48

pytest-faker

faker integration the pytest test runner
Python
38
star
49

nose2pytest

Scripts to convert Python Nose tests to PyTest
Python
35
star
50

pytest-base-url

pytest plugin for URL based tests
Python
35
star
51

pytest-cloud

Distributed tests planner plugin for pytest testing framework.
Python
35
star
52

pytest-freezer

Pytest plugin providing a fixture interface for spulec/freezegun
Python
35
star
53

plugincompat

Test execution and compatibility checks for pytest plugins
CSS
34
star
54

pytest-fixture-tools

Pytest fixture tools
Python
32
star
55

pytest-faulthandler

py.test plugin that activates the fault handler module during testing
Python
27
star
56

pytest-echo

pytest plugin to dump environment variables, package version and generic attributes.
Python
21
star
57

pytest-localserver

py.test plugin to test server connections locally. This repository was migrated from Bitbucket.
Python
19
star
58

pytest-inline

pytest-inline is a pytest plugin for writing inline tests.
Python
14
star
59

pytest-nunit

An Nunit output plugin for Pytest
Python
10
star
60

design

Graphic design for Pytest project
9
star
61

pytest-plus

pytest-plus adds new features to pytest
Python
9
star
62

pytest-bpdb

pytest plugin for dropping to bpdb on test failures
Python
6
star
63

blog.pytest.org

Repository for the official pytest blog
Python
4
star
64

pytest-tcpclient

pytest mocking of TCP clients
Python
3
star
65

meta

Used for generic pytest organization issues, stuff that can impact multiple projects.
2
star
66

pytest-talks

public pytest talks and workshops - meant to help user groups spin up talks and workshops
HTML
1
star
67

regendoc

Python
1
star
68

sprint

pytest development sprint 2024
1
star