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

2024-03-07

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

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

相关问题 https://stackoverflow.com/questions/4922867/junit-xml-format-specification-that-hudson-supports.


上面的 Corey 建议使用 junitxml,但我和 larrycai 的情况一样,因为我没有编写单元测试来测试 Python 代码。我正在编写 Python 脚本来进行黑盒系统测试,只想以 JUnit XML 输出结果,而无需重新发明轮子。

我简要地查看了上面 larrycai 建议的 David Black 的“python junit xml 输出模块”,但最终选择了另一个类似的包。不确定哪个更好,因为我只尝试过这个,但它最终对我来说效果很好。

仅一字符不同,但包是“junit-xml”:https://pypi.python.org/pypi/junit-xml/1.0 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:\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()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

用于从另一个测试结果生成 JUnit 报告的 Python 脚本 的相关文章

随机推荐

  • Python:用于从各种格式加载音频元数据的功能最丰富的库是什么?

    我正在寻找一个功能丰富的优秀库 用于从各种音频格式 MP3 FLAC OGG WAV 等 读取元数据 我已经看过 Mutagen 但文档几乎不存在 而且它似乎无法加载基本信息 例如艺术家和音频标题 艺术家和音频标题编码是否正确 它无法满足哪
  • AppCode 工作区和 TFS 命令行客户端工具

    我可以使用 AppCode 创建一个工作区 并成功签入和签出 TFS 这一切都很好 但是当我使用TFS 的跨平台命令行客户端 http msdn microsoft com en us library gg413282 28v vs 100
  • perl 如何将 perl 脚本的列表形式系统调用的 STDOUT 附加到文件

    My bosswrap pl将生成元素中包含空格的任意数组 它通过系统调用重复发送数组wrapped pl 它根据数组创建 STDOUT 随后 bosswrap pl必须附加 STDOUTwrapped pl到一个文件bosswrap pl
  • Android - 拍摄照片并通过 Intent 使用自定义名称将其保存到自定义目的地

    我有一个程序可以通过 Intent 打开相机来拍照 这么多部分已经可以正常工作了 但是 我希望它将它保存到具有特定文件名的特定文件夹中 文件名是可选的 但它确实很有帮助 这就是我到目前为止所拥有的 这是打开相机的代码行 TODO camer
  • Alfresco:定义新的控制参数

    我正在开发一个自定义表单控件 需要定义一个名为字符串类型的新控制参数helptext 我了解如何在我的 share config custom 中调用它 如何在我的自定义表单控件中使用它 但不知道如何最初声明它 我看到其他控制参数使用以下格
  • 如何在blazor web assembly中获取id_token

    我有一个带有 oidc 身份验证的 Blazor WebAssembly 最新 3 2 0 应用程序 asp net 身份验证提供了一种获取 accessToken 的方法 但看不到任何访问我的场景所需的 id token jwt 的方法
  • 生成带前导 0 的整数

    我有一个项目 我必须生成从 00000 到 99999 的随机数 随机化并不是让我陷入困境的地方 但事实上它总是需要 5 个字符 所以当它生成数字 14 时 我希望它是 00014 实现这一目标的最佳方法是什么 sprintf http d
  • 如何仅删除 a:before 中的下划线?

    我有一组样式链接 使用 before应用箭头 它在所有浏览器中看起来都不错 但是当我将下划线应用于链接时 我不想在链接上有下划线 before部分 箭头 请参阅 jsfiddle 例如 http jsfiddle net r42e5 1 h
  • Matlab 的 slice() 函数无法按预期工作

    我想在 13 处绘制离散的 2D 图像z地点位于 4 4 52 使用以下代码行 a 100 mesh x mesh y mesh z meshgrid 1 1 100 1 1 100 4 4 52 a unifdist 0 b unifdi
  • 将 DropDownListFor 助手绑定到字典

    我正在创建并填充字典 并希望通过使用将其绑定到下拉列表下拉列表的 http msdn microsoft com en us library ee703573 aspx辅助方法 我如何映射该词典的key and value到下拉菜单 看起来
  • 如何确定拨打的电话是本地电话还是 STD 或 ISD

    我正在开发一个应用程序 需要跟踪用户发出的拨出呼叫 我可以通过使用广播接收器来跟踪 我还想检测呼叫是本地呼叫还是 STD 或 ISD 但我无法检测到 假设 你知道哪个State 电信界 正在拨出电话 然后你只需要检查你的地方州 电信界 与去
  • Elastic Beanstalk .ebextensions 在 WAR 中被忽略

    我正在尝试改变client max body size我的 Elastic Beanstalk NGINX 反向代理的属性 以便允许上传更大的 JPEG 文件 因此 我将文件夹 ebextensions 添加到 WAR 文件的根目录中 WA
  • 如何在Android模拟器上模拟速度?

    有没有办法在Android模拟器上模拟速度 edit 这样做的目的是测试 getSpeed 方法 Example 我想将设备设置为每小时 20 英里 是的 你可以这么做 如果您使用 Eclipse 开发应用程序 则必须进入 DDMS 视角
  • Titan 警告:查询需要迭代所有顶点

    下面我补充一下cdate索引和一些数据 baseGraph makeKey cdate dataType Long class indexed Vertex class make for int i 0 i lt 20 i Vertex p
  • C++11 std::to_string(double) - 没有尾随零

    今天尝试了C 11 STL的一些新功能 遇到了std to string 可爱的 可爱的一组功能 创建一个 stringstream 对象来进行一次双精度到字符串的转换对我来说总是显得有些过分 所以我很高兴我们现在可以做这样的事情 std
  • 无法在 Google 云端硬盘文件夹中创建虚拟环境

    我正在使用 Google Drive 保留我的代码项目的副本 以防我的计算机死机 我也在使用 GitHub 但不在某些私人项目上 但是 当我尝试使用创建虚拟环境时virtualenv 我收到以下错误 PS C users fchatter
  • 如何保护 phpMyAdmin

    我注意到我的网站有一些奇怪的请求试图找到 phpmyadmin 例如 phpmyadmin pma etc 现在我已经通过 apt 在 Ubuntu 上安装了 PMA 并且想通过不同于 phpmyadmin 的网址访问它 我可以做什么来改变
  • Jenkins 中的条件构建后步骤(理想情况下没有插件)

    如果我理解正确的话 我可以使用这个插件来设置构建步骤的条件 https wiki jenkins ci org display JENKINS Conditional BuildStep Plugin https wiki jenkins
  • 如何计算到达某一天的剩余时间(天、小时、分钟、秒)?

    我在用Moment js https momentjs com docs displaying fromnow 检索下周五 moment day 5 format 成功运行并打印2020 06 26T13 19 01 04 00到控制台 现
  • 用于从另一个测试结果生成 JUnit 报告的 Python 脚本

    我有一个验收测试用例 结果是纯文本 我想使用Jenkins来显示结果 JUnit格式适合我 所以我想检查是否有现有的Python代码来生成JUnit格式的XML 这样我就可以轻松地添加我的解析代码 相关问题 https stackoverf