Java JUnit 测试用例
例如,在
public void divide(Integer a){
//.....
}
Java测试中的方法中,我们需要测试参数为String,null,0,Long ....,我们是否有一个工具可以自动测试所有这些情况?
谢谢
For example, in a method
public void divide(Integer a){
//.....
}
In a Java test, we need to test a parameter as String, null, 0, Long .... , do we have a tool which will automatically test these all cases ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,编译器不会让您传递 String 或 Long,因此对它们进行单元测试是没有意义的。
其次,虽然有一些像您所描述的那样的工具,但我不会推荐它们。有效的单元测试并不是试图覆盖尽可能多的状态空间(因为状态空间无限大)。这是对“重大”案件的明智选择。决定什么被认为是重要的是你,程序员。
First, the compiler will not let you pass String nor Long so there's no point in unit-testing these.
Second, while there are some tools like the one you're describing, I wouldn't recommend them. Effective unit testing is not about trying to cover as much of the state space as possible (because the state space is infinitely large). It is about the wise choice of the "significant" cases. The one who decides what is considered to be significant is you, the programmer.
您可以使用 JUnit TestCase 自己编写此代码。我不知道有什么免费工具会自动用所有可能的输入来轰炸它,看看它会如何反应,但我确实知道 AgitarOne 软件产品(商业)可以进行此类测试。它称为“搅动”,可让您探索代码如何处理空值、最大值、最小值等。
Well you can write this yourself using a JUnit TestCase. I don't know of any free tools which will automatically bombard it with all possible inputs to see how it will react, but I do know of the AgitarOne software product (commercial) which does this kind of testing. It's called agitation and lets you explore how your code behaves with nulls, max values, min values, etc.
为什么不将 JUnit 4 与参数化测试用例一起使用? JUnit 3 不支持参数化测试用例 OOTB,但 JUnit 4 具有特定的 API 来接受参数并使用这些参数作为输入来调用测试用例。这对于你的情况来说应该是可行的吗?
Why not use JUnit 4 with a Parameterized test case? JUnit 3 did not support parameterized test cases OOTB, but JUnit 4 has specific API to accept arguments and invoke your test case using those parameters as input. Should that be feasible in your case?