Skip to content

Instantly share code, notes, and snippets.

@whwkong
Last active February 16, 2019 19:07
Show Gist options
  • Select an option

  • Save whwkong/a86af03cabdc8de969fc732b54456f08 to your computer and use it in GitHub Desktop.

Select an option

Save whwkong/a86af03cabdc8de969fc732b54456f08 to your computer and use it in GitHub Desktop.
Using Mixins as fixtures for Python UnitTest
"""
Consider that you want to reuse the setUp() of a some base class that derives from
unittest.TestCase.
class BaseTest(unittest.TestCase):
def setUp(self):
super().setUp()
print('BaseTest setUp')
def testCommon(self):
print('Calling BaseTest:testCommon')
self.assertTrue(True)
class SubTest1(BaseTest):
def setUp(self):
super().setUp()
print('SubTest1 setUp')
def testSub1(self):
print('Calling SubTest1:testSub1')
self.assertTrue(True)
The problem is that a class that inherits from another TestCase will run all the tests
of the parent class.
See:
https://stackoverflow.com/questions/1323455/python-unit-test-with-base-and-sub-class/17696807#17696807
However, if you just want to extract the setUp(), then move the common code into a
mixture.
"""
import unittest
class FirstMixin():
def setUp(self):
super().setUp()
print('FirstMixin setUp')
def tearDown(self):
super().tearDown()
print('FirstMixin tearDown')
class SecondMixin():
def setUp(self):
super().setUp()
print('SecondMixin setUp')
def tearDown(self):
super().tearDown()
print('SecondMixin tearDown')
class RealTest(FirstMixin, SecondMixin, unittest.TestCase):
def setUp(self):
super().setUp()
print('RealTest setUp')
def tearDown(self):
super().tearDown()
print('RealTest tearDown')
def test_foo(self):
print('RealTest test_foo')
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment