File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import hashlib
12import random
23
4+ import pytest
35import torch
46
57
6- def pytest_configure ():
7- _set_random_seed (42 )
8+ def pytest_collectstart (collector ):
9+ if isinstance (collector , pytest .Module ):
10+ _set_random_seed (_hash (collector .name ))
11+
12+
13+ @pytest .fixture (scope = "module" , autouse = True )
14+ def set_seed_per_module (request ):
15+ _set_random_seed (_hash (_module_path_from_request (request )))
16+
17+
18+ @pytest .fixture (autouse = True )
19+ def set_seed_per_test (request ):
20+ _set_random_seed (_hash (_test_case_path_from_request (request )))
821
922
1023def _set_random_seed (seed ):
1124 random .seed (seed )
1225 torch .manual_seed (seed )
26+
27+
28+ def _test_case_path_from_request (request ):
29+ return f"{ _module_path_from_request (request )} ::{ request .node .name } "
30+
31+
32+ def _module_path_from_request (request ):
33+ return f"{ request .module .__name__ .replace ('.' , '/' )} .py"
34+
35+
36+ def _hash (string ):
37+ return int (hashlib .sha256 (string .encode ("utf-8" )).hexdigest (), 16 ) % 2 ** 32
You can’t perform that action at this time.
0 commit comments