Pytest plugin to randomly order tests and control random.seed.
Pytest plugin to randomly order tests and control random.seed. (Also available for nose).
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 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.
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.
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.
This plugin is a Pytest port of my plugin for nose, nose-randomly.
Install from pip with:
pip install pytest-randomly
Pytest will automatically find the plugin and use it when you run py.test. The output will start with an extra line that tells you the random seed that is being used:
$ py.test platform darwin -- Python 2.7.11, pytest-2.9.1, py-1.4.31, pluggy-0.3.1 Using --randomly-seed=1460130750 ...
If the tests fail due to ordering or randomly created data, you can restart them with that seed using the flag as suggested:
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:
py.test -p no:randomly
- New release notes here
- Fix DeprecationWarning with recent versions of factory_boy.
- Fix collection to not sometimes crash when encoutering pytest Items without a module.
- Fix collection to be deterministically shuffled again, regression in 1.2.0.
- Dropped Python 2.6 compatibility, as upstream dependency NumPy did.
- Reset and output the seed at the start of the test run when --randomly-dont-reset-seed is set, to allow the reorganization of tests to be reproducible.
- Reset the random state for NumPy too.
- Add Python 2.6 compatibility
- Offset the random seed during test setup and teardown. This is to avoid the awkward situation where test setup generates a random object, then the test generates a second one, but due to the re-seeding, they end up being always the same object. Thanks @rouge8 for the report.
- First release on PyPI.
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size & hash SHA256 hash help||File type||Python version||Upload date|
|pytest_randomly-1.2.3-py2.py3-none-any.whl (8.8 kB) Copy SHA256 hash SHA256||Wheel||2.7|
|pytest-randomly-1.2.3.tar.gz (8.3 kB) Copy SHA256 hash SHA256||Source||None|