Java自学-junit TestSuite

本文详细介绍了如何使用JUnit创建测试套件(TestSuite),通过整合多个测试类(TestCase1, TestCase2等)实现一次运行多测试的目的。文章展示了如何通过注解@RunWith和@Suite.SuiteClasses指定要运行的测试类集合,并提供了完整的代码示例。

junit TestSuite

步骤 1 : 多个测试

在 入门教程 中,讲解的是对一个工具类 SumUtil 的测试类 TestCase1.
如果有很多工具类需要被测试,那么就会有 TestCase2, TestCase3, TestCase4,

如果不得不挨个去执行这些单独的测试类,也是比较麻烦的,所以就有了 TestSuite的概念.

步骤 2 : TestCase2

TestSuite 。。。 其实就是一下执行多个测试类。 首先来个TestCase2, 它。。。其实和 TestCase1 的内容是一样的,假装是个新的TestCase吧。。。

package junit;
 
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
 
import junit.framework.Assert;
 
public class TestCase2 {
 
    @Before
    public void before() {
        System.out.println("测试前的准备工作,比如链接数据库等等");
    }
    @After
    public void after() {
        System.out.println("测试结束后的工作,比如关闭链接等等");
    }
     
    @Test
    public void testSum1() {
        int result = SumUtil.sum1(1, 2);
        Assert.assertEquals(result, 3);
    }
 
    @Test
    public void testSum2() {
        int result = SumUtil.sum2(1, 2,3);
        Assert.assertEquals(result, 5);
    }
}

步骤 3 : TestSuite

如代码所示的这么写。。。。就表示一下运行TestCase1 和 TestCase2 了。。。
运行之后可以看到,哪些成功了,哪些失败了

TestSuite

package junit;
 
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
 
@RunWith(Suite.class)
@Suite.SuiteClasses({TestCase1.class,TestCase2.class})
public class TestSuite {
 
}

步骤 4 : 可运行项目

junit可运行项目

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值