java - 在 JUnit 5 参数化测试中。CSV 输入。有没有办法通过 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));
}
不幸的是,它会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);
解决方案
正如错误所暗示的,JUnit 无法将源值转换为double
类型。Double.NEGATIVE_INFINITY
是 Double 类的静态最终成员。您不能在CsvSource
. 但是,您需要做的是通过 String 重新表示它。
来自 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);
}
推荐阅读
- reactjs - socketio.client 监听服务器
- python - Pandas:按时间间隔的另一个数据帧过滤数据帧
- python - 如何使 pyplot 图表中的 y 轴显示相同值的两个测量值(计数和百分比)?
- kubernetes - Kubernetes 是否重用了以前 cron 作业中的容器?
- javascript - PHP: '0' 被放置在 HTML 字符串的末尾
- c# - 创建配置节处理程序 workerConfiguration 时出错 - 启动项目
- python - 变分自动编码器 - 警告:张量流:当最小化损失时,变量 [] 不存在梯度
- python - 在 Python 中删除文件时如何自动按 Enter?
- c# - 计算达到一定百分比所需的数量
- javascript - 使用时刻和使用 .net 核心的 Web api 请求的日期格式不正确