当前位置: 首页 > 面试题库 >

Jenkins中的Python单元测试?

宗政霄
2023-03-14
问题内容

您如何让Jenkins执行python unittest案例?是否可以从内置unittest包中输出JUnit样式的XML ?


问题答案:

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)

[带有pytest的JUnit](http://pytest.org/latest/usage.html#creating-junitxml-

format-files)

使用以下命令运行测试:

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

results.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>

带nose的JUnit

使用以下命令运行测试:

nosetests --with-xunit

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

带有nose的JUnit2

您将需要使用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>

[具有unittest-xml-reporting的JUnit](https://github.com/danielfm/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>


 类似资料:
  • 这是我在Python中的第一个项目,我刚刚学习了框架。测试模块运行良好,当我做,但当我想执行某个类或方法,如留档中所说,使用: 我得到以下错误: 我运行命令的目录包含(我还尝试将名称更改为),带有和方法都以开头,这里是我在终端上运行的命令: 我在任何地方都找不到类似的问题,知道错误背后的原因以及如何在模块或方法内部运行某个类将是非常好的

  • 问题内容: 有人与Jenkins CI一起使用过cFix(Visual Assert)吗?如何设置?如何通过Jenkins执行C ++项目测试(Testt Framework cFix {isualAssert)? 任何指针!谢谢 问题答案: 我写了一个小程序将cFix输出转换为XML。我将jenkins识别的最简单的Junit XML格式用于程序输出。

  • 问题内容: 第3、16、17、18和19行用*突出显示的含义是什么。有人可以解释他们的工作吗?我是python和编程新手 问题答案: 此代码中只有三行用*突出显示,但这是它们的含义: 第一行: 这是在声明 后面 的函数( test_Iframe 和 tearDown )的 类* 。在面向对象的编程中,使用一个类来创建“对象” 。可以将 类 视为数据/过程的抽象,而 对象 是类的特定实例。 ***

  • 我正在设置我的第一个Jenkins服务器来构建和测试现有的maven项目,而jenkins无法完成单元测试。 当我从命令行运行“mvn清洁安装”时,所有模块都会构建并通过它们的单元测试。然而,Jenkins只能运行第一组单元测试(通过),然后以某种方式出错。 我用的是詹金斯1.499。 以下是来自顶级pom报告插件的一些相关信息:

  • 问题内容: 我在该领域做了一些谷歌搜索,发现了很多有关让Jenkins理解boost.test的XML输出格式的讨论,但是没有规范的参考。 有人说我们只需要使用XLST来转换XML格式,另一些人则认为XML在此之前需要进行一些修改有人拥有XSL来将Boost.TestXML日志转换为可显示的格式吗?)。有些人建议xUnit插件可以原生理解boost.test XML格式,另一些人则认为它不能 因为

  • 问题内容: 如何在Swift中为代码路径实现单元测试? 例如,我有以下快速代码 我想对y = 0的情况进行单元测试。 注意,我要使用fatalError而不是其他任何断言函数。 问题答案: Nimble(“用于Swift和Objective- C的Matcher框架”)得到了支持: 迅捷断言 如果使用的是Swift,则可以使用throwAssertion匹配器检查是否抛出了断言(例如fatalEr