Skip to content

Commit 669818d

Browse files
committed
Use random.seed for deterministic doctest outputs
1 parent 1af2089 commit 669818d

File tree

3 files changed

+38
-28
lines changed

3 files changed

+38
-28
lines changed

patterns/other/blackboard.py

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -90,30 +90,37 @@ def contribute(self):
9090
self.blackboard.common_state['progress'] += random.randint(10, 100)
9191

9292

93+
def main():
94+
"""
95+
>>> blackboard = Blackboard()
96+
>>> blackboard.add_expert(Student(blackboard))
97+
>>> blackboard.add_expert(Scientist(blackboard))
98+
>>> blackboard.add_expert(Professor(blackboard))
99+
100+
>>> c = Controller(blackboard)
101+
>>> contributions = c.run_loop()
102+
103+
>>> from pprint import pprint
104+
>>> pprint(contributions)
105+
['Student',
106+
'Student',
107+
'Student',
108+
'Student',
109+
'Scientist',
110+
'Student',
111+
'Student',
112+
'Student',
113+
'Scientist',
114+
'Student',
115+
'Scientist',
116+
'Student',
117+
'Student',
118+
'Scientist',
119+
'Professor']
120+
"""
121+
122+
93123
if __name__ == '__main__':
94-
blackboard = Blackboard()
95-
96-
blackboard.add_expert(Student(blackboard))
97-
blackboard.add_expert(Scientist(blackboard))
98-
blackboard.add_expert(Professor(blackboard))
99-
100-
c = Controller(blackboard)
101-
contributions = c.run_loop()
102-
103-
from pprint import pprint
104-
105-
pprint(contributions)
106-
107-
### OUTPUT ###
108-
# ['Student',
109-
# 'Student',
110-
# 'Scientist',
111-
# 'Student',
112-
# 'Scientist',
113-
# 'Student',
114-
# 'Scientist',
115-
# 'Student',
116-
# 'Scientist',
117-
# 'Student',
118-
# 'Scientist',
119-
# 'Professor']
124+
random.seed(1234) # for deterministic doctest outputs
125+
import doctest
126+
doctest.testmod()

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
pytest~=4.3.0
44
pytest-cov~=2.6.0
55
flake8~=3.7.0
6+
pytest-randomly~=3.1.0

tox.ini

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ deps =
99
-r requirements-dev.txt
1010
commands =
1111
flake8 patterns/
12-
pytest --doctest-modules patterns/
13-
pytest -s -vv --cov={envsitepackagesdir}/patterns --log-level=INFO tests/
12+
; `randomly-seed` option from `pytest-randomly` helps with deterministic outputs for examples like `other/blackboard.py`
13+
pytest --randomly-seed=1234 --doctest-modules patterns/
14+
; `-p no:randomly` turns off `randomly` plugin for unit tests
15+
pytest -s -vv --cov={envsitepackagesdir}/patterns --log-level=INFO -p no:randomly tests/
1416

1517

1618
[testenv:cov-report]

0 commit comments

Comments
 (0)