I'm having trouble figuring out the proper way of using pythons unittest framework
I currently have 3 different implementations for a data structure class and unittests to test various things in the class as follows:
import fooHorse
import fooSnake
import fooMoose
import unittest
foo = fooHorse.foo()
#foo = fooSnake.foo()
#foo = fooMoose.foo()
class testFoo(unittest.TestCase):
def testSomething(self):
foo.do_something()
...
开发者_JS百科 def testSomethingelse(self):
...
# etc
if __name__ == "__main__":
unittest.main()
How do I refactor the code so that all the tests are run for fooSnake.foo
, fooMoose.foo
and fooHorse.foo
?
Just factor all the testing into a function and call it from the three tests :
class testFoo(unittest.TestCase):
def _doTest(self, foo):
foo.do_something()
# ...
def testFoorHorse(self):
foo = fooHorse.foo()
self._doTest(foo)
# and so on.
I wouldn't try to do anything more clever so that the test logic stays simple enought to be obviously bug-free.
I think it is better to keep the testing code inside the classes under test (so, inside the fooHorse
classes and such). I can't really tell from the code segment, so maybe you are already doing that. Then to combine multiple tests to be run at the same time, or selecting a few of them, you could create a separate testing class that creates a test suite. In there, the individual unittests can be assigned. See the python docs for details on this.
精彩评论