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.

Revisions

  1. whwkong revised this gist Feb 16, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mixins_as_fixtures.py
    Original 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
    $ python fixture_test.py
    # Output
    SecondMixin setUp
    FirstMixin setUp
    RealTest setUp
  2. whwkong revised this gist Feb 16, 2019. 1 changed file with 9 additions and 3 deletions.
    12 changes: 9 additions & 3 deletions mixins_as_fixtures.py
    Original 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
    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
    FirstMixin setUp
  3. whwkong revised this gist Feb 16, 2019. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions mixins_as_fixtures.py
    Original 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
  4. whwkong revised this gist Feb 16, 2019. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions mixins_as_fixtures.py
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    """
    Consider that you want to reuse the setUp() of a some base class that derives from
    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 code into a
    mixture.
    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
  5. whwkong revised this gist Feb 16, 2019. 1 changed file with 14 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions mixins_as_fixtures.py
    Original 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

  6. whwkong created this gist Feb 16, 2019.
    70 changes: 70 additions & 0 deletions mixins_as_fixtures.py
    Original 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()