软件测试学习笔记丨 Allure2 报告中添加用例标题
作者:测试人
- 2024-07-08 北京
本文字数:1995 字
阅读完需:约 7 分钟
本文转自测试人社区,原文链接:https://ceshiren.com/t/topic/28350
Allure 用法
Allure2 报告中添加用例标题
应用场景:为了让生成的测试报告便于阅读,可以为每条用例添加一个便于阅读的标题(可以使用中文标题)。生成的报告展示用例时,就会以设置的标题名展示出来。
Allure2 报告中添加用例标题
通过使用 @DisplayName 注解可以为测试用例自定义一个可阅读性的标题。
@DisplayName 注解 的三种使用方式:
直接设置标题。
设置参数化用例标题。
动态设置测试用例标题。
Allure2 报告直接设置标题
方法一:直接设置标题。
@DisplayName("验证 加法 计算")public class DisplayNameTest { @Test @DisplayName("验证 8+9 计算") public void testAdd() { int result = 8 + 9; assertEquals(17,result,"8 + 9的计算结果失败"); }}复制代码
Allure2 报告参数化设置用例标题
方式二:参数化设置用例标题
@ParameterizedTest @CsvSource({"foo, 3", "hello, 5", "world, 5"}) @DisplayName("测试字符串长度 - 参数化") void testStr(String str, int expected) { assertEquals(expected, str.length()); }
@DisplayName("验证减法计算:") @ParameterizedTest(name = "{0} - {1} = {2}") @MethodSource public void testSub(int a, int b, int re) { int result = a - b; assertEquals(re,result,()->a + " - " + b + "的计算结果失败"); } public static Stream<Arguments> testSub(){ return Stream.of( Arguments.arguments(2,2,0), Arguments.arguments(4,5,-1), Arguments.arguments(8,3,5) ); }复制代码
Allure2 报告动态设置测试用例标题
@DisplayName("动态更新测试用例标题")public class DynamicAddTitleTest { @TestFactory List<DynamicTest> dynamicStrTest() { List<String> inputList = Arrays.asList("apple", "banan", "orang");
return inputList.stream() .map(str -> DynamicTest.dynamicTest("动态测试字符串长度:" + str, () -> { // 在此处可以使用TestInfo来动态更新测试用例标题 TestInfo testInfo = new TestInfo() { @Override public String getDisplayName() { return "动态测试字符串长度:" + str; }
@Override public Set<String> getTags() { return null; }
@Override public Optional<Class<?>> getTestClass() { return Optional.empty(); }
@Override public Optional<Method> getTestMethod() { return Optional.empty(); } }; assertEquals(5, str.length(), "Length of " + str); })) .toList(); }}复制代码
动态修改测试用例标题
修改前
package com.junit5;
import io.qameta.allure.Allure;import org.junit.jupiter.api.DisplayName;import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
@DisplayName("首个报告测试用例")public class FirstTest {
@Test @DisplayName("第一个测试用例") public void test1(){ //加法 int result = 3+2; //登录 //输入账号 //输入密码 //点击登录按钮
// Allure.getLifecycle().updateTestCase(testResult -> testResult.setName("用户登录测试")); assertEquals(5,result,"计算错误"); }}复制代码
修改后
package com.junit5;
import io.qameta.allure.Allure;import org.junit.jupiter.api.DisplayName;import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
@DisplayName("首个报告测试用例")public class FirstTest {
@Test @DisplayName("第一个测试用例") public void test1(){ //加法 int result = 3+2; //登录 //输入账号 //输入密码 //点击登录按钮
Allure.getLifecycle().updateTestCase(testResult -> testResult.setName("用户登录测试")); assertEquals(5,result,"计算错误"); }}复制代码
软件测试开发免费视频教程分享
划线
评论
复制
发布于: 刚刚阅读数: 3
版权声明: 本文为 InfoQ 作者【测试人】的原创文章。
原文链接:【http://xie.infoq.cn/article/9e6a7ee3ce5b239edd5304bec】。文章转载请联系作者。
测试人
关注
专注于软件测试开发 2022-08-29 加入
霍格沃兹测试开发学社,测试人社区:https://ceshiren.com/t/topic/22284







评论