Running JUnit4 Test classes in specified order

馋奶兔 提交于 2020-02-26 18:24:52

问题


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. 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.

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.

I'm using Java 1.6.0_26 (I don't have a choice here) and JUnit 4.11.


回答1:


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.

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.



来源:https://stackoverflow.com/questions/19177195/running-junit4-test-classes-in-specified-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!