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

2024-06-08 09:06:32 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

Related question


Tags: 代码文本格式测试用例xmljunitjenkinsquestion
1条回答
网友
1楼 · 发布于 2024-06-08 09:06:32

上面提到的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上报告了错误(github链接包含在pypi页面中)。他的“prettyprint”arg处理也有一个bug,但我将向您介绍我在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:\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()

相关问题 更多 >