JUnit4中的测试套件

测试套件
  JUnit3.8中,用测试套件同时运行多个测试类(http://www.cnblogs.com/mengdd/archive/2013/04/07/3006265.html)。
  在JUnit4中也有类似功能,只不过是用注解来实现的。
Suite类的文档

public class Suite
extends org.junit.internal.runners.CompositeRunnerUsing
Suite as a runner allows you to manually build a suite containing tests from many classes.
It is the JUnit 4 equivalent of the JUnit 3.8.x static Test suite() method.
To use it, annotate a class with @RunWith(Suite.class) and @SuiteClasses(TestClass1.class, ...).
When you run this class, it will run all the tests in all the suite classes.

程序实例
  写一个类TestAll,然后让其运行CalculatorTest和ParametersTest中的测试:

package com.mengdd.junit4;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
// 指定运行器
@Suite.SuiteClasses({ CalculatorTest.class, ParametersTest.class })
// 指定要测试的类
public class TestAll
{

}

在一个套件中也可包含另一个套件类,比如:
再写一个类TestAll2,其中指定的类是上面的TestAll:

package com.mengdd.junit4;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses(TestAll.class)
// 除了指定类,也可以指定套件类
public class TestAll2
{

}

总结:
  在JUnit4中,如果想要同时运行多个测试类,需要使用两个注解:
  @RunWith(Suite.class)指定使用Suite运行器来运行测试;
  @SuiteClasses(ClassName.class)指定运行哪些测试类。测试类可以指定为Suite,这样JUnit会继续再去寻找里面的测试类,一直找到能够执行的Test Case并执行之。

参考资料
圣思园张龙老师视频教程。
JUnit4 chm格式文档网盘下载链接:
JUnit4.0: http://pan.baidu.com/share/link?shareid=539345&uk=2701745266

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

专注网赚的程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值