Background
In Python's unittest
framework, it is a fairly common idiom to use inheritance on a base set of tests to apply an entire set of tests to a new problem, and occasionally to add additional tests. A trivial example would be:
from unittest import TestCase
class BaseTestCase(TestCase):
VAR = 3
def test_var_positive(self):
self.assertGreaterEqual(self.VAR, 0)
class SubTestCase(BaseTestCase):
VAR = 8
def test_var_even(self):
self.assertTrue(self.VAR % 2 == 0)
Which, when run, runs 3 tests:
$ python -m unittest -v
test_var_positive (test_unittest.BaseTestCase) ... ok
test_var_even (test_unittest.SubTestCase) ... ok
test_var_positive (test_unittest.SubTestCase) ... ok
----------------------------------------------------------------------
Ran 3 tests in 0.000s
This is particularly useful if you are testing a class hierarchy, where each subclass is a subtype of the parent classes, and should thus be able to pass the parent class's test suite in addition to its own.
Problem
I would like to switch over to using pytest
, but I have a lot of tests that are structured this way. From what I can tell, pytest
intends to replace most of the functionality of TestCase
classes with fixtures, but is there a pytest idiom that allows test inheritance, and if so what is it?
I am aware that pytest
can be used to run unittest
-style tests, but the support is limited, and I would like to use some of the "will never be supported" features of pytest
in my tests.