[PYTHON] Decorator for unit tests using random numbers

wrap up

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.

Source

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.

Execution result

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...

Recommended Posts

Decorator for unit tests using random numbers
I wrote unit tests for various languages
Give pytest clean parameters for flask unit tests
Python unit tests
How to make unit tests Part.2 Class design for tests