如何为一种测试方法运行许多测试用例 [英] How can I run many test cases for a test method
问题描述
我正在使用JUnit.我有一个测试方法来测试一个方法和一些测试用例.我想在该测试方法中运行所有tes案例,但是我不能这样做.当第一个测试用例失败时,测试方法将不运行第二个测试用例
I'm using JUnit.I have a test method to test a method and some test cases. I want to run all tes case in that test method, but I can't do that. When first test case fail, test method don't run second test case
这是我的代码
public class ComputeServiceTest extends TestCase {
//test add method
public void testAdd()
{
ComputeServices 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
int x2 = 9;
int y2 = 6;
int expResult2 = 15;
int result2 = instance.add(x2, y2);
assertEquals("Second test case fail",expResult2, result2);
}
我该怎么做?
推荐答案
此处的标准建议是将您的第二个测试用例放入一个单独的方法中,然后不管第一个测试用例"是否成功,它都将运行是否.
The standard advice here would be to put your second test case into a separate method, then it will run regardless of whether or not the first "test case" succeeds or not.
您可以使用setUp方法来初始化ComputeServices实例,这样就不必在每个测试方法中都使用该样板.
You can use a setUp method to initialize the ComputeServices instance so you don't need that boilerplate in each test method.
这篇关于如何为一种测试方法运行许多测试用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!