ChatGPT解决这个技术问题 Extra ChatGPT

Jenkins 中的 Python 单元测试?

你如何让 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 模块的脚本。

d
dnozay

样品测试:

测试.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)

带有 pytest 的 JUnit

运行测试:

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

运行测试:

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>

带nose2的JUnit

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

运行测试:

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

鼻子2-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>

带有 unittest-xml-reporting 的 JUnit

将以下内容附加到 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 才能正确运行。
N
Nakilon

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

鼻子测试--with-xunit

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


D
Dave Bacher

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

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

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


R
Rajib Mitra
python -m pytest --junit-xml=pytest_unit.xml source_directory/test/unit || true # tests may fail

从 jenkins 作为 shell 运行它,您可以在 pytest_unit.xml 中获取报告作为工件。


J
John La Rooy

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


M
Martijn Pieters

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