I'm trying to create my own decorator for a python unit test My goal is to wrap an entire unittest TestCase class and apply certain modifications to it. For some reason, however, the wrapped class seems to be invisible to the test framework.
For these particular tests, I've been trying to use the python built in unittest framework. I've also tried using pytest and following along with this stackoverflow post, but I was still unable to get my decorator to work.
Here is what I've tried:
# test_decorator.py
import unittest
import functools
def wrapper_for_test(test_case):
@functools.wraps(test_case)
def wrapper(*args, **kwargs):
return test_case(*args, **kwargs)
return wrapper
@wrapper_for_test
class TestClass(unittest.TestCase):
def test_example(self):
self.assertTrue(False)
I then run the following:
$ python -m unittest test_decorator.py
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
And the test is not found.
But when I comment out @wrapper_for_test, I get:
$ python -m unittest test_decorator.py
FAIL: test_example (test_decorator.TestClass)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\...\test_decorator.py", line 78, in test_example
self.assertTrue(False)
AssertionError: False is not true
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (failures=1)
The test fails (as expected).
Why isn't the test found when I use the decorator? How can I wrap a unittest TestCase class and have it be found?