如何解决JUnit5针对不同参数的多个来源笛卡尔积
我试图用JUnit 5编写一个测试,该测试应该测试某些参数的多个组合。本质上,我想测试来自不同来源的输入的某些笛卡尔积。考虑以下测试:
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
class CartesianProductTest {
@ParameterizedTest
@CsvFileSource(resources = { "values.csv" })
void testIt(int input,int expected,int otherParameter) {
assertEquals(expected,methodUnderTest(input,otherParameter));
}
}
现在的问题是,我在input
中只有expected
和values.csv
,应该测试otherParameter
的某些固定值,methodUnderTest()
总是返回所有这些值的期望值。我必须以某种方式提供CSV中所有值和otherParameter
可以取的所有值的笛卡尔乘积。我看着https://stackoverflow.com/a/57648088/7962200,但这需要对所有测试用例进行硬编码或手动读取CSV以提供值流。我想更多类似的东西
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvFileSource;
class CartesianProductTest {
@ParameterizedTest
@MethodSource
void testIt(int input,otherParameter));
}
static Stream<Arguments> testIt() {
return csvValues().flatMap(csv ->
otherParams().map(otherParam -> Arguments.of(csv,otherParam)));
}
@CsvFileSource(resources = { "values.csv" })
static Stream<Arguments> csvValues() {/* get somehow from Annotation */}
@CsvFileSource(resources = { "otherparam.csv" })
static Stream<Arguments> otherParams() {/* get somehow from Annotation */}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。