Pav*_*nko 5 java unit-testing junit5
我正在尝试将Double.NEGATIVE_INFINITY,Double.POSITIVE_INFINITY和Double.NaN作为 JUnit5 中的 CSV 参数传递:
@DisplayName("Testing ActivationFunction.heaviside")
@ParameterizedTest(name = "Testing ActivationFunction.heaviside ({0},{1})")
@CsvSource({
"Double.NEGATIVE_INFINITY, 0",
"-100, 0",
"-1, 0",
"0, 0.5",
"1, 1",
"100, 1",
"Double.POSITIVE_INFINITY, 0",
"Double.NaN, Double.NaN"
})
void testActivationFunction_heaviside(double input, double output) {
assertEquals(output, ActivationFunction.heaviside(input));
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,它会Error converting parameter at index 0: Failed to convert String "Double.POSITIVE_INFINITY" to type java.lang.Double在 JUnit5 测试运行程序中触发类似“”的错误。有没有一种好方法可以自动传递这些值进行测试,或者我只需编写一个单独的测试方法,如下所示:
assertEquals(0, Double.NEGATIVE_INFINITY);
assertEquals(1, Double.POSITIVE_INFINITY);
assertEquals(Double.NaN, Double.NaN);
Run Code Online (Sandbox Code Playgroud)
正如错误所示,JUnit 无法将源值转换为double类型。Double.NEGATIVE_INFINITY是 Double 类的静态最终成员。您不能在 中传递变量名称CsvSource。但是,您要做的就是传递它的字符串重新表示。
来自 Java 文档:
- 如果参数为 NaN,则结果为字符串“NaN”。
- 如果m为无穷大,则用字符串“Infinity”表示;因此,正无穷大产生结果“Infinity”,负无穷大产生结果“-Infinity”。
因此,您可以重新建模您的代码,如下所示:
@DisplayName("Testing ActivationFunction.heaviside")
@ParameterizedTest(name = "Testing ActivationFunction.heaviside ({0},{1})")
@CsvSource({
"-Infinity, 0",
"-100, 0",
"-1, 0",
"0, 0.5",
"1, 1",
"100, 1",
"Infinity, 0",
"NaN, NaN"
})
void testActivationFunction_heaviside(double input, double output) {
System.out.println(input + " :: "+output);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1935 次 |
| 最近记录: |