简体   繁体   English

无法使用 xmlrunner for unittest 打印日志输出

[英]Unable to print logging output using xmlrunner for unittest

I am trying to use xmlrunner to generate a xml of my test result with unittest.我正在尝试使用 xmlrunner 用 unittest 生成我的测试结果的 xml。 I would like the report to include my logging output.我希望报告包含我的日志输出。 But it looks like it doesn't work.但它看起来不起作用。

For example, in this code, the logging output is not showing in the xml report.例如,在此代码中,日志输出未显示在 xml 报告中。

import unittest
import logging
import xmlrunner
import sys


class Test(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        logging.getLogger().debug("setUpClass")

    def test_step_001(self):
        logging.getLogger().debug("test_step_001")

    @classmethod
    def tearDownClass(cls):
        logging.getLogger().debug("tearDownClass")


if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG, format="%(asctime)s %(levelname)s %(message)s", stream=sys.stdout)
    unittest.main(testRunner=xmlrunner.XMLTestRunner(output='test-reports'))

I just worked on this.我刚刚在做这件事。 Its very stupid.它非常愚蠢。 You would setup your loghandler inside your setup function.您将在设置函数中设置日志处理程序。 This creates a hierarchical seperation which runs internally in xmlrunner.这将创建一个在 xmlrunner 内部运行的分层分离。

    @classmethod
    def setUpClass(cls):
        handler = logging.StreamHandler(sys.stdout)
        handler.setLevel(logging.INFO)
        log.addHandler(handler)
        log.debug("setUpClass")

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

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