1

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()
    ...
    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?

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.