简体   繁体   中英

How to retrieve the TestResult object from a python unittest?

I would like to get the test results from my unit tests and then log them. Having some trouble figuring out the best way to do it. Ideally I think I would like to get them from the tearDown method, and then log them there, so that each test is logging it's result as it finishes, but I can't seem to get it to work.

Here is some example code that you can run:

import unittest

class sample_tests(unittest.TestCase):

    def test_it(self):
        self.assertTrue(1==2)


    def tearDown(self):
        print("Get test results and log them here")
        print(unittest.TestResult())


if __name__=='__main__':
    #unittest.main()
    suite = unittest.TestSuite()
    suite.addTest(sample_tests("test_it"))
    runner = unittest.TextTestRunner()
    result = runner.run(suite)
    print(result.failures)

When you run this you will get the following output:

Get test results and log them here
<unittest.result.TestResult run=0 errors=0 failures=0>
F
======================================================================
FAIL: test_it (__main__.sample_tests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File ".\sample.py", line 6, in test_it
    self.assertTrue(1==2)
AssertionError: False is not true

----------------------------------------------------------------------
Ran 1 test in 0.005s

FAILED (failures=1)
[(<__main__.sample_tests testMethod=test_it>, 'Traceback (most recent call last):\n  File ".\\sample.py", line 6, in test_it\n    self.assertTrue(1==2)\nAssertionError: False is not true\n')]
PS C:\Users\cn187366\Documents\Python_Test\ETL_Test_Framework>

As you can see, the tear down method is not returning the expected results and I think it is because I'm not referencing the test runner which contains the TestResults object.

EDIT I've found a solution here: Getting Python's unittest results in a tearDown() method

Here is the actual code that does what I wanted:

def tearDown(self):
    print("Get test results and log them here")

    if hasattr(self,'_outcome'):
        result = self.defaultTestResult()
        self._feedErrorsToResult(result,self._outcome.errors)
        print(result)

This should work!

import xmlrunner
with open('test-reports/result.xml', 'wb') as output:
    unittest.main(testRunner=xmlrunner.XMLTestRunner(output=output),
              failfast=False, buffer=False, catchbreak=False)

Alternative:

def tearDown(self)
    super(sample_tests, self).tearDown()
    with open('result.txt', 'w+') as output:
        test_failed = self._outcomes.errors
        output.write(test_failed)

You can stream to a file with stream=file-name.log . For more detail, check the unittest.TextTestRunner class.

if __name__ == '__main__':
  suite = unittest.TestLoader().loadTestsFromTestCase(YourTestClass)
  with open('test_result.out', 'w') as f:
    unittest.TextTestRunner(stream=f, verbosity=2).run(suite)

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