2023-09-26
测试开发
00
请注意,本文编写于 276 天前,最后修改于 276 天前,其中某些信息可能已经过时。

目录

处理 Jenkins 中的测试预期失败与构建状态的设置
问题引入:预期失败的测试和构建状态
解决方案一:Groovy 脚本定制构建后操作
解决方案二:使用插件自动检测并设置构建状态
总结:更精准的构建状态反映项目质量

处理 Jenkins 中的测试预期失败与构建状态的设置

在自动化测试的过程中,持续集成是一个至关重要的环节,可以帮助团队更高效地进行代码集成和测试。Jenkins作为一个流行的持续集成工具,提供了丰富的功能来支持构建、测试和部署。本文将讨论如何在 Jenkins 中处理测试中的预期失败情况,并将其与构建状态相结合,以便更好地监控和管理项目的健康状况。

问题引入:预期失败的测试和构建状态

在自动化测试中,有时我们会遇到预期失败的测试情况。

这可能是由于功能尚未实现、缺少依赖、环境问题等引起的。在测试框架中,我们可以使用 xfail 标记来标记预期失败的测试用例。

然而,如果仅仅将测试标记为预期失败,它在 Jenkins 中将不会影响构建的状态,可能会导致错误的构建结果。 我们希望能够根据预期失败的测试情况,适当地调整构建状态,以便更准确地反映项目的质量。

测试脚本中的 xfail 使用:在执行用例前,先检查当前设备是否联网,没有网络就失败不再继续往下执行

python
gen_report( results=[ { "result": [ "warning_report", "Warning: The current device network is abnormal, please check it and run it again!", ] } ], report_path=warning_report, ) # todo: xfail is skip ,fail is fail. Lack of multi-device differentiation. pytest.xfail( f"The current device {idx} network is abnormal, please check it and run it again!" )

解决方案一:Groovy 脚本定制构建后操作

这种方法需要一定的脚本编写和配置,但提供了高度的灵活性和定制性。

一种解决方案是使用 Jenkins 的 "Groovy Postbuild" 步骤来自定义构建后操作。我们可以编写 Groovy 脚本来分析测试日志,检查是否存在预期失败的标记,并根据情况将构建状态设置为 "UNSTABLE" 或其他适当的状态。

在 "Groovy Postbuild" 步骤中,编写 Groovy 脚本来解析测试日志并检查是否存在 "XFAIL" 标记。如果存在,则设置构建状态为 "失败"。

以下是一个示例的 Groovy 脚本:

groovy
def logContainsXFail = manager.getLogMatcher(".+XFAIL.+") // Use regex to match "XFAIL" in log if (logContainsXFail) { manager.buildFailure() }

执行Jenkins job之后报错,如下

没有权限,需要审批该Groovy 脚本

bash
[PostBuildScript] - [INFO] Executing post build scripts. [PostBuildScript] - [INFO] An error occured during post-build processing. org.jenkinsci.plugins.scriptsecurity.scripts.UnapprovedUsageException: script not yet approved for use at org.jenkinsci.plugins.scriptsecurity.scripts.ScriptApproval.using(ScriptApproval.java:474) at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript.evaluate(SecureGroovyScript.java:377) at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript.evaluate(SecureGroovyScript.java:314) at org.jenkinsci.plugins.postbuildscript.service.GroovyScriptExecutor.execute(GroovyScriptExecutor.java:49) at org.jenkinsci.plugins.postbuildscript.service.GroovyScriptPreparer.evaluateScript(GroovyScriptPreparer.java:53) at org.jenkinsci.plugins.postbuildscript.service.GroovyScriptPreparer.evaluateScript(GroovyScriptPreparer.java:38) at org.jenkinsci.plugins.postbuildscript.processor.Processor.processGroovyScripts(Processor.java:152) at org.jenkinsci.plugins.postbuildscript.processor.Processor.processScripts(Processor.java:90) at org.jenkinsci.plugins.postbuildscript.processor.Processor.process(Processor.java:79) at org.jenkinsci.plugins.postbuildscript.processor.Processor.process(Processor.java:73) at org.jenkinsci.plugins.postbuildscript.PostBuildScript.perform(PostBuildScript.java:116) at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:21) at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:808) at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:757) at hudson.model.Build$BuildExecution.post2(Build.java:179) at hudson.model.AbstractBuild$AbstractBuildExecution.post(AbstractBuild.java:701) at hudson.model.Run.execute(Run.java:1914) at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43) at hudson.model.ResourceController.execute(ResourceController.java:100) at hudson.model.Executor.run(Executor.java:433) Build step 'Execute scripts' changed build result to UNSTABLE Notifying upstream projects of job completion No emails were triggered. Finished: UNSTABLE

这需要管理员的脚本批准。

image.png

解决方案二:使用插件自动检测并设置构建状态

另一种更简化的方法是利用 Jenkins 插件来自动检测并设置构建状态。例如,可以使用 "Text-finder" 插件或 "Log Parser" 插件来扫描测试日志,查找预期失败的标记(如 "XFAIL"),并根据结果将构建状态设置为 "UNSTABLE"。这种方法不需要编写复杂的脚本,只需要简单地配置插件并设置适当的规则即可。

  • 使用 "Text-finder" 插件:
  1. 在 Jenkins 作业配置页面中,找到 "构建后操作" 部分。
  2. 添加一个 "Text Finder" 步骤。
  3. 在 "Find text" 字段中输入 "XFAIL",并选择 "Mark build as unstable" 选项。

这将在构建后操作中检查测试日志中是否包含 "XFAIL" 标记。如果存在,则将构建状态设置为 "UNSTABLE"。

image.png

  • 使用 "Log Parser" 插件:
  1. 安装并配置 Jenkins "Log Parser" 插件。
  2. 在 Jenkins 作业配置页面中,找到 "构建后操作" 部分。
  3. 添加一个 "Log Parser" 步骤。
  4. 在 "Parsing Rules" 部分,设置规则以匹配 "XFAIL" 或其他您期望的标记,并将构建状态设置为 "UNSTABLE"。

总结:更精准的构建状态反映项目质量

通过解决预期失败测试与构建状态之间的关系,我们可以更精确地反映项目的质量状况。

无论是使用定制的 Groovy 脚本还是插件,都可以根据项目的需求来选择适合的方法。持续集成的核心目标是尽早发现和解决问题,以确保软件交付的可靠性和稳定性。通过将预期失败的测试情况与构建状态相结合,团队可以更有效地跟踪和管理项目,保障项目的成功交付。

如果对你有用的话,可以打赏哦
打赏
ali pay
wechat pay

本文作者:李佳玮

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!