| 1 |
import random |
| 2 |
import sys |
| 3 |
import traceback |
| 4 |
import warnings |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
from random_order.bucket_types import bucket_type_keys, bucket_types |
| 9 |
from random_order.cache import process_failed_first_last_failed |
| 10 |
from random_order.config import Config |
| 11 |
from random_order.shuffler import _disable, _get_set_of_item_ids, _shuffle_items |
| 12 |
from random_order.xdist import XdistHooks |
| 13 |
|
| 14 |
|
| 15 |
def pytest_addoption(parser): |
| 16 |
group = parser.getgroup("pytest-random-order options") |
| 17 |
group.addoption( |
| 18 |
"--random-order", |
| 19 |
action="store_true", |
| 20 |
dest="random_order_enabled", |
| 21 |
help="Randomise test order (by default, it is disabled) with default configuration.", |
| 22 |
) |
| 23 |
group.addoption( |
| 24 |
"--random-order-bucket", |
| 25 |
action="store", |
| 26 |
dest="random_order_bucket", |
| 27 |
default=Config.default_value("module"), |
| 28 |
choices=bucket_types, |
| 29 |
help="Randomise test order within specified test buckets.", |
| 30 |
) |
| 31 |
group.addoption( |
| 32 |
"--random-order-seed", |
| 33 |
action="store", |
| 34 |
dest="random_order_seed", |
| 35 |
default=Config.default_value(str(random.randint(1, 1000000))), |
| 36 |
help="Randomise test order using a specific seed.", |
| 37 |
) |
| 38 |
|
| 39 |
|
| 40 |
def pytest_configure(config): |
| 41 |
config.addinivalue_line( |
| 42 |
"markers", "random_order(disabled=True): disable reordering of tests within a module or class" |
| 43 |
) |
| 44 |
|
| 45 |
if config.pluginmanager.hasplugin("xdist"): |
| 46 |
config.pluginmanager.register(XdistHooks()) |
| 47 |
|
| 48 |
if hasattr(config, "workerinput"): |
| 49 |
|
| 50 |
seed = config.workerinput["random_order_seed"] |
| 51 |
if hasattr(config, "cache"): |
| 52 |
assert config.cache is not None |
| 53 |
config.cache.set("random_order_seed", seed) |
| 54 |
config.option.random_order_seed = seed |
| 55 |
|
| 56 |
|
| 57 |
def pytest_report_header(config): |
| 58 |
plugin = Config(config) |
| 59 |
if not plugin.is_enabled: |
| 60 |
return "Test order randomisation NOT enabled. Enable with --random-order or --random-order-bucket=<bucket_type>" |
| 61 |
return ("Using --random-order-bucket={plugin.bucket_type}\n" "Using --random-order-seed={plugin.seed}\n").format( |
| 62 |
plugin=plugin |
| 63 |
) |
| 64 |
|
| 65 |
|
| 66 |
def pytest_collection_modifyitems(session, config, items): |
| 67 |
failure = None |
| 68 |
|
| 69 |
session.random_order_bucket_type_key_handlers = [] |
| 70 |
process_failed_first_last_failed(session, config, items) |
| 71 |
|
| 72 |
item_ids = _get_set_of_item_ids(items) |
| 73 |
|
| 74 |
plugin = Config(config) |
| 75 |
|
| 76 |
try: |
| 77 |
seed = plugin.seed |
| 78 |
bucket_type = plugin.bucket_type |
| 79 |
if bucket_type != "none": |
| 80 |
_shuffle_items( |
| 81 |
items, |
| 82 |
bucket_key=bucket_type_keys[bucket_type], |
| 83 |
disable=_disable, |
| 84 |
seed=seed, |
| 85 |
session=session, |
| 86 |
) |
| 87 |
|
| 88 |
except Exception as e: |
| 89 |
|
| 90 |
_, _, exc_tb = sys.exc_info() |
| 91 |
failure = "pytest-random-order plugin has failed with {0!r}:\n{1}".format( |
| 92 |
e, "".join(traceback.format_tb(exc_tb, 10)) |
| 93 |
) |
| 94 |
if not hasattr(pytest, "PytestWarning"): |
| 95 |
config.warn(0, failure, None) |
| 96 |
else: |
| 97 |
warnings.warn(pytest.PytestWarning(failure)) |
| 98 |
|
| 99 |
finally: |
| 100 |
|
| 101 |
if item_ids != _get_set_of_item_ids(items): |
| 102 |
if not failure: |
| 103 |
failure = "pytest-random-order plugin has failed miserably" |
| 104 |
raise RuntimeError(failure) |