Reputation: 477
I wanted to call setUpClass
and tearDownClass
so that setup
and teardown
would be performed only once for each test. However, it keeps failing for me when I call tearDownClass
. I only want to record 1 test result, either PASS if both tests passed or FAIL if both tests failed. If I call only setup
and tearDown
then all works fine:
Calling setUpClass
and tearDownClass
:
#!/usr/bin/python
import datetime
import itertools
import logging
import os
import sys
import time
import unittest
LOGFILE = 'logfile.txt'
class MyTest(unittest.TestCase):
global testResult
testResult = None
@classmethod
def setUpClass(self):
## test result for DB Entry:
self.dbresult_dict = {
'SCRIPT' : 'MyTest.py',
'RESULT' : testResult,
}
def test1(self):
expected_number = 10
actual_number = 10
self.assertEqual(expected_number, actual_number)
def test2(self):
expected = True
actual = True
self.assertEqual(expected, actual)
def run(self, result=None):
self.testResult = result
unittest.TestCase.run(self, result)
@classmethod
def tearDownClass(self):
ok = self.testResult.wasSuccessful()
errors = self.testResult.errors
failures = self.testResult.failures
if ok:
self.dbresult_dict['RESULT'] = 'Pass'
else:
logging.info(' %d errors and %d failures',
len(errors), len(failures))
self.dbresult_dict['RESULT'] = 'Fail'
if __name__ == '__main__':
logger = logging.getLogger()
logger.addHandler(logging.FileHandler(LOGFILE, mode='a'))
stderr_file = open(LOGFILE, 'a')
runner = unittest.TextTestRunner(verbosity=2, stream=stderr_file, descriptions=True)
itersuite = unittest.TestLoader().loadTestsFromTestCase(MyTest)
runner.run(itersuite)
sys.exit()
unittest.main(module=itersuite, exit=True)
stderr_file.close()
Error:
test1 (__main__.MyTest) ... ok
test2 (__main__.MyTest) ... ok
ERROR
===================================================================
ERROR: tearDownClass (__main__.MyTest)
-------------------------------------------------------------------
Traceback (most recent call last):
File "testTearDownClass.py", line 47, in tearDownClass
ok = self.testResult.wasSuccessful()
AttributeError: type object 'MyTest' has no attribute 'testResult'
----------------------------------------------------------------------
Ran 2 tests in 0.006s
FAILED (errors=1)
Upvotes: 1
Views: 9005
Reputation: 133
like @Marcin already pointed out, you're using the Unittest-Framework in a way it isn't intended.
assertEqual(given, expected)
. Unittest will then collect a summary of failed ones. you don't have to do this manually.setUp()
Will be called before each test-methodtearDown()
Will be called after each test-methodsetUpClass()
Will be called once per class (before the first test-method of this class)tearDownClass()
Will be called once per class (after the last test-method of this class)Here's the official documentation
Here's a related answer
Upvotes: 0
Reputation: 34
Change tearDownClass(self)
to tearDownClass(cls)
and setUpClass(self)
to setUpClass(cls)
.
Upvotes: -4