如何让Jenkins执行python单元测试用例?
是否可以从内置的unittest包中输出JUnit风格的XML
样本测试:
tests.py:
#tests.py
随机输入
尝试:
将unittest2导入为unittest
除恐怖外:
导入单元测试
类SimpleTest(unittest.TestCase):
@unittest.skip(“演示跳过”)
已跳过def测试(自身):
自我失败(“不应该发生”)
def测试通过(自我):
自评资格(10,7+3)
def测试失败(自身):
自评资格(11,7+3)
JUnit与pytest
使用以下工具运行测试:
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\u fail”time=“0.000301837921143”>;
<;失败消息=“测试失败”>;自我=&;书信电报;tests.SimpleTest testMethod=测试失败&;燃气轮机;
def测试失败(自身):
&;燃气轮机;自评资格(11,7+3)
E断言错误:11!=10
测试。py:16:AssertionError</失败>;
</测试用例>;
<;testcase classname=“tests.SimpleTest”name=“test\u pass”time=“0.000109910964966”/>;
<;testcase classname=“tests.SimpleTest”name=“test\u跳过”time=“0.000164031982422”>;
<;跳过消息=“演示跳过”type=“pytest.skip”>/home/damien/test-env/lib/python2.6/site-packages/_-pytest/unittest.py:119:跳过:演示跳过</跳过>;
</测试用例>;
</测试套件>;
带鼻杜松
使用以下工具运行测试:
鼻子测试——使用xunit
nosetests.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\u fail”time=“0.000”>;
<;故障类型=“异常.断言错误”消息=“11!=10”>;
<;![CDATA[回溯(最近一次通话最后一次):
文件“/opt/python-2.6.1/lib/python2.6/site packages/unittest2-0.5.1-py2.6.egg/unittest2/case.py”,第340行,运行中
testMethod()
文件“/home/damien/tests.py”,第16行,测试失败
自评资格(11,7+3)
assertEqual中的文件“/opt/python-2.6.1/lib/python2.6/site packages/unittest2-0.5.1-py2.6.egg/unittest2/case.py”,第521行
断言函数(第一,第二,msg=msg)
文件“/opt/python-2.6.1/lib/python2.6/site packages/unittest2-0.5.1-py2.6.egg/unittest2/case.py”,第514行,位于baseAssertEqual中
引发自我失败异常(msg)
断言错误:11!=10
]]&燃气轮机;
</失败>;
</测试用例>;
<;testcase classname=“tests.SimpleTest”name=“test\u pass”time=“0.000”></测试用例>;
<;testcase classname=“tests.SimpleTest”name=“test\u跳过”time=“0.000”>;
<;跳过类型=“nose.plugins.skip.SkipTest”消息=“演示跳过”>;
<;![CDATA[SkipTest:演示跳过
]]&燃气轮机;
</跳过>;
</测试用例>;
</测试套件>;
带鼻杜松
您需要使用nos2.plugins.junitxml插件。您可以像通常一样使用配置文件配置nos2,或者使用--plugin命令行选项配置
使用以下工具运行测试:
nos2-plugin-nos2.plugins.junitxml-junitxml测试
nos2-junit.xml:
<;testsuite errors=“0”failures=“1”name=“nos2 junit”skips=“1”tests=“3”time=“0.001”>;
<;testcase classname=“tests.SimpleTest”name=“test\u fail”time=“0.000126”>;
<;失败消息=“测试失败”>;回溯(最近一次呼叫最后一次):
文件“/Users/damien/Work/test2/tests.py”,第18行,测试失败
自评资格(11,7+3)
断言者错误:11!=10
</失败>;
</测试用例>;
<;testcase classname=“tests.SimpleTest”name=“test\u pass”time=“0.000095”/>;
<;testcase classname=“tests.SimpleTest”name=“test\u跳过”time=“0.000058”>;
<;跳过/>;
</测试用例>;
</测试套件>;
使用UnitTestXML报告的JUnit
将以下内容附加到tests.py
如果uuuu name uuuuuu=='\uuuuuu main\uuuuuuu':
导入xmlrunner
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=“测试通过”time=“0.000”/>;
<;testcase classname=“SimpleTest”name=“测试失败”time=“0.000”>;
<;错误消息=“11!=10”type=“AssertionError”>;
<;![CDATA[回溯(最近一次通话最后一次):
文件“tests.py”,第16行,测试失败
自评资格(11,7+3)
断言错误:11!=10
]]&燃气轮机</错误>;
</测试用例>;
<;testcase classname=“SimpleTest”name=“test\u跳过”time=“0.000”>;
<;跳过消息=“演示跳过”type=“跳过”/>;
</测试用例>;
<;系统输出>;
<;![CDATA[]]></系统输出>;
<;系统错误>;
<;![CDATA[]]></系统错误>;
</测试套件>;