JUnit3 一次运行多个测试类和进行多次重复测试:使用测试套件和RepeatedTest
测试套件
如果测试类写到很多,每次要进行测试,难道要重新点击每一个测试类来运行?如果有200个测试类要测试呢?
为了解决这个问题,引入了测试套件(TestSuite)。
通过将多个测试放入套件中,一并执行多个测试。
实例:一次运行多个测试类
比如,有两个测试类,叫CalculatorTest和CalculatorTest2。
在测试类所在的包中新建一个类,叫做TestAll,如下:
package com.mengdd.junit; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; public class TestAll extends TestCase { public static Test suite() { // 创建一个测试套件 TestSuite suite = new TestSuite(); // 增加测试类的class对象 suite.addTestSuite(CalculatorTest.class); suite.addTestSuite(CalculatorTest2.class); return suite; } }
运行时,右键这个类,Run As ->JUnit Test:
就相当于运行了CalculatorTest和CalculatorTest2。
实例:进行方法的重复测试
如果要对某个方法进行多次测试,可以使用RepeatedTest。
使用时注意,测试类需要一个带参数(String)的构造函数,将String参数传给基类TestCase,调用基类的带参数的构造函数。
于是修改之前的代码如下:
被测试类不变:

package com.mengdd.junit; public class Calculator { public int add(int a, int b) { return a + b; } public int subtract(int a, int b) { return a - b; } public int multiply(int a, int b) { return a * b; } public int divide(int a, int b) throws Exception { if(0 == b) { throw new Exception("除数不能为0"); } return a / b; } }
测试类:

package com.mengdd.junit; import junit.framework.Assert; import junit.framework.TestCase; public class CalculatorTest extends TestCase { private Calculator calculator = null; public CalculatorTest() { super(); } public CalculatorTest(String name) { super(name); } @Override public void setUp() throws Exception { System.out.println("set up"); // 生成成员变量的实例 calculator = new Calculator(); System.out.println(calculator); } @Override public void tearDown() throws Exception { System.out.println("tear down"); } public void testAdd() { int result = calculator.add(1, 2); // 判断方法的返回结果 Assert.assertEquals(3, result);// 第一个参数是期望值,第二个参数是要验证的值 } public void testSubtract() { int result = calculator.subtract(1, 2); // 判断方法的返回结果 Assert.assertEquals(-1, result);// 第一个参数是期望值,第二个参数是要验证的值 } public void testMultiply() { int result = calculator.multiply(2, 3); // 判断方法的返回结果 Assert.assertEquals(6, result);// 第一个参数是期望值,第二个参数是要验证的值 } public void testDivide() { int result = 0; try { result = calculator.divide(12, 3); } catch (Exception e) { e.printStackTrace(); // 如果抛出异常,证明测试失败,没有通过,没通过的测试计数在Failures中 Assert.fail(); // 如果不加这一行,如果程序进入到catch,无法判断其失败 } // 判断方法的返回结果 Assert.assertEquals(4, result);// 第一个参数是期望值,第二个参数是要验证的值 } public void testDivideByZero() { Throwable tx = null; int result = 0; try { result = calculator.divide(12, 0); Assert.fail("没有抛出异常,测试失败");// 如果执行到这行代码,则证明没有抛出异常,说明我们的验证失败 } catch (Exception e) { e.printStackTrace(); tx = e; } Assert.assertEquals(Exception.class, tx.getClass());// 抛出的异常类型是否和期望一致 Assert.assertEquals("除数不能为0", tx.getMessage());// 抛出的异常信息是否和期望一致 //如果上面两个都通过,则测试通过 } }
测试套件类:
package com.mengdd.junit; import junit.extensions.RepeatedTest; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; public class TestAll extends TestCase { public static Test suite() { // 创建一个测试套件 TestSuite suite = new TestSuite(); // 增加测试类的class对象 suite.addTestSuite(CalculatorTest.class); suite.addTestSuite(CalculatorTest2.class); // 测试某个方法多次,注意测试类需要提供带参数的构造方法 suite.addTest(new RepeatedTest(new CalculatorTest("testSubtract"), 20)); return suite; } }
运行结果:
参考资料
圣思园张龙老师视频教程
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了