我有一个验收测试时,结果是纯文本。 我想用詹金斯显示结果,和JUnit格式是适合我。
所以,我要检查是否有现有的Python代码生成JUnit格式的XML,这样我就可以很容易地再补充我的解析代码。
相关的问题 。
我有一个验收测试时,结果是纯文本。 我想用詹金斯显示结果,和JUnit格式是适合我。
所以,我要检查是否有现有的Python代码生成JUnit格式的XML,这样我就可以很容易地再补充我的解析代码。
相关的问题 。
上述科瑞建议junitxml,但我是在为在larrycai我不写单元测试来测试Python代码在同一条船上。 我正在写Python脚本做黑匣子系统的测试,只是想在JUnit的XML输出结果,而不重新发明轮子。
我简要地看着由larrycai上述建议的大卫·布莱克的“Python的JUnit XML输出模块”,但最终与另一个类似的包去。 不知道哪个更好,因为我只试过这一个,但它结束了对我很好地工作。
只有一个字符,但包装不同的是,“基于JUnit XML”: https://pypi.python.org/pypi/junit-xml/1.0
当心......在他的自述文件中的例子有错误和不工作。 我报在GitHub上的错误(包括页面的PyPI在github上的链接)。 还有一个与他的“prettyprint” ARG处理的错误,但我将把你发出#3,我也GitHub上,其中包括我我修复报告。 如果你下载的源,你可以看看他的test.py单元测试,但这里也是,我测试了我的测试脚本/用几个例子(使用Python 3.3)试验:
#junit-xml 1.0 downloaded from https://pypi.python.org/pypi/junit-xml
from junit_xml import TestSuite, TestCase
#Good article that has examples of how Jenkins parses JUnit XML to display output:
#http://nelsonwells.net/2012/09/how-jenkins-ci-parses-and-displays-junit-output/
#One version of JUnit XML schema: http://windyroad.org/dl/Open%20Source/JUnit.xsd
def testBasicToConsole():
''' Perform the very basic test with 1 suite and 1 test case, output to console.
This is the example from the above referenced pypi webpage, but corrected to
actually work.
'''
test_cases = [TestCase('Test1', 'some.class.name', 123.345, 'I am stdout!', 'I am stderr!')]
ts = [TestSuite("my test suite", test_cases)]
# pretty printing is on by default but can be disabled using prettyprint=False
print(TestSuite.to_xml_string(ts, prettyprint=False))
def testBasicInfoToConsole():
''' Actually, even more basic than the test above, with classname, stdout, and stderror
removed to demonstrate they are optional. For system testing we often won't use them.
Output to console.
'''
test_cases = [TestCase('PathCheck: ApplicationControl', '', .0523, '', '')]
ts = [TestSuite("DirectorITG2", test_cases)]
# pretty printing is on by default but can be disabled using prettyprint=False
print(TestSuite.to_xml_string(ts))
def testFailureInfoToConsole():
''' 1 suite and test case with failure info added. Output to console.
'''
test_cases = TestCase('FileCheck: DesktopNotificationCenter', '', .0451, '', '')
test_cases.add_failure_info('Invalid File \'DNC.exe\'.')
ts = [TestSuite("DirectorITG2", [test_cases])]
# pretty printing is on by default but can be disabled using prettyprint=False
print(TestSuite.to_xml_string(ts))
def testMultiTestCasesToConsole():
''' Demonstrates a single test suite with multiple test cases, one of which
has failure info. Output to console.
'''
test_cases = [TestCase('FileCheck: DesktopNotificationCenter', '', .0451, '', '')]
test_cases.append(TestCase('FileCheck: PropertyServer', '', .0452, '', ''))
test_cases[0].add_failure_info('Invalid File \'DNC.exe\'.')
ts = [TestSuite("DirectorITG2", test_cases)]
# pretty printing is on by default but can be disabled using prettyprint=False
print(TestSuite.to_xml_string(ts))
def testMultiTestSuitesToConsole():
''' Demonstrates adding multiple test suites. Output to console.
'''
test_cases = [TestCase('FileCheck: DesktopNotificationCenter', '', .0451, '', '')]
ts = [TestSuite("FileChecks", test_cases)]
ts.append(TestSuite("ProcessChecks", [TestCase('ProcessCheck: ApplicationControl', '', 1.043, '', '')]))
# pretty printing is on by default but can be disabled using prettyprint=False
print(TestSuite.to_xml_string(ts))
def testMultiTestCasesToFile():
''' Demonstrates a single test suite with multiple test cases, one of which
has failure info. Output to a file with PrettyPrint disabled (Jenkins-friendly).
'''
test_cases = [TestCase('DesktopNotificationCenter', 'Integration.FileCheck', .0451, '', '')]
test_cases.append(TestCase('PropertyServer', 'Integration.FileCheck', .5678, '', ''))
test_cases[0].add_failure_info('Invalid File \'DNC.exe\'.')
ts = [TestSuite("GII_2013_R1", test_cases)]
# open the file, then call the TestSuite to_File function with prettyprint off.
# use raw text here to protect slashes from becoming escape characters
with open(r'C:\Users\Administrator\.jenkins\workspace\IntegrationTests\FileCheck.xml', mode='a') as lFile:
TestSuite.to_file(lFile, ts, prettyprint=False)
lFile.close()
if __name__ == '__main__':
''' If this module is being run directly, run all of the example test functions.
Test functions output JUnit XML for various scenarios to either screen (Console)
or file.
'''
testBasicToConsole()
# testBasicInfoToConsole()
# testFailureInfoToConsole()
# testMultiTestCasesToConsole()
# testMultiTestSuitesToConsole()
# testMultiTestCasesToFile()
else:
''' Function calls for an external run of this script.
'''
testMultiTestCasesToFile()
你可以使用junitxml
(Python的JUnit的XML记者)
是的PyPI: http://pypi.python.org/pypi/junitxml
如果你有一个标准的unittest
的测试套件称为suite
。 你可以运行它,结果写入这样的XML文件:
import junitxml
fp = file('results.xml', 'wb')
result = junitxml.JUnitXmlResult(fp)
result.startTestRun()
TestSuite(suite).run(result)
result.stopTestRun()
或者发现测试和打印XML到stdout:
python -m junitxml.main discover
另一种选择是使用nose
与运行套件:
nosetests --with-xunit
该collective.recipe.xmltestreport
扩建配方包正是这样做的。 这需要测试运行器输出和创建一个适合于JUnit的XML文件。 它是,但是, 扩建具体使用zope.testrunner
测试运行包 。
如果切换到扩建是不是你的选择,你可以研究它的源代码中提取的重要组成部分。
我发现一个Python模块https://bitbucket.org/db_atlass/python-junit-xml-output-module/ ,看上去适合我的需要。 THX大卫·布莱克那儿
# code snippet for the usage
""" a short example of how to use this module """
test_cases = []
for i in range(0, 5):
type_c = ""
if i % 2 == 0:
type_c = "failure"
test_cases.append(TestCase(i, str(i) + "contents", type_c) )
junit_xml = JunitXml("demo test example", test_cases)
在这里,我有另外一个包从GitHub https://github.com/kyrus/python-junit-xml
好的答案在这里:(有很多方法可以做到这一点) Python的单元测试的詹金斯?
恕我直言,最好的办法是写单元测试的Python测试和安装pytest(像“百胜安装pytest”)来获得py.test安装。 然后运行这样的测试: 'py.test --junitxml results.xml test.py'。 你可以运行任何单元测试的Python脚本,并得到JUnit的XML结果。
https://docs.python.org/2.7/library/unittest.html
在詹金斯生成配置生成后的动作与添加一个为result.xml“发布JUnit测试结果报告书”的行动和你产生任何更多的测试结果文件。