A decorator that displays "Don't rush, try again" when a unit test that fails with a certain probability because a random number is used fails.
randtest.py
import unittest
import numpy as np
def statistical(test):
def do_test(self):
try:
test(self)
except AssertionError as e:
e.args += ("NOTE: this is a statistical test, which may fail.", )
raise
return do_test
class TestHoge(unittest.TestCase):
@statistical
def test_normal(self):
val = np.random.uniform(0, 1, 1)
self.assertTrue(val[0] < 0.8) #Fail about 2 times in 10 times
if __name__ == "__main__":
unittest.main()
Define a decorator called statistical and put @statistical before testing with random numbers.
python
bash-3.2$ for i in $(seq 10); do ./randtest.py ; done
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
F
======================================================================
FAIL: test_normal (__main__.TestHoge)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./randtest.py", line 14, in do_test
test(self)
File "./randtest.py", line 24, in test_normal
self.assertTrue(val[0] < 0.8)
AssertionError: ('False is not true', 'NOTE: this is a statistical test, which may fail.')
----------------------------------------------------------------------
Ran 1 test in 0.006s
FAILED (failures=1)
...The following is omitted...