1
+ import random
1
2
import sys
2
3
import traceback
3
4
@@ -14,18 +15,34 @@ def pytest_addoption(parser):
14
15
choices = ('global' , 'package' , 'module' , 'class' ),
15
16
help = 'Limit reordering of test items across units of code' ,
16
17
)
18
+ group .addoption (
19
+ '--random-order-seed' ,
20
+ action = 'store' ,
21
+ dest = 'random_order_seed' ,
22
+ default = None ,
23
+ help = 'Seed for the test order randomiser to produce a random order that can be reproduced using this seed' ,
24
+ )
17
25
18
26
19
27
def pytest_configure (config ):
20
28
config .addinivalue_line ("markers" , "random_order(disabled=True): disable reordering of tests within a module or class" )
21
29
30
+ if config .getoption ('random_order_seed' ):
31
+ seed = str (config .getoption ('random_order_seed' ))
32
+ else :
33
+ seed = str (random .randint (1 , 1000000 ))
34
+ config .random_order_seed = seed
35
+
22
36
23
37
def pytest_report_header (config ):
24
- out = None
38
+ out = ''
25
39
26
40
if config .getoption ('random_order_bucket' ):
27
41
bucket = config .getoption ('random_order_bucket' )
28
- out = "Using --random-order-bucket={0}" .format (bucket )
42
+ out += "Using --random-order-bucket={}\n " .format (bucket )
43
+
44
+ if hasattr (config , 'random_order_seed' ):
45
+ out += 'Using --random-order-seed={}\n ' .format (getattr (config , 'random_order_seed' ))
29
46
30
47
return out
31
48
@@ -36,8 +53,9 @@ def pytest_collection_modifyitems(session, config, items):
36
53
item_ids = _get_set_of_item_ids (items )
37
54
38
55
try :
56
+ seed = getattr (config , 'random_order_seed' , None )
39
57
bucket_type = config .getoption ('random_order_bucket' )
40
- _shuffle_items (items , bucket_key = _random_order_item_keys [bucket_type ], disable = _disable )
58
+ _shuffle_items (items , bucket_key = _random_order_item_keys [bucket_type ], disable = _disable , seed = seed )
41
59
42
60
except Exception as e :
43
61
# See the finally block -- we only fail if we have lost user's tests.
0 commit comments