在Junit 4中运行所有测试 [英] Run all tests in Junit 4

查看:111
本文介绍了在Junit 4中运行所有测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过编程方式在项目中运行所有测试。我知道Eclipse有一个运行为JUnit测试配置,它以某种方式抓住项目中的所有测试并运行它们。我有没有办法通过编程方式获取测试列表并运行它们?还是有一些很好的方式构建一个包含所有测试用例的测试套件,而不需要手动列出每个测试用例(全部700+)?



我在Eclipse中尝试过New ... - > Test Suite选项,但这似乎只适用于JUnit 3,通过扩展从TestCase



测试类是JUnit 4,所以它们唯一的区别特征是注释,没有命名约定,没有TestCase的子类化。



提前感谢

解决方案

虽然这并不能真正解决您的直接问题,创建套房和套房的有用的一般做法,例如对于一些类似于 PackageFooSuite 等的套件,再次将这些套件组合在一个或多个套件中,如 ModuleFooSuite 并具有一个顶级套件,如 AllTestsSuite 。这样一来,可以轻松运行所有测试,以及对我正在处理的软件包的子模块测试(并且测试运行速度比我总是运行所有测试的速度更快):

  @RunWith(Suite.class)
@ Suite.SuiteClasses({PackageFooSuite.class,PackageBarSuite.class})
public final AllTestsSuite {} //或ModuleFooSuite,以及AllTests中的


I want to be able to run all tests in a project programmatically. I know Eclipse has a "Run as JUnit test" configuration which somehow grabs all the tests in a project and run them. Is there any way for me to also grab the list of tests programmatically and run them? Or is there some good way to construct a test suite containing all the test cases without manually listing out every one (all 700+) of them?

I've tried the "New... -> Test Suite" option in Eclipse, but that seems to work only for JUnit 3, identifying tests by their extending from TestCase

The test classes are JUnit 4, so their only distinguishing characteristic is the annotation, no naming convention, no subclassing from TestCase.

Thanks in advance!

解决方案

Though it does not really solve your immediate problem, I find it a very useful general practice to create suites and suites of suites, e.g. for a package something like PackageFooSuite etc. and assemble these suites in one or more suites again, like ModuleFooSuite and have one top-level suite, like AllTestsSuite. That way it's easy to run both all tests in one step as well as submodule tests for the package I'm currently working on (and have the tests run quicker than if I would always run all of them):

@RunWith(Suite.class)
@Suite.SuiteClasses({ PackageFooSuite.class, PackageBarSuite.class} )
public final class AllTestsSuite {} // or ModuleFooSuite, and that in AllTests

这篇关于在Junit 4中运行所有测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆