I've seen this question and this one but still am curious about how pyunit is meant to work for my use case.
I want to test the same method for multiple inputs. To do this, I created a class with several test methods, and two subclasses from it that have different setUp
methods.
Something like this:
class Test_Foo(unittest.TestCase):
# abstract class, do not actually add this to the test suite!
def test_01_preserves(self):
"""Foo preserves bazness correctly"""
self.assertTrue(is_preserved(self.foo.baz))
....
...
class Test_Foo_simple(Test_Foo):
"""test Foo for some simple input"""
def setUp(self):
self.input = ""
self.params = {}
self.foo = Foo(self.input, self.params)
class Test_Foo_complex(Test_Foo):
"""test Foo for some complex input"""
def setUp(self):
self.input = "complicated stuff"
self.params = {"bar" : 3}
self.foo = Foo(self.input, self.params)
...
followed by adding the second two classes to a TestSuite
and running that with TextTestRunner
.
Now the output will be:
Foo preserves bazness correctly ... ok
...
Foo preserves bazness correctly ... ok
...
with no obvious way to keep track of which test case is being run at which point.
How do I fix this?
What I'm doing at the moment is adding an __init__
method to each subclass that looks like:
super(Test_Foo_simple, self).__init__(self, *args, **kwds)
self._testMethodDoc = self._testMethodDoc + "(simple)"
which seems like an awful hack, and not guaranteed to work on future versions of pyunit. I could define another local variable with the desired docstring and subclass TextTestRunner
to use that instead, but that brings its own problems. Is there a better way around this?