JUnit 5显示名称

本文介绍了如何在JUnit 5中为测试类和方法设置自定义显示名称,包括使用@DisplayName注解、创建显示名称生成器以及在参数化测试中声明自定义名称。还提供了源代码下载链接供读者实践。
摘要由CSDN通过智能技术生成

在JUnit 5中,我们可以使用@DisplayName声明测试类和测试方法的自定义显示名称。

PS已通过JUnit 5.5.2测试

1. @DisplayName

1.1测试类和方法的默认名称。

DisplayNameTest.java
package com.mkyong.display;

import org.junit.jupiter.api.Test;

public class DisplayNameTest {

    @Test
    void test_spaces_ok() {
    }

    @Test
    void test_spaces_fail() {
    }

}

输出量

输出
+-- JUnit Jupiter [OK]
| '-- DisplayNameTest [OK]
|   +-- test_spaces_ok() [OK]
|   '-- test_spaces_fail() [OK]

1.2 @DisplayName

DisplayNameCustomTest.java
package com.mkyong.display;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

@DisplayName("I'm a Test Class")
public class DisplayNameCustomTest {

    @Test
    @DisplayName("Test with spaces, expected ok")
    void test_spaces_ok() {
    }

    @DisplayName("Test with spaces, expected failed")
    @Test
    void test_spaces_fail() {
    }

}

输出量

输出
+-- JUnit Jupiter [OK]
| '-- I'm a Test Class [OK]
|   +-- Test with spaces, expected ok [OK]
|   '-- Test with spaces, expected failed [OK]

2.显示名称生成器

2.1我们还可以创建一个自定义显示名称生成器,并通过@DisplayNameGeneration配置。

2.2此示例使用JUnit ReplaceUnderscores生成器将下划线替换为空格。

DisplayNameGenerator1Test.java
package com.mkyong.display;

import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Method;

@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
public class DisplayNameGenerator1Test {

    @Test
    void test_spaces_ok() {
    }

    @Test
    void test_spaces_fail() {
    }

}

输出量

输出

2.3我们可以扩展JUnit DisplayNameGenerator以创建我们的自定义显示名称生成器。

DisplayNameGenerator2Test.java
package com.mkyong.display;

import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.stream.Collectors;

@DisplayNameGeneration(DisplayNameGenerator2Test.CustomDisplayNameGenerator.class)
public class DisplayNameGenerator2Test {

    @Test
    void test_spaces_ok() {
    }

    @Test
    void test_spaces_fail() {
    }

    static class CustomDisplayNameGenerator extends DisplayNameGenerator.Standard {

        @Override
        public String generateDisplayNameForClass(Class<?> testClass) {
            return "New Name for test class";
        }

        @Override
        public String generateDisplayNameForNestedClass(Class<?> nestedClass) {
            return super.generateDisplayNameForNestedClass(nestedClass);
        }
        
        @Override
        public String generateDisplayNameForMethod(Class<?> testClass, Method testMethod) {
            String name = testMethod.getName();
            return Arrays.stream(name.split("_")).collect(Collectors.joining(" | "));
        }
    }

}

输出量

输出

3.参数化测试

3.1对于参数化测试,我们可以通过@ParameterizedTest的name属性声明自定义显示名称,请参见以下示例:

DisplayNameParamTest.java
package com.mkyong.display;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.EnumSource;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

import static org.junit.jupiter.params.provider.Arguments.arguments;

public class DisplayNameParamTest {

    @ParameterizedTest(name = "#{index} - Test with TimeUnit: {0}")
    @EnumSource(value = TimeUnit.class, names = {"MINUTES", "SECONDS"})
    void test_timeunit_ok(TimeUnit time) {
    }


    @ParameterizedTest(name = "#{index} - Test with {0} and {1}")
    @MethodSource("argumentProvider")
    void test_method_multi(String str, int length) {
    }

    static Stream<Arguments> argumentProvider() {
        return Stream.of(
                arguments("abc", 3),
                arguments("lemon", 2)
        );
    }

}

输出量

输出

下载源代码

$ git clone https://github.com/mkyong/junit-examples
$ cd junit5-examples
$检查src / test / java / com / mkyong / display / *。java

参考文献

翻译自: https://mkyong.com/junit5/junit-5-display-names/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值