Running JUnit4 Test classes in this order

I wrote several tests, divided not only into separate classes, but, depending on in which area of ​​my application they are tested, into separate subpackages. So my package structure looks something like this:

my.package.tests my.package.tests.four my.package.tests.one my.package.tests.three my.package.tests.two 

In the package my.package.tests , I have a parent class that passes all the tests in subpackages from one to four. Now I would like to choose the order of the tests; not included in the classes (which is possible using the FixMethodOrder annotation ), but the order of the classes themselves or subpackages (so those are the one package, then those that are listed in two , ect.). Some test classes use a Parameterized runner , in case it matters. The choice of tests is not required for the tests to be successful, they are independent of each other; however, it would be useful to sort them to reflect the order in which the various parts of the program are usually used, as this facilitates analysis.

Now, ideally, I would like to have some kind of configuration file that tells JUnit to order tests; I suppose, however, that it will not be so simple. What options do I have and what would be the easiest? I would also prefer only to list subpackages rather than the various classes in packages or even the testing functions in classes.

I am using Java 1.6.0_26 (I have no choice here) and JUnit 4.11.

+6
source share
1 answer

You can do this with test suites. (you can also nest them)

 @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 "toplevelsuite" as the entry point, and the tests will run in the order in which you define the @SuiteClasses array.

As for the methods inside the class, you can specify the order in which they are executed using @FixMethodOrder (as you mentioned in your question.

+8
source

Source: https://habr.com/ru/post/955239/


All Articles