Last active
February 16, 2019 19:07
-
-
Save whwkong/a86af03cabdc8de969fc732b54456f08 to your computer and use it in GitHub Desktop.
Revisions
-
whwkong revised this gist
Feb 16, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -37,7 +37,7 @@ def testSub1(self): https://python-history.blogspot.com/2010/06/method-resolution-order.html # Output SecondMixin setUp FirstMixin setUp RealTest setUp -
whwkong revised this gist
Feb 16, 2019 . 1 changed file with 9 additions and 3 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -23,14 +23,20 @@ def testSub1(self): 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 $ python fixture_test.py SecondMixin setUp FirstMixin setUp -
whwkong revised this gist
Feb 16, 2019 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -25,6 +25,7 @@ def testSub1(self): of the parent class. 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 However, if you just want to extract the setUp(), then move the common setup code into a -
whwkong revised this gist
Feb 16, 2019 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,5 @@ """ Consider the case of reusing the setUp() of a some base class that derives from unittest.TestCase. class BaseTest(unittest.TestCase): @@ -27,8 +27,8 @@ def testSub1(self): 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 setup code into a mixin. Make that your mixins do not have any tests. $ python fixture_test.py SecondMixin setUp -
whwkong revised this gist
Feb 16, 2019 . 1 changed file with 14 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -29,6 +29,20 @@ def testSub1(self): However, if you just want to extract the setUp(), then move the common code into a mixture. $ python fixture_test.py SecondMixin setUp FirstMixin setUp RealTest setUp RealTest test_foo SecondMixin tearDown FirstMixin tearDown RealTest tearDown . ---------------------------------------------------------------------- Ran 1 test in 0.000s OK """ import unittest -
whwkong created this gist
Feb 16, 2019 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,70 @@ """ 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()