【问题标题】:Python unittests in Jenkins?Jenkins 中的 Python 单元测试?
【发布时间】:2025-12-27 02:20:32
【问题描述】:

如何让 Jenkins 执行 python 单元测试用例? 是否可以从内置 unittest 包中输出 JUnit 样式的 XML?

【问题讨论】:

  • 所有答案都假定您想从命令行启动测试用例。但如果你想以编程方式运行测试,试试这个:import nose ; nose.runmodule() # aka nose.run(defaultTest=__name__)
  • 恕我直言,简单的“py.test --junitxml results.xml test.py”建议最好地回答了这个问题。 'yum install pytest' 安装 py.test。然后您可以运行任何 unittest python 脚本并获取 jUnit xml 结果
  • @gaoithe 回答了 jenkins 部分,但不满足使用内置 unittest 模块的要求。在那个项目中,这是一个给定的要求。
  • @erikb85 当我说“运行任何 unittest python 脚本”时,我指的是使用 unittest 模块的脚本。

标签: python unit-testing jenkins junit xunit


【解决方案1】:
python -m pytest --junit-xml=pytest_unit.xml source_directory/test/unit || true # tests may fail

从 jenkins 作为 shell 运行,你可以得到 pytest_unit.xml 中的报告作为工件。

【讨论】:

    【解决方案2】:

    样本测试:

    tests.py:

    # tests.py
    
    import random
    try:
        import unittest2 as unittest
    except ImportError:
        import unittest
    
    class SimpleTest(unittest.TestCase):
        @unittest.skip("demonstrating skipping")
        def test_skipped(self):
            self.fail("shouldn't happen")
    
        def test_pass(self):
            self.assertEqual(10, 7 + 3)
    
        def test_fail(self):
            self.assertEqual(11, 7 + 3)
    

    JUnit with pytest

    运行测试:

    py.test --junitxml results.xml tests.py
    

    结果.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <testsuite errors="0" failures="1" name="pytest" skips="1" tests="2" time="0.097">
        <testcase classname="tests.SimpleTest" name="test_fail" time="0.000301837921143">
            <failure message="test failure">self = &lt;tests.SimpleTest testMethod=test_fail&gt;
    
        def test_fail(self):
    &gt;       self.assertEqual(11, 7 + 3)
    E       AssertionError: 11 != 10
    
    tests.py:16: AssertionError</failure>
        </testcase>
        <testcase classname="tests.SimpleTest" name="test_pass" time="0.000109910964966"/>
        <testcase classname="tests.SimpleTest" name="test_skipped" time="0.000164031982422">
            <skipped message="demonstrating skipping" type="pytest.skip">/home/damien/test-env/lib/python2.6/site-packages/_pytest/unittest.py:119: Skipped: demonstrating skipping</skipped>
        </testcase>
    </testsuite>
    

    JUnit with nose

    运行测试:

    nosetests --with-xunit
    

    鼻子测试.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <testsuite name="nosetests" tests="3" errors="0" failures="1" skip="1">
        <testcase classname="tests.SimpleTest" name="test_fail" time="0.000">
            <failure type="exceptions.AssertionError" message="11 != 10">
                <![CDATA[Traceback (most recent call last):
    File "/opt/python-2.6.1/lib/python2.6/site-packages/unittest2-0.5.1-py2.6.egg/unittest2/case.py", line 340, in run
    testMethod()
    File "/home/damien/tests.py", line 16, in test_fail
    self.assertEqual(11, 7 + 3)
    File "/opt/python-2.6.1/lib/python2.6/site-packages/unittest2-0.5.1-py2.6.egg/unittest2/case.py", line 521, in assertEqual
    assertion_func(first, second, msg=msg)
    File "/opt/python-2.6.1/lib/python2.6/site-packages/unittest2-0.5.1-py2.6.egg/unittest2/case.py", line 514, in _baseAssertEqual
    raise self.failureException(msg)
    AssertionError: 11 != 10
    ]]>
            </failure>
        </testcase>
        <testcase classname="tests.SimpleTest" name="test_pass" time="0.000"></testcase>
        <testcase classname="tests.SimpleTest" name="test_skipped" time="0.000">
            <skipped type="nose.plugins.skip.SkipTest" message="demonstrating skipping">
                <![CDATA[SkipTest: demonstrating skipping
    ]]>
            </skipped>
        </testcase>
    </testsuite>
    

    JUnit with nose2

    您需要使用nose2.plugins.junitxml 插件。您可以像往常一样使用配置文件配置nose2,或者使用--plugin 命令行选项。

    运行测试:

    nose2 --plugin nose2.plugins.junitxml --junit-xml tests
    

    nose2-junit.xml:

    <testsuite errors="0" failures="1" name="nose2-junit" skips="1" tests="3" time="0.001">
      <testcase classname="tests.SimpleTest" name="test_fail" time="0.000126">
        <failure message="test failure">Traceback (most recent call last):
      File "/Users/damien/Work/test2/tests.py", line 18, in test_fail
        self.assertEqual(11, 7 + 3)
    AssertionError: 11 != 10
    </failure>
      </testcase>
      <testcase classname="tests.SimpleTest" name="test_pass" time="0.000095" />
      <testcase classname="tests.SimpleTest" name="test_skipped" time="0.000058">
        <skipped />
      </testcase>
    </testsuite>
    

    JUnit with unittest-xml-reporting

    将以下内容附加到tests.py

    if __name__ == '__main__':
        import xmlrunner
        unittest.main(testRunner=xmlrunner.XMLTestRunner(output='test-reports'))
    

    运行测试:

    python tests.py
    

    测试报告/TEST-SimpleTest-20131001140629.xml:

    <?xml version="1.0" ?>
    <testsuite errors="1" failures="0" name="SimpleTest-20131001140629" tests="3" time="0.000">
        <testcase classname="SimpleTest" name="test_pass" time="0.000"/>
        <testcase classname="SimpleTest" name="test_fail" time="0.000">
            <error message="11 != 10" type="AssertionError">
    <![CDATA[Traceback (most recent call last):
      File "tests.py", line 16, in test_fail
        self.assertEqual(11, 7 + 3)
    AssertionError: 11 != 10
    ]]>     </error>
        </testcase>
        <testcase classname="SimpleTest" name="test_skipped" time="0.000">
            <skipped message="demonstrating skipping" type="skip"/>
        </testcase>
        <system-out>
    <![CDATA[]]>    </system-out>
        <system-err>
    <![CDATA[]]>    </system-err>
    </testsuite>
    

    【讨论】:

    • +1 用于简单的“py.test --junitxml results.xml test.py”建议。 'yum install pytest' 安装 py.test。然后您可以运行任何 unittest python 脚本并获取 jUnit xml 结果。
    • 如果您想使用 unittest-xml-reporting 并从Test Discovery feature 中受益,可以输入unittest.main(module=None, testRunner=xmlrunner.XMLTestRunner(output='test-reports'))
    • @RosbergLinhares 为什么需要module=None 才能使用测试发现?它的工作原理与答案 unittest.main(testRunner=xmlrunner.XMLTestRunner(output='test-reports')) 中的描述完全相同。
    • @RosbergLinhares,在测试发现期间,模块只被导入但不被执行。那么,这些解决方案中的任何一个应该如何与发现一起使用呢?我刚试了一下,没有一个能用。还是我错过了什么?
    • @Konstantin,我还必须包含 module=None 才能正常运行。
    【解决方案3】:

    我会第二次使用鼻子。现在内置了基本的 XML 报告。只需使用 --with-xunit 命令行选项,它将生成一个 nosetests.xml 文件。例如:

    鼻子测试 --with-xunit

    然后在构建后添加一个“发布 JUnit 测试结果报告”操作,并使用 nosetests.xml 填写“测试报告 XMLs”字段(假设您在 $WORKSPACE 中运行了nosetests)。

    【讨论】:

      【解决方案4】:

      您可以安装unittest-xml-reporting 包以将生成XML 的测试运行器添加到内置unittest

      我们使用pytest,它内置了 XML 输出(它是一个命令行选项)。

      无论哪种方式,都可以通过运行 shell 命令来执行单元测试。

      【讨论】:

        【解决方案5】:

        在使用 buildout 时,我们使用 collective.xmltestreport 来生成 JUnit 样式的 XML 输出,可能是 source code 或者模块本身可能会有所帮助。

        【讨论】:

          【解决方案6】:

          我使用了鼻子测试。有插件可以为 Jenkins 输出 XML

          【讨论】: