22

macaca-reporter-java-plugin (基于 java-junit5 的 macaca 测试报告) · TesterHome

 4 years ago
source link: https://testerhome.com/topics/22986?
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Macaca macaca-reporter-java-plugin (基于 java-junit5 的 macaca 测试报告)

shixing · 2020年04月06日

· 1010 次阅读

macaca-reporter-java-plugin (基于java-junit5的macaca测试报告)

目前mcaca-reporter对java的测试代码没有很好的支持,公司目前的自动化一直使用macaca配合java和junit5进行,在这基础上,
为支持开源,对macaca-reporter进行了一些研究,试图在java的基础上开发出一个适用于java的macaca测试报告。

目前我们产出了第一个版本,也就是当前的macaca-reporter-java-plugin。报告效果如下:

(图1,据说适当打码有益身心健康)

78356a91-1143-47c9-a0d9-53d453b0d7dd.png!large

(图2)
0910a10e-29c7-454a-9aa1-ed61da8dcd83.png!large

(图3)
d32ff463-c01b-45c9-a8d1-f9a4ea1d5ca3.png!large

如何使用macaca-reporter-java-plugin

因为我们目前是基于junit5进行开发,所以第一步你可能要在你的测试代码中引入junit5

一、 pom.xml 引入 junit5

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.6.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.6.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.6.0</version>
<scope>test</scope>
</dependency>

二、引入macaca-reporter-java-plugin

<dependency>
<groupId>com.macacajs</groupId>
<artifactId>macaca-reporter-java-plugin</artifactId>
<version>1.0</version>
</dependency>

如果你已经使用了junit5完成了你的测试代码,不妨直接运行尝试一下结果。

如果你需要使用junit5开始改造你的代码,你可能需要了解下junit5。

junit5官方文档

macaca-reporter-java-plugin实现原理源于官方文档的6.1.5章节

三、开始改造你的代码

监听用例状态并给错误截图

建议使用junit5的@DisplayName特性,你可以通过给用例添加该注解配置用例对应的标题,在用例失败的时候使用getDisplayName方法去命名错误图片。

用例如果失败,macaca-reporter-java-plugin将通过当前工程的目录./screenshot下去获取图片名,并通过DisplayName去比对,找到对应的图片转换为Base64图片码保存到报告中。

junit5监听用例状态,并截图参考方法:

@ExtendWith(BaseTest.class)
public class BaseTest implements TestWatcher {
@BeforeAll
static void beforeAll() { ... }
@BeforeEach
void setUp() { ... }
@Test
@DisplayName("失败的用例")
void name() { ... }
@AfterEach
void tearDown() { ... }
@AfterAll
static void afterAll() { ... }
@Override
public void testFailed(ExtensionContext context, Throwable cause) {
//截图方法。
saveScreen(context.getDisplayName()+"_"+new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss").format(new Date()));
}
}

四、执行测试

因为macaca-reporter-java-plugin是以maven插件的形式开发,建议通过idea增加Maven执行配置去便捷执行。

指定计划名

mvn执行配置增加 (默认名:Macaca测试报告)

-DplanName=测试计划报告

指定Current Screen图

mvn执行配置增加 (不需要可忽略)

-Dlogo=http://www.logo.png

mvn执行配置参考

(添加入口)

b2d89334-5478-4166-861f-9b371b8bcbbc.png!large

(参考)
e0eaa12e-9042-4044-b490-5a060087cfc8.png!large

测试报告生成位置

测试执行完毕,你会在当前工程目录中发现多出一个report.js文件,和一个/reports目录。

report.js查看你会发现这里边储存的就是macaca-reporter所需要的json数据, 你也可以尝试修改report.js中json的数据,然后在当前目录执行 macaca-reporter -d report.js命令然后检查测试报告的变化。

/reports目录下自动生成的 index.html ,就是我们的测试报告了,你可以使用浏览器直接打开查看。

五、运行结果(根据上图配置)

794a4a4e-4a46-43ea-8ac3-427b97537681.png!large

macaca-reporter-java-plugin

问题&建议

如果你在使用过程中出现了什么问题,或者有好的建议可以在仓库中以issue的方式反馈给我们,
或者在Macaca开源社区钉钉群中可以联系到我们 钉钉群号(11775486)@鸟帅 @石 星


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK