简体   繁体   中英

How to generate xml test report with stdout of unittest?

Assume I have a python tool to build and run unittest , eg python3 build_and_test ... .

Is there any way to parse stdout of unittest and generate xml(junit) report? I have refer to unittest-xml-reporting , but it seems that I must change my build_and_test code and I can't do that. How can I just parse the stdout of unittest and create xml reports?

You need to take 2 steps:

  1. Extend unittest.TextTestRunner and override _makeResult(). See https://docs.python.org/3.4/library/unittest.html#unittest.TextTestRunner._makeResult

  2. Implement your own version of unittest.TestResult. See https://docs.python.org/3.4/library/unittest.html#unittest.TestResult . Your code will be code upon various events. Example: When there is a testing error the methid addError will be called. See https://docs.python.org/3.4/library/unittest.html#unittest.TestResult.addError

Now you have control of the various events and you will be able to create the XML report.

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