栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Jenkins中的Python单元测试?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Jenkins中的Python单元测试?

样本测试:

tests.py:

# tests.pyimport randomtry:    import unittest2 as unittestexcept importError:    import unittestclass 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 != 10tests.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 runtestMethod()File "/home/damien/tests.py", line 16, in test_failself.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 assertEqualassertion_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 _baseAssertEqualraise 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>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/514867.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号