0

I'm trying to run a TestCase in python 3.3.2 that has several test methods in it:

class ttt(unittest.TestCase):
    def setUp(self):
        ...

    def tearDown(self):
        ...

    def test_test1(self):
        ...      

    def test_test2(self):
        ...



if __name__ == "__main__":
    instance = ttt()
    instance.run()

The documentation states the following:

Each instance of TestCase will run a single base method: the method named methodName. However, the standard implementation of the default methodName, runTest(), will run every method starting with test as an individual test, and count successes and failures accordingly. Therefore, in most uses of TestCase, you will neither change the methodName nor reimplement the default runTest() method.

However, when I run the code I get the following:

'ttt' object has no attribute 'runTest'

I want to ask: Is this a bug? And if it's not why is there no runTest method? Am I doing something wrong?

1
  • I assume my identation fix didn't solve your issue (dunno why it would), secondly i assume unittest doesn't require you to manually creating def runTest(self) to specify the test-order?
    – Torxed
    Oct 16, 2013 at 11:08

1 Answer 1

3

When the unit test framework runs test cases, it creates an instance of the test class for each test.

I.e. to simulate what the unit test framework does you need to do:

if __name__ == "__main__":
    for testname in ["test_test1", "test_test2"]:
        instance = ttt(testname)
        instance.run()

The correct way to run unit tests in a module is:

if __name__ == "__main__":
    unittest.main()

... but I assume you know this already.

Regarding runTest: unittest.TestCase.__init__ signature and docstring is:

def __init__(self, methodName='runTest'):
    """Create an instance of the class that will use the named test
       method when executed. Raises a ValueError if the instance does
       not have a method with the specified name.
    """

Meaning that if you don't specify a test name in the constructor, the default is runTest.

1
  • 1
    Thanks for the clarification! Anyway, I think that the documentation is somehow misleading.
    – skrech
    Oct 17, 2013 at 7:33

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.