[TIP] Inheritance of TestClasses?

Dan Stromberg drsalists at gmail.com
Tue Sep 3 15:35:27 PDT 2019


On Tue, Sep 3, 2019 at 2:05 PM Dan Stromberg <drsalists at gmail.com> wrote:

>
> Hi folks.
>
> I'm still working on converting that large codebase from unittest to
> pytest (in parallel with a couple of other projects).
>
> I was able to get past the runTest issue I was seeing, with the great help
> on this list.  It turned out to be because we were using unittest.TestCase
> and:
>       runner = unittest.TextTestRunner(verbosity=3)
>
>       result = runner.run(test_suite)
>
>
> Now I have a new question: Does pytest allow one test class to inherit
> from another?
>
> I'm asking, because our unittest test cases use inheritance a lot.  With
> our somewhat strange use of runTest, this was working well enough, but with
> pytest it appears to be a problem.
>
> Specifically, we have lots of things like:
>       def setup_method(self):
>
>           super(TestFeatures, self).setup_method()
>
> And we -had- a lot of:
>       def __init__(self):
>           super(TestFeatures, self).__init__()
> ...but I eliminated those by merging them into setup_method().  I did this
> because pytest apparently skips any test class that has an __init__ method.
>
> However, now from our many setup_method's I'm getting things like:
> >       super(FeatureElasticSearchAwareTestCase, self).setup_method()
> E       AttributeError: 'super' object has no attribute 'setup_method'
>
> And I chased up the inheritance hierarchy, and found that every super
> class but object itself has a .setup_method().  This is a 2.x and 3.x
> codebase, so that's why it cares about object.
>
> So I ask: does pytest eliminate the possilbity of using an inheritance
> hierarchy in the test code? Or is this another oddity of the codebase I'm
> working on?
>
> Thanks!
>

Based on this test code, inheritance works, at least sometimes:

#!/usr/bin/env python3.7

"""A quick test to see if pytest can deal with an exceptionally simple
inheritance hierarchy."""


class TestClass1(object):
    """Test 1."""
    def setup_method(self):
        print("In TestClass1's setup_method()")

    def teardown_method(self):
        print("In TestClass1's teardown_method()")

    def test_1(self):
        print('In test1')
        raise ValueError


class TestClass2(TestClass1):
    """Test 2."""
    def setup_method(self):
        print("In TestClass2's setup_method()")
        super(TestClass2, self).setup_method()

    def teardown_method(self):
        print("In TestClass2's teardown_method()")
        super(TestClass2, self).teardown_method()

    def test_2(self):
        print('In test2')
        raise ValueError


So now I'm left wondering: Why doesn't it work in my large project?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20190903/dcf58de6/attachment.htm>


More information about the testing-in-python mailing list