Last active
February 16, 2019 19:07
-
-
Save whwkong/a86af03cabdc8de969fc732b54456f08 to your computer and use it in GitHub Desktop.
Using Mixins as fixtures for Python UnitTest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Consider the case of reusing 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. | |
| However, if you just want to extract the setUp(), then move the common setup code into a | |
| mixin. Make that your mixins do not have any tests. | |
| This pattern relies on each mixin's setUp() calling `super().setUp(), and that | |
| unittest.TestCase is the last parent class (TestCase does not invoke a call to super, | |
| as object.setUp() does not exist). | |
| See: | |
| https://stackoverflow.com/questions/1323455/python-unit-test-with-base-and-sub-class/17696807#17696807 | |
| https://nedbatchelder.com/blog/201210/multiple_inheritance_is_hard.html | |
| https://python-history.blogspot.com/2010/06/method-resolution-order.html | |
| # Output | |
| SecondMixin setUp | |
| FirstMixin setUp | |
| RealTest setUp | |
| RealTest test_foo | |
| SecondMixin tearDown | |
| FirstMixin tearDown | |
| RealTest tearDown | |
| . | |
| ---------------------------------------------------------------------- | |
| Ran 1 test in 0.000s | |
| OK | |
| """ | |
| 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