Subclassing unittest.TestCase?

Ulrich Eckhardt eckhardt at satorlaser.com
Thu Nov 4 03:44:01 EDT 2010


Roy Smith wrote:
> I'm writing a test suite for a web application.  There is a subclass of
> TestCase for each basic page type. 
[...]
> class CommonTestCase(unittest.TestCase):
>    def test_copyright(self):
>       self.assert_(find copyright notice in DOM tree)
> 
> class HomepageTestCase(CommonTestCase):
[...]
> This works fine as far as HomepageTestCase running test_copyright() and
> test_whatever().  The problem is that CommonTestCase *also* runs
> test_copyright(), which fails because there's no setUp(), and thus no
> retrieved page for it to work on.

Rename the function to _test_copyright and then in the derived class either
call it or just

  test_copyrigth = CommonTestCase._test_copyright


> Is there a cleaner way to define some common test methods which all of
> my test cases can inherit, without having them be run in the base class?

I haven't tried it, but you could put the test into a separate baseclass
that doesn't derive from unittest.TestCase. All tests that pull a page and
need to check for this then add this class to the baseclasses.

Uli

-- 
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932




More information about the Python-list mailing list