Автор оригинала: mkyong.
В JUnit 5 мы можем использовать @DisplayName
для объявления пользовательских отображаемых имен для тестовых классов и методов тестирования.
P.S Протестировано с JUnit 5.5.2
1. @оТображаЕмое имя
1.1 Имя классов и методов тестирования по умолчанию.
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 @оТображаЕмое имя
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 Заменить подчеркивания
для замены подчеркиваний пробелами.
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 Генератор отображаемых имен
, чтобы создать наш собственный генератор отображаемых имен.
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 Для параметризованных тестов мы можем объявить пользовательское отображаемое имя с помощью атрибута name @parameterizedtest
, см. Следующий пример:
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 StreamargumentProvider() { return Stream.of( arguments("abc", 3), arguments("lemon", 2) ); } }
Выход
Скачать Исходный Код
- Отображаемые имена JUnit 5
- JUnit 5 Параметризованные тесты
Оригинал: “https://mkyong.com/junit5/junit-5-display-names/”