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

Lar*_*Cai 16 python junit jenkins

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

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

相关问题.

tbr*_*adt 17

上面的Corey建议使用junitxml,但我和larrycai在同一条船上,因为我没有编写单元测试来测试Python代码.我正在编写Python脚本来进行黑盒系统测试,只是想在不重新发明轮子的情况下输出JUnit XML中的结果.

我简单地看了一下上面larrycai建议的David Black的"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()
Run Code Online (Sandbox Code Playgroud)


Cor*_*erg 7

你可以使用junitxml(Python JUnit XML reporter)

关于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()
Run Code Online (Sandbox Code Playgroud)

或者发现测试并将xml打印到stdout:

python -m junitxml.main discover
Run Code Online (Sandbox Code Playgroud)

另一个选择是使用nose和运行您的套件:

nosetests --with-xunit
Run Code Online (Sandbox Code Playgroud)


Lar*_*Cai 1

我发现一个 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)
Run Code Online (Sandbox Code Playgroud)