首页 > 解决方案 > 詹金斯管道如何根据测试结果的百分比更改阶段结果

问题描述

jenkins pipeline junit 使用一个奇怪的指标来衡量工作的健康状况,
https: //www.jenkins.io/doc/pipeline/steps/junit/如果一个测试失败将标记阶段/工作不稳定

如果 20% 的测试失败,是否可以告诉阶段,然后将其标记为不稳定,否则成功?

标签: jenkinsjenkins-pipelinepipeline

解决方案


我创建了一个读取结果 xml 的 python 脚本,并获得了失败的测试和总数,并在 python 脚本中而不是从 cli 中运行 pytest,这样我就可以控制错误代码。因为如果某些测试失败,junit插件会将它标记为不稳定,因为错误代码不是零,所以我的脚本

pytest_result = pytest.main([my tests, "--junitxml", f"{args.reports_folder}/xml/*name*.xml", "-o", "junit_suite_name=*name*"] )

使用 import xml.etree.ElementTree as ET 解析 xml 以获得失败的数量和测试的总量

percentage_fail = (failed_tests/total_tests)*100

在我将其与阈值进行比较之后

if percentage_fail > threshold:
    raise SystemExit(1)
else:
    print("All test working")
    raise SystemExit(0)

如果变为“不稳定”,junit 将不允许您更改构建的状态,诀窍是控制运行测试产生的错误代码。在将 report.xml 传递给junit之前


推荐阅读