JUnit5用户手册~参数化测试

@ParameterizedTest
@ValueSource(strings = { "racecar", "radar", "able was I ere I saw elba" })
void palindromes(String candidate) {
  assertTrue(StringUtils.isPalindrome(candidate));
}

参数来源

@ValueSource

short
byte
int
long
float
double
char
boolean
java.lang.String
java.lang.Class

@ParameterizedTest
@ValueSource(ints = { 1, 2, 3 })
void testWithValueSource(int argument) {
  assertTrue(argument > 0 && argument < 4);
}

Null和空

@NullSource:null,不能用于原始类型
@EmptySource:java.lang.String, java.util.List, java.util.Set,java.util.Map,原始类型数组(int[], char[][]),对象数组(String[],Integer[][])
@NullAndEmptySource:  @NullSource和@EmptySource

@ParameterizedTest
@NullSource
@EmptySource
@ValueSource(strings = { " ", " ", "\t", "\n" })
void nullEmptyAndBlankStrings(String text) {
  assertTrue(text == null || text.trim().isEmpty());
}

或者

@ParameterizedTest
@NullAndEmptySource
@ValueSource(strings = { " ", " ", "\t", "\n" })
void nullEmptyAndBlankStrings(String text) {
  assertTrue(text == null || text.trim().isEmpty());
}
@EnumSource
@ParameterizedTest
@EnumSource(ChronoUnit.class)
void testWithEnumSource(TemporalUnit unit) {
  assertNotNull(unit);
}

默认值

@ParameterizedTest
@EnumSource
void testWithEnumSourceWithAutoDetection(ChronoUnit unit) {
  assertNotNull(unit);
}

指定

@ParameterizedTest
@EnumSource(names = { "DAYS", "HOURS" })
void testWithEnumSourceInclude(ChronoUnit unit) {
  assertTrue(EnumSet.of(ChronoUnit.DAYS, ChronoUnit.HOURS).contains(unit));
}

排除

@ParameterizedTest
@EnumSource(mode = EXCLUDE, names = { "ERAS", "FOREVER" })
void testWithEnumSourceExclude(ChronoUnit unit) {
  assertFalse(EnumSet.of(ChronoUnit.ERAS, ChronoUnit.FOREVER).contains(unit));
}

匹配

@ParameterizedTest
@EnumSource(mode = MATCH_ALL, names = "^.*DAYS$")
void testWithEnumSourceRegex(ChronoUnit unit) {
  assertTrue(unit.name().endsWith("DAYS"));
}

@MethodSource

@ParameterizedTest
@MethodSource("stringProvider")
void testWithExplicitLocalMethodSource(String argument) {
  assertNotNull(argument);
}
static Stream stringProvider() {
  return Stream.of("apple", "banana");
}
@ParameterizedTest
@MethodSource("range")
void testWithRangeMethodSource(int argument) {
  assertNotEquals(9, argument);
}
static IntStream range() {
  return IntStream.range(0, 20).skip(10);
}

多参数

@ParameterizedTest
@MethodSource("stringIntAndListProvider")
void testWithMultiArgMethodSource(String str, int num, List list) {
  assertEquals(5, str.length());
  assertTrue(num >=1 && num <=2);
  assertEquals(2, list.size());
}
static Stream stringIntAndListProvider() {
  return Stream.of(
  arguments("apple", 1, Arrays.asList("a", "b")),
  arguments("lemon", 2, Arrays.asList("x", "y"))
  );
}
@CsvSource
@ParameterizedTest
@CsvSource({
  "apple, 1",
  "banana, 2",
  "'lemon, lime', 0xF1",
  "strawberry, 700_000"
})
void testWithCsvSource(String fruit, int rank) {
  assertNotNull(fruit);
  assertNotEquals(0, rank);
}
Example Input
Resulting Argument List
@CsvSource({ "apple, banana" })
"apple" , "banana"
@CsvSource({ "apple, 'lemon, lime'" })
"apple" , "lemon, lime"
@CsvSource({ "apple, ''" })
"apple" , ""
@CsvSource({ "apple, " })
"apple" , null
@CsvSource(value = { "apple, banana, NIL" },
nullValues = "NIL")
"apple" , "banana" , null
@CsvSource(value = { " apple , banana" },
ignoreLeadingAndTrailingWhitespace = false)
" apple " , " banana"

文本块

@ParameterizedTest(name = "[{index}] {arguments}")
@CsvSource(useHeadersInDisplayName = true, textBlock = """
  FRUIT, RANK
  apple, 1
  banana, 2
  'lemon, lime', 0xF1
  strawberry, 700_000
  """)
void testWithCsvSource(String fruit, int rank) {
  // ...
}

显示

[1] FRUIT = apple, RANK = 1
[2] FRUIT = banana, RANK = 2
[3] FRUIT = lemon, lime, RANK = 0xF1
[4] FRUIT = strawberry, RANK = 700_000
@CsvFileSource
@ParameterizedTest
@CsvFileSource(resources = "/two-column.csv", numLinesToSkip = 1)
void testWithCsvFileSourceFromClasspath(String country, int reference) {
  assertNotNull(country);
  assertNotEquals(0, reference);
}

@ParameterizedTest
@CsvFileSource(files = "src/test/resources/two-column.csv", numLinesToSkip = 1)
void testWithCsvFileSourceFromFile(String country, int reference) {
  assertNotNull(country);
  assertNotEquals(0, reference);
}

@ParameterizedTest(name = "[{index}] {arguments}")
@CsvFileSource(resources = "/two-column.csv", useHeadersInDisplayName = true)
void testWithCsvFileSourceAndHeaders(String country, int reference) {
  assertNotNull(country);
  assertNotEquals(0, reference);
}
two-column.csv
COUNTRY, REFERENCE
Sweden, 1
Poland, 2
"United States of America", 3
France, 700_000
@ArgumentsSource
@ParameterizedTest
@ArgumentsSource(MyArgumentsProvider.class)
void testWithArgumentsSource(String argument) {
  assertNotNull(argument);
}


public class MyArgumentsProvider implements ArgumentsProvider {

  @Override
  public Stream provideArguments(ExtensionContext context) {
      return Stream.of("apple", "banana").map(Arguments::of);
  }
}

4、参数转换

你可能感兴趣的:(单元测试,Java,java,服务器,javascript)