如何使用setUp()和tearDown()多次调用assertXXX
我正在使用 JUnit。 我想在一个测试方法中多次调用assertEquals()来测试许多不同的测试用例,但我不想有很多测试方法。 所以我使用setUp()和tearDown()。 但是当第一个assertEquals()失败时,第二个assertEquals()不起作用,setUp()方法只被调用一次。
这是我的代码
public class ComputeServiceTest extends TestCase {
private ComputeServices instance = new ComputeServices();
public ComputeServiceTest(String name)
{
super(name);
}
protected void setUp()
{
System.out.println("aaaaaaaaaaaaaaaaaaaaa");
instance = new ComputeServices();
}
protected void tearDown() {
}
//test add method
public void testAdd1()
{
//instance = new ComputeServices();
//First test case
int x1 = 7;
int y1 = 5;
int expResult1 = 13;
int result1 = instance.add(x1, y1);
assertEquals("First test case fail",expResult1, result1);
// Second test case
System.out.println("AAAAAAAAAAAAAAAAAAAAAaaaaaaaaaa");
int x2 = 9;
int y2 = 6;
int expResult2 = 16;
int result2 = instance.add(x2, y2);
assertEquals("Second test case fail",expResult2, result2);
}
}
请帮助我修复这个错误。
I'm using JUnit.
I want to invoke assertEquals() many times in a test method to test many diferent test cases but I don't want to have many test method.
So I use setUp() and tearDown().
But when the first assertEquals() fail.The second assertEquals()doesn't work and setUp() method just have been invoked one time.
Here is my code
public class ComputeServiceTest extends TestCase {
private ComputeServices instance = new ComputeServices();
public ComputeServiceTest(String name)
{
super(name);
}
protected void setUp()
{
System.out.println("aaaaaaaaaaaaaaaaaaaaa");
instance = new ComputeServices();
}
protected void tearDown() {
}
//test add method
public void testAdd1()
{
//instance = new ComputeServices();
//First test case
int x1 = 7;
int y1 = 5;
int expResult1 = 13;
int result1 = instance.add(x1, y1);
assertEquals("First test case fail",expResult1, result1);
// Second test case
System.out.println("AAAAAAAAAAAAAAAAAAAAAaaaaaaaaaa");
int x2 = 9;
int y2 = 6;
int expResult2 = 16;
int result2 = instance.add(x2, y2);
assertEquals("Second test case fail",expResult2, result2);
}
}
Pls help me to fix thi bug.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试使用参数化测试。
Try using parametrized tests.
我认为您没有正确使用该工具(JUnit)。
单元测试要么失败,要么成功,不存在半成功或半失败的测试。
单个设置必须足以满足每个测试的需要,如果您需要多个设置调用,那么您就将许多测试放在一个测试中。
我认为将测试分成许多较小的测试没有任何缺点。设置和拆卸不应非常繁重,测试应始终以毫秒为单位运行,并且测试的粒度越高越好,这样您就可以准确地知道哪些功能已停止工作。
I don't think you are using the tool (JUnit) correctly.
A unit test can either fail or success, there is no such a thing as a half-succeeded or half-failed test.
A single setup has to be enough for every test, if you need more than one setup call then you are putting many tests in one.
I don't think there is any drawback on splitting your test into many smaller tests. Setup and teardown shouldn't be very heavy, tests should alway run in the order of ms and the higher the granularity of the test the better, so you know exactly what functionality has stopped working.
您应该让它们工作,或者如果您现在不想要它们,请将它们写入许多测试方法中。
问候
迈克
[;-)
You should make them work or if that's not you want right now, write them into many test methods.
Regards
Mike
[;-)
如果您确实只想只使用一种测试方法,则可以在独特的测试方法末尾有一个断言:
但我不会建议这样做:那么您如何知道哪个测试失败了?你不会有任何细节。
为什么您不想避免在 TestCase 中使用多种测试方法?可能还有另一种方法可以实现您想要的目标。
If you really wants to do have one test method only, you could have a single assertion at the end of your unique test method:
But I won't recommend that: how would you know which test failed then ? You won't have any detail.
Why don't you want to avoid having several test methods in your TestCase ? There could be another way to achieve what you want.