[TIP] Inheritance of TestClasses?
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
> 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?
Based on this test code, inheritance works, at least sometimes:
"""A quick test to see if pytest can deal with an exceptionally simple
print("In TestClass1's setup_method()")
print("In TestClass1's teardown_method()")
print("In TestClass2's setup_method()")
print("In TestClass2's teardown_method()")
So now I'm left wondering: Why doesn't it work in my large project?
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the testing-in-python