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:

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

猜你喜欢

转载自blog.csdn.net/wumingxiaoyao/article/details/110718064