简体   繁体   English

如何使用unittest的stdout生成xml测试报告?

[英]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 ... . 假设我有一个python工具来构建和运行unittest ,例如python3 build_and_test ...

Is there any way to parse stdout of unittest and generate xml(junit) report? 有没有办法解析unittest stdout并生成xml(junit)报告? 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. 我已经参考了unittest-xml-reporting ,但似乎我必须更改build_and_test代码而我不能这样做。 How can I just parse the stdout of unittest and create xml reports? 我怎样才能解析unittest的stdout并创建xml报告?

You need to take 2 steps: 你需要采取两个步骤:

  1. Extend unittest.TextTestRunner and override _makeResult(). 扩展unittest.TextTestRunner并覆盖_makeResult()。 See https://docs.python.org/3.4/library/unittest.html#unittest.TextTestRunner._makeResult 请参阅https://docs.python.org/3.4/library/unittest.html#unittest.TextTestRunner._makeResult

  2. Implement your own version of unittest.TestResult. 实现您自己的unittest.TestResult版本。 See https://docs.python.org/3.4/library/unittest.html#unittest.TestResult . 请参阅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. 示例:当存在测试错误时,将调用methid addError。 See https://docs.python.org/3.4/library/unittest.html#unittest.TestResult.addError 请参阅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. 现在您可以控制各种事件,并且您将能够创建XML报告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 selenium 和 unittest 在 python 中生成测试报告? - How to generate a test report in python using selenium and unittest? Python 覆盖如何生成单元测试报告 - Python Coverage how to generate Unittest report Python unittest报告通过测试 - Python unittest report passed test 这是使用Python 3 unittest测试stdout的正确方法吗? - Is this a proper way to test stdout with Python 3 unittest? 如何在unittest中自动生成测试用例编号? - How can generate automatically test case number in unittest? Python unittest:如何临时将标准输出消息重定向到缓冲区并测试其内容? - Python unittest: How to temporarily redirect stdout messages to a buffer and to test its content? 如何在单元测试代码中提取stdout重定向以使其可被其他测试用例安全地重用? - How to extract stdout redirection in unittest code to make it safe reusable by other test cases? 是否可以在 python-unittest 中生成 Allure 报告? - Is it possible to generate Allure report in python-unittest? 如何生成XML格式的python单元测试报告以总结已通过和失败的测试用例 - how to generate python unittesting report in XML format to summarize passed and failed test cases 使用Python unittest-xml-reporting打印到stdout和XML文件 - Printing to stdout and XML file using Python unittest-xml-reporting
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM