May 15, 2021 jUnit
In a real project, as the project progresses, there will be more and more unit test classes, but until now we have only run the test classes one by one, which is certainly not feasible in the actual project practice. To solve this problem, JUnit provides a way to run test classes in bulk, called a test suite.
This way, each time you need to verify that the system is functionally correct, you can execute only one or more test suites. The test suite is very simple to write, and we need to follow the following rules:
package test;
import org.junit.Assert;
import org.junit.Test;
/**类描述:
*@author: zk
*@date: 日期:2018-6-6 时间:下午3:56:17
*@version 1.0
*/
public class JunitTestOne {
@Test
public void test() {
System.out.println("测试一。。。");
Assert.assertTrue(true);
}
}
package test;
import org.junit.Assert;
import org.junit.Test;
/**类描述:
*@author: zk
*@date: 日期:2018-6-6 时间:下午3:58:56
*@version 1.0
*/
public class JunitTestTwo {
@Test
public void test() {
System.out.println("测试二。。。");
Assert.assertTrue(true);
}
}
package test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
/**类描述:
*@author: zk
*@date: 日期:2018-6-6 时间:下午4:00:06
*@version 1.0
*/
@RunWith(Suite.class)
@SuiteClasses({ JunitTestOne.class,JunitTestTwo.class })
public class AllTests {
}
In the example code above, I put two test classes in the test suite AllTests, ran the test suite in Eclipse, and we could see that two test classes were called to execute. The test suite can contain not only basic test classes, but also other test suites, which makes it easy to layer unit test code for different modules.
Note: Be sure to ensure that there is no loop containing relationship between the test suites, otherwise endless loops will appear in front of us.