Cucumber Rerun Formatter

API automation cases,有时会因为环境的问题 fail 了一些 case,Triage 的时候把 fail 的 case 再本地重跑验证一下。怎样才能快捷方便的重跑呢?

Cucumber Rerun Formatter

Cucumber 提供 Rerun formatter,可以将跑 fail 的 cases 记录到一个文本中,然后直接运行这介文本就能重跑所有 fail 的 cases 了。

第一次运行,将 fail 的 cases 记录到 target/rerun-first.txt中,执行命令中options设置--format rerun:rerun txt path

mvn clean test -Dprofile=profile -Dcucumber.options="--tags '(tag filter expression)' --format rerun:target/rerun-first.txt"

target 目录下会生成 rerun-first.txt 的文本文件,内容类似下面这种,数字代码cases 对应的行号:
src/test/resources/features/…/feature1.feature:130:132:134
src/test/resources/features/…/feature.feature:82:83

重跑 rerun-first.txt 中 fail 的 cases,options 设置 @rerun txt path, 还可以继续记录 fail 的 cases 到文本 rerun-second.txt
注意:执行命令不要加 clean,不然 target 目录清掉了,前面保存的 rerun-first.txt 就清掉了。

mvn test -Dprofile=profile -Dcucumber.options="@target/rerun-first.txt --format rerun:target/rerun-second.txt"

Parallel run 的 Rerun

前面有篇文章介绍了有关 Cucumber Parallel Run,大大提高了cases 的运行时间,但是后面麻烦也来了,在 cucumber-jvm-parallel-plugin 中设置了 rerun plugin, 因为是按 feature 并行跑的,所以每个 feature 都产生了一个rerun 文本文件, 有多少个 feature,就生成多少个 rerun 文本文件,只是全 pass 的 feature 的 rerun 文本是空的而已,要是有一个汇总的 rerun文本就好了。

<plugins>
	<plugin>
		<name>json</name>
		<extension>json</extension>
		<outputDirectory>${project.build.directory}/cucumber-parallel/json
		</outputDirectory>
	</plugin>
	<plugin>
		<name>html</name>
		<extension>html</extension>
		<outputDirectory>${project.build.directory}/cucumber-parallel/html
		</outputDirectory>
	</plugin>
	<plugin>
		<name>rerun</name>
		<extension>txt</extension>
		<outputDirectory>${project.build.directory}/rerun</outputDirectory>
	</plugin>
</plugins>

实在忍受不了人力一个个 case 重跑,或则一个个 rerun 的文本重跑,还是自己merge 一个总的 rerun 文本吧,哈哈。说干就干,还是用 Python 比较方便,在本地把脚本写好,测试没有问题。

import os
from os.path import abspath,getsize,curdir,join

def mergeRerunFiles(dirName):
    dir = abspath(join(curdir, dirName, "rerun"))
    file_list = os.listdir(dir)
    newFile = dir + '/rerun.txt'
    print(len(file_list))
    lines = []
    for f in file_list:
            file_path = abspath(join(dir,f))
            if getsize(file_path) > 0:
                with open(file_path,'r',encoding='utf-8') as fr:
                    for line in fr.readlines():
                        lines.append(line[line.find('src'):])
    with open(newFile, 'w') as fw:
        fw.writelines(lines)

mergeRerunFiles("target")

再移步到 Jenkins 上,遇到 2 个问题
在这里插入图片描述

当 cases 执行完再执行这段 python 脚本。
在这里插入图片描述

  1. 执行 Shell 脚本跑 case,如果 case 都 PASS,会执行 python 脚本,但 case有 Failed,Python 脚本根本不会执行,
    解决方法:
    Execute shell 点击Advance…,在展开的 Exit code to set build unstable 中输入 1,这时如果该 shell 执行失败了,jenkins 的执行结果将不是 failure,而是unstable,执行后续的流程。
    在这里插入图片描述
    下面这段是对 Exit code to set build unstable 设置的解释:

If set, the shell exit code that will be interpreted as an unstable build result. If the exit code matches the value, the build results will be set to ‘unstable’ and next steps will be continued. On Unix-like it is a value between 0-255. The value 0 is ignored and does not make the build unstable to keep the default behaviour consistent.

  1. 执行 Python 脚本有遇到异常
    with open(file_path,‘r’,encoding=‘utf-8’) as fr:
    TypeError: 'encoding' is an invalid keyword argument for this function
    解决方案:
    是因为 Jenkins 上的 Python 解释器版本比较老,不兼容,导入 io 便可解决。
import io
with io.open(file_path,'r',encoding='utf-8') as fr:

以上就是我在工作中遇到的问题,花时间研究并解决了,一劳永逸,提高 了工作效率,很开心,哈哈!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值