简体   繁体   中英

How to use Python Unittest TearDownClass with TestResult.wasSuccessful()

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)

like @Marcin already pointed out, you're using the Unittest-Framework in a way it isn't intended.

  1. To see if the tests are successful you check the given values with the expected, like you already did: assertEqual(given, expected) . Unittest will then collect a summary of failed ones. you don't have to do this manually.
  2. If you want to check that two tests need to be together successful or fail together, these should be combined in ONE Test, maybe as a additionally one, if the individual Tests need to be checked as well. This is nothing you want to save and load afterwards. The tests itself should be as stateless as possible.
  3. When you say you want to run the SetUp and TearDown 'once per test', do you mean once per test-method or per test-run? This is different if you have more than one test-method inside your class:
  • setUp() Will be called before each test-method
  • tearDown() Will be called after each test-method
  • setUpClass() 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

tearDownClass(self)更改为tearDownClass(cls) ,将setUpClass(self)更改为setUpClass(cls)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM