在 JUnit 5 中参数化测试。 CSV 输入。有没有办法传Double.NaN、Double.POSITIVE_INFINITY?
In JUnit 5 parametrized tests. CSV inputs. Is there a way to pass Double.NaN, Double.POSITIVE_INFINITY?
我正在尝试将 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));
}
不幸的是,它会在 JUnit5 测试运行程序中触发类似“Error converting parameter at index 0: Failed to convert String "Double.POSITIVE_INFINITY" to type java.lang.Double
”的错误。
有没有什么好的方法可以自动传递这样的值进行测试,或者我只好单独写一个测试方法如下:
assertEquals(0, Double.NEGATIVE_INFINITY);
assertEquals(1, Double.POSITIVE_INFINITY);
assertEquals(Double.NaN, Double.NaN);
如错误所示,JUnit 无法将源值转换为 double
类型。 Double.NEGATIVE_INFINITY
是 Double class 的静态最终成员。您不能在 CsvSource
中传递变量名。但是,您需要做的是传递它的 String 重新表示。
来自 Java 文档:
- If the argument is NaN, the result is the string "NaN".
- If m is infinity, it is represented by the string "Infinity"; thus, positive infinity produces the result "Infinity" and negative infinity produces the result "-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);
}
我正在尝试将 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));
}
不幸的是,它会在 JUnit5 测试运行程序中触发类似“Error converting parameter at index 0: Failed to convert String "Double.POSITIVE_INFINITY" to type java.lang.Double
”的错误。
有没有什么好的方法可以自动传递这样的值进行测试,或者我只好单独写一个测试方法如下:
assertEquals(0, Double.NEGATIVE_INFINITY);
assertEquals(1, Double.POSITIVE_INFINITY);
assertEquals(Double.NaN, Double.NaN);
如错误所示,JUnit 无法将源值转换为 double
类型。 Double.NEGATIVE_INFINITY
是 Double class 的静态最终成员。您不能在 CsvSource
中传递变量名。但是,您需要做的是传递它的 String 重新表示。
来自 Java 文档:
- If the argument is NaN, the result is the string "NaN".
- If m is infinity, it is represented by the string "Infinity"; thus, positive infinity produces the result "Infinity" and negative infinity produces the result "-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);
}