Subclassing unittest.TestCase?

R

Roy Smith

I'm using Python 2.6, with unittest2 imported as unittest.

I'm writing a test suite for a web application. There is a subclass of
TestCase for each basic page type. One thing that's in common between
all the pages is that every page must have a valid copyright notice. It
seems like the logical thing would be to put the common test(s) in a
subclass unittest.TestCase and have all my real test cases derive from
that:

class CommonTestCase(unittest.TestCase):
def test_copyright(self):
self.assert_(find copyright notice in DOM tree)

class HomepageTestCase(CommonTestCase):
def setUp(self):
self.url = "http://site.com"

def test_whatever(self):
self.assert_(whatever)

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.

The hack that I came up with is:

class CommonTestCase(unittest.TestCase):
def test_copyright(self):
if self.__class__.__name__ == 'CommonTestCase':
return
self.assert_(find copyright notice in DOM tree)

which works, but it's ugly. It also counts
CommonTestCase.test_copyright() as passing, which messes up the
statistics. 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?
 
P

Peter Otten

Roy said:
I'm using Python 2.6, with unittest2 imported as unittest.

I'm writing a test suite for a web application. There is a subclass of
TestCase for each basic page type. One thing that's in common between
all the pages is that every page must have a valid copyright notice. It
seems like the logical thing would be to put the common test(s) in a
subclass unittest.TestCase and have all my real test cases derive from
that:

class CommonTestCase(unittest.TestCase):
def test_copyright(self):
self.assert_(find copyright notice in DOM tree)

class HomepageTestCase(CommonTestCase):
def setUp(self):
self.url = "http://site.com"

def test_whatever(self):
self.assert_(whatever)

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.

The hack that I came up with is:

class CommonTestCase(unittest.TestCase):
def test_copyright(self):
if self.__class__.__name__ == 'CommonTestCase':
return
self.assert_(find copyright notice in DOM tree)

which works, but it's ugly. It also counts
CommonTestCase.test_copyright() as passing, which messes up the
statistics. 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?

Remove the base class from the module with

del CommonTestCase

before you invoke unittest.main().

Peter
 
R

Roy Smith

Peter Otten said:
Remove the base class from the module with

del CommonTestCase

before you invoke unittest.main().

Wow, what a clever idea! I tried it, and it does exactly what I need.
Thanks!
 
U

Ulrich Eckhardt

Roy said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top