Reputation: 127
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?
Upvotes: 0
Views: 339
Reputation: 100766
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
.
Upvotes: 3