在 Python 中,我经常有看起来像这样的测试:
tests = [
(2, 4),
(3, 9),
(10, 100),
]
for (input, expected_output) in tests:
assert f(input) == expected_output
用 JUnit 在 Java 中编写这样的测试(指定一组测试用例,然后循环运行每个测试用例)的“正确”方法是什么?
谢谢!
先发制人的 react :我意识到我可以做类似的事情:
assertEquals(4, f(2))
assertEquals(9, f(3))
....
但是...我希望有更好的方法。
最佳答案
同样的事情。
int[][] tests = {
{2, 4},
{3, 9},
{10, 100}
};
for (int[] test : tests) {
assertEquals(test[1], f(test[0]));
}
当然不如 python 漂亮,但很少有东西漂亮。
您可能还想查看 JUnit Theories , future 的功能...
关于java - 测试Java方法输出的 "right"方式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/663005/