按指定顺序运行JUnit4测试类 [英] Running JUnit4 Test classes in specified order
问题描述
我编写了许多测试,这些测试不仅分为单独的类,而且根据要测试的应用程序领域的不同,分为单独的子包.所以,我的包结构看起来像这样:
I've written a number of tests, divided not only into separate classes but, depending on what area of my application they're testing, into separate sub-packages. So, my package structure looks a bit like this:
my.package.tests
my.package.tests.four
my.package.tests.one
my.package.tests.three
my.package.tests.two
在my.package.tests
程序包中,我有一个父类,子程序包中的所有测试都扩展到一到四个.现在,我想选择测试的运行顺序;不在类中(使用 FixMethodOrder注释似乎可以实现)但是类或子包本身的顺序(因此,首先在子包one
中的,然后在two
中的,等等).某些测试类使用参数化运行器,以防万一这有所作为.测试成功不需要选择顺序,因为它们彼此独立.但是,对它们进行排序以反映程序的各个部分通常被使用的顺序会有所帮助,因为这样会使分析更加容易.
In the my.package.tests
package I have a parent class that all tests in the sub-packages one through four extend. Now, I would like to choose the order in which the tests are run; not within the classes (which seems to be possible with the FixMethodOrder annotation) but the order of the classes or sub-packages themselves (So those in sub-package one
first, then those in two
, ect.). Some of the test classes use the Parameterized runner, just in case that makes a difference. Choosing the order is not required for the tests to succeed, they are independent of each other; it would however be helpful to sort them to reflect the order in which the various parts of the program are normally used as it makes the analysis a bit easier.
现在,理想情况下,我想拥有一些配置文件,该文件告诉JUnit应该以哪种顺序执行测试;我猜不过这不会那么容易.我在这里有哪些选择?最简单的选择是什么?我还希望只列出子包,而不是列出包中的各个类甚至列出这些类中的测试函数.
Now, ideally I would like to have some configuration file which tells JUnit which order the tests should be executed in; I'm guessing however that it won't be so easy. Which options do I have here and what would be the easiest one? I'd also prefer to only have to list the sub-packages, not the various classes in the packages or even the test functions in the classes.
我正在使用Java 1.6.0_26(这里没有选择)和JUnit 4.11.
I'm using Java 1.6.0_26 (I don't have a choice here) and JUnit 4.11.
推荐答案
您可以使用测试套件进行此操作. (您也可以嵌套"这些内容)
You can do this with test suites. (you can "nest" these, too)
@SuiteClasses({SuiteOne.class, SuiteTwo.class})
@RunWith(Suite.class)
public class TopLevelSuite {}
@SuiteClasses({Test1.class, Test2.class})
@RunWith(Suite.class)
public class SuiteOne {}
@SuiteClasses({Test4.class, Test3.class})
@RunWith(Suite.class)
public class SuiteTwo {}
...依此类推.使用"toplevelsuite"作为入口点,测试将按照定义@SuiteClasses
数组的顺序执行.
... And so on. Use the "toplevelsuite" as an entry point, and the tests will execute in the order in which you define the @SuiteClasses
array.
对于类中的方法,您可以使用@FixMethodOrder
指定它们执行的顺序(如您在问题中已经提到的那样.
As for the methods within a class, you can specify the order they execute in with @FixMethodOrder
(as you have already mentioned in your question.
这篇关于按指定顺序运行JUnit4测试类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!