[英]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
作為 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
是雙 class 的 static 最終成員。 您不能在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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.