简体   繁体   English

按指定顺序运行JUnit4 Test类

[英]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

In the my.package.tests package I have a parent class that all tests in the sub-packages one through four extend. my.package.tests包中,我有一个父类,子类包中的所有测试都是一到四个扩展。 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.). 未中的类(这似乎是可能与FixMethodOrder注解 ),但类或子包本身的顺序(因此,那些在子包one第一,那么那些在two ,等。)。 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. 然而,对它们进行排序以反映通常使用程序的各个部分的顺序是有帮助的,因为它使分析更容易一些。

Now, ideally I would like to have some configuration file which tells JUnit which order the tests should be executed in; 现在,理想情况下我想要一些配置文件告诉JUnit应该执行哪些测试顺序; 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. 我也更喜欢只需列出子包,而不是包中的各种类,甚至是类中的测试函数。

I'm using Java 1.6.0_26 (I don't have a choice here) and JUnit 4.11. 我正在使用Java 1.6.0_26(我在这里没有选择)和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 {}

... 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. 使用“toplevelsuite”作为入口点,测试将按照定义@SuiteClasses数组的顺序执行。

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. 对于类中的方法,您可以使用@FixMethodOrder指定它们执行的顺序(正如您在问题中已经提到的那样)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM