Unit test – run Testng test in random order

Similar to How can I make my JUnit tests run in random order?, I want TestNG to run my tests in random order, so unconscious dependencies cannot be entered.

TestNG manual states:

By default, TestNG will run the tests found in your testng.xml file in
a random order.< /p>

However, I created a small test project with a simple testng.xml:









The package testngtests contains two test classes (MyTest1, MyTest2), which contain some empty methods, as shown below:

 @Test
public void testOne(){

}

The test methods are all empty, but the names are different.

When I run them When running the program (using Eclipse TestNG or on the command line), the tests are consistently run in the same order (i.e. sorted alphabetically, first sorted by class, then sorted by method name).

Then the documentation is it wrong?

Or “random order” just means “no guaranteed order”? Then, how do I make TestNG automatically randomize test orders?

Yes, “random” should be “unpredictable”.

If you want real randomization, please look for IMethodInterceptor, where TestNG will provide you with the opportunity to change the order to whatever you like.

Similar to How can I make my JUnit tests run in random order?, I want TestNG to run my tests in random order, so unconscious dependencies cannot enter.

TestNG manual states:

p>

By default, TestNG will run the tests found in your testng.xml file in
a random order.

But , I created a small test project with a simple testng.xml:









< p>The package testngtests contains two test classes (MyTest1, MyTest2), which contain some empty methods, as shown below:

@Test
public void testOne() {

}

The test methods are all empty, but the names are different.

When I run them (using Eclipse TestNG to run the program or on the command line ), test one Consistently run in the same order (ie sort alphabetically, first sort by class, then sort by method name).

So is the documentation wrong?

Or “random order” just means “no guaranteed order”? Then, how do I make TestNG automatically randomize test orders?

Yes, “random” should be “unpredictable”.

If you want real randomization , Please look for IMethodInterceptor, where TestNG will provide you with the opportunity to change the order to whatever you like.

Leave a Comment

Your email address will not be published.