当前位置: 首页 > article >正文

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<String> 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<String> list) {
  assertEquals(5, str.length());
  assertTrue(num >=1 && num <=2);
  assertEquals(2, list.size());
}
static Stream<Arguments> 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<? extends Arguments> provideArguments(ExtensionContext context) {
      return Stream.of("apple", "banana").map(Arguments::of);
  }
}

4、参数转换


http://www.kler.cn/a/5071.html

相关文章:

  • 2025-01-06日SSH钓鱼日志
  • 跟着逻辑先生学习FPGA-第六课 无源蜂鸣器发声实验
  • 深度学习笔记11-优化器对比实验(Tensorflow)
  • 【设计模式】介绍常见的设计模式
  • Unity3D使用GaussianSplatting加载高斯泼溅模型
  • vivado时序约束和优化
  • 入门力扣自学笔记252 C++ (题目编号:1638)
  • PyTorch 之 简介、相关软件框架、基本使用方法、tensor 的几种形状和 autograd 机制
  • 基于token的身份认证及自动续期的解决方案
  • MySQL OCP888题解072-显示表的存储引擎的方法
  • 计算机网络第五章(运输层)【湖科大教书匠】
  • Python 彩蛋 —— 开发人员留下的惊喜
  • 快速融人,融资的共享模式,实体,线上皆可参考
  • 在 Flutter 多人视频通话中实现虚拟背景、美颜与空间音效
  • 【Java 数据结构与算法】-遍历Map和Set的方式
  • GitHub Copilot X:AI 协助编程的软件开发新时代来了!
  • 低功耗技术——流水线设计(加法器和乘法器)
  • 1、AzerothCore服务器编译并搭建
  • 什么是图神经网络?
  • Jetcache踩坑合集
  • soccer demo
  • keepalived+nginx 双机热备搭建
  • 信息技术最全总结(备考教资)
  • 图片压缩到200k以内怎么操作?图片压缩指定大小工具推荐
  • 4、操作系统——进程间通信(2)(system V-IPC介绍)
  • ASIC-WORLD Verilog(2)FPGA的设计流程