2.3显示名称

测试类和测试方法可以通过@DisplayName来声明自定义显示地名称,可以显示空格、特殊字符、甚至emojis。这些名称将通过测试运行器和IDE在测试报告显示。

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

@DisplayName("A special test case")
class DisplayNameDemo {

    @Test
    @DisplayName("Custom test name containing spaces")
    void testWithDisplayNameContainingSpaces() {
    }

    @Test
    @DisplayName("╯°□°)╯")
    void testWithDisplayNameContainingSpecialCharacters() {
    }

    @Test
    @DisplayName("😱")
    void testWithDisplayNameContainingEmoji() {
    }

}

2.3.1 名称显示生成器

JUnit Jupiter允许用户通过配置@DisplayNameGeneration注解来自定义名称显示生成器。@DisplayName提供名称的优先级高于@DisplayNameGenerator

import java.lang.reflect.Method;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

class DisplayNameGeneratorDemo {

    @Nested
    @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
    class A_year_is_not_supported {

        @Test
        void if_it_is_zero() {
        }

        @DisplayName("A negative value for year is not supported by the leap year computation.")
        @ParameterizedTest(name = "For example, year {0} is not supported.")
        @ValueSource(ints = { -1, -4 })
        void if_it_is_negative(int year) {
        }

    }

    @Nested
    @DisplayNameGeneration(IndicativeSentences.class)
    class A_year_is_a_leap_year {

        @Test
        void if_it_is_divisible_by_4_but_not_by_100() {
        }

        @ParameterizedTest(name = "Year {0} is a leap year.")
        @ValueSource(ints = { 2016, 2020, 2048 })
        void if_it_is_one_of_the_following_years(int year) {
        }

    }

    static class IndicativeSentences extends DisplayNameGenerator.ReplaceUnderscores {

        @Override
        public String generateDisplayNameForClass(Class<?> testClass) {
            return super.generateDisplayNameForClass(testClass);
        }

        @Override
        public String generateDisplayNameForNestedClass(Class<?> nestedClass) {
            return super.generateDisplayNameForNestedClass(nestedClass) + "...";
        }

        @Override
        public String generateDisplayNameForMethod(Class<?> testClass, Method testMethod) {
            String name = testClass.getSimpleName() + ' ' + testMethod.getName();
            return name.replace('_', ' ') + '.';
        }

    }

}

显示的结果是:

+-- DisplayNameGeneratorDemo [OK]
  +-- A year is not supported [OK]
  | +-- A negative value for year is not supported by the leap year computation. [OK]
  | | +-- For example, year -1 is not supported. [OK]
  | | '-- For example, year -4 is not supported. [OK]
  | '-- if it is zero() [OK]
  '-- A year is a leap year... [OK]
    +-- A year is a leap year if it is divisible by 4 but not by 100. [OK]
    '-- A year is a leap year if it is one of the following years. [OK]
      +-- Year 2016 is a leap year. [OK]
      +-- Year 2020 is a leap year. [OK]
      '-- Year 2048 is a leap year. [OK]

2.3.2设置默认的名称显示生成器

您可以使用junit.jupiter.displayname.generator.default配置参数来指定您希望在缺省情况下使用的DisplayNameGenerator的完全限定类名。与通过@DisplayNameGeneration注释配置的显示名称生成器一样,提供的类必须实现DisplayNameGenerator接口。默认的显示名称生成器将用于所有测试,除非@DisplayNameGeneration注释出现在一个封闭的测试类或测试接口上。通过@DisplayName注解所显示名称的优先级总高于通过DisplayNameGenerator显示的名称。

比如,默认使用ReplaceUnderscores名称显示生成器,您需要将配置参数设置为相应的完全限定类名。比如在src/test/resources/junit-platform.properties文件下:

junit.jupiter.displayname.generator.default = \
    org.junit.jupiter.api.DisplayNameGenerator$ReplaceUnderscores

类似地,可以指定实现DisplayNameGenerator的任何自定义类的完全限定名。

总的来说,按照以下优先规则来显示测试类和测试方法的名称:

  • @DisplayName提供的值;
  • @DisplayNameGeneration注解调用DisplayNameGenerator提供的名称;
  • 通过配置参数来调用默认的DisplayNameGenerator
  • 调用org.junit.jupiter.api.DisplayNameGenerator.Standard显示的名称;

results matching ""

    No results matching ""