[TIP] How do I run the same test on many different classes?

Matthew Wilson matt at tplus1.com
Sun Dec 13 11:45:55 PST 2009


This is one of those problems that I know how to solve in an ugly way,
but I want to learn a better way.

I need to write a class that acts like a dictionary with some other
tweaks bolted on.

I wrote a single unittest.TestCase class that tests for all the
features I care about.  Pretend that the TestCase subclass is called
TestTask and the class I want to make will be named Task.

I have several ideas about how to build this class and I want to use
the TestTask class as a pass-fail filter.  Pretend I'll name each idea
Task1, Task2, Task3, etc.

Then I want to pass Task1, Task2, etc into unittest.TestCase so it
would run the tests on an instance of that class.

I experimented with something like this:

    >>> import task, test_task, unittest
    >>> Task = task.Task1
    >>> tl = unittest.TestLoader()
    >>> tl.loadTestsFromModule(test_task)
    >>> suite = tl.loadTestsFromModule(test_task)
    >>> unittest.TextTestRunner().run(suite)

Of course, that blows up because my test_task.TestTask class depends
on a global Task class that isn't defined when I defined that module.

All feedback is welcome.

Matt



-- 
W. Matthew Wilson
matt at tplus1.com
http://tplus1.com



More information about the testing-in-python mailing list