问题描述
我试图用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 (将#修改为@)