gpt4 book ai didi

java - 在 JUnit 5 参数化测试中。 CSV 输入。有没有办法通过 Double.NaN、Double.POSITIVE_INFINITY?

转载 作者:行者123 更新时间:2023-12-04 09:37:32 25 4
gpt4 key购买 nike

我想通过 Double.NEGATIVE_INFINITY , Double.POSITIVE_INFINITYDouble.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 类的静态最终成员。您不能在 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);
}

关于java - 在 JUnit 5 参数化测试中。 CSV 输入。有没有办法通过 Double.NaN、Double.POSITIVE_INFINITY?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62496734/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com