从另一个测试结果生成JUnit报告的Python脚本



我有一个验收测试用例,结果是纯文本。我想用Jenkins来显示结果,JUnit格式适合我。

因此,我想检查是否有现有的python代码来生成JUnit格式的XML,这样我就可以轻松地添加我的解析代码。

相关问题。

Corey建议使用junitxml,但我和larrycai的处境相同,因为我没有编写单元测试来测试Python代码。我正在编写Python脚本来进行黑盒系统测试,只想在不重新设计轮子的情况下用JUnitXML输出结果。

我简单地看了一下上面larrycai建议的David Black的"python-junit xml输出模块",但最终得到了另一个类似的包。不确定哪一个更好,因为我只试过这个,但它最终对我来说效果很好。

只有一个字符不同,但包是"junit xml":https://pypi.python.org/pypi/junit-xml/1.0

小心。。。他的自述中的例子有错误,不起作用。我报告了github上的错误(pypi页面上包含github链接)。他的"prettyprint"arg处理也有一个错误,但我将向您介绍我在github上报告的问题#3,其中包含了我的修复。如果你下载了源代码,你可以查看他的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:UsersAdministrator.jenkinsworkspaceIntegrationTestsFileCheck.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

如果您有一个名为suite的标准unittest测试套件。您可以运行它,并将结果写入这样的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/,看起来很适合我的需要。大卫·布莱克在那里

# 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单元测试?

IMHO最好的方法是编写python-unittest测试安装pytest(类似于"yum-install-pytest")以安装py.test。然后运行这样的测试:'py.test--junitxml results.xml test.py'。您可以运行任何unittest python脚本并获得jUnit.xml结果。

https://docs.python.org/2.7/library/unittest.html

在jenkins构建配置中,构建后操作添加一个带有result.xml的"发布JUnit测试结果报告"操作以及您生成的任何其他测试结果文件。

最新更新