简体   繁体   中英

python Amount of failed tests, errors and successes Unittest

I want to make a pie chart of test statistics - -how many passed and failed.

How to get the amount of tests that passed and failed?

I'm using python 2.7 so _outcomeForDoCleanups doesn't work

Assuming that you have a number of tests defined in a subclass of unittest.TestCase , you can create a test suite, a TestResult object, and then run the tests:

import unittest

class TestThings(unittest.TestCase):
    def test_success(self):
        self.assertEqual(1, 1)

    def test_fail(self):
        self.assertEqual(1, 2)

    def test_error(self):
        1/0

if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(TestThings)
    results = unittest.TestResult()
    suite.run(results)
    print results

Output:

<unittest.result.TestResult run=3 errors=1 failures=1>

You can use the results object to get the stats:

>>> results.testsRun
3
>>> results.errors
[(<__main__.TestThings testMethod=test_error>, 'Traceback (most recent call last):\n  File "ut2.py", line 11, in test_error\n    1/0\nZeroDivisionError: integer division or modulo by zero\n')]
>>> num_errors = len(results.errors)
>>> num_errors
1
>>> results.failures
[(<__main__.TestThings testMethod=test_fail>, 'Traceback (most recent call last):\n  File "ut2.py", line 8, in test_fail\n    self.assertEqual(1, 2)\nAssertionError: 1 != 2\n')]
>>> num_failures = len(results.failures)
>>> num_failures
1
>>> num_successful = results.testsRun - num_failures - num_errors
>>> num_successful
1

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