How to run test methods ok with Junit

I am using JUnit and Selenium Webdriver. I want to run my test methods so that I can write them in my code, as shown below:

@Test public void registerUserTest(){ // code } @Test public void welcomeNewUserTest(){ // code } @Test public void questionaireNewUserTest(){ // code } 

But it does not work, it always executes my test methods in the following order:

 welcomeNewUserTest() registerUserTest() questionaireNewUserTest() 

I read the answer somewhere, if I call my method using the suffix test, then JUnit will execute them the way I order them in code. This does not seem to work.

Any help? Thanks

+7
java junit selenium-webdriver automated-tests
source share
4 answers

So, for such tests, where these steps depend on each other, you should really execute them as one unit. You really should do something like:

 @Test public void registerWelcomeAndQuestionnaireUserTest(){ // code // Register // Welcome // Questionnaire } 

As @Jeremiah mentions below, there are several unique ways that individual tests can run unpredictably.

Now that I have said this, here is your solution.

If you need separate tests, you can use @FixMethodOrder and then do it with NAME_ASCENDING . This is the only way I know.

 @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class TestMethodOrder { @Test public void testA() { System.out.println("first"); } @Test public void testC() { System.out.println("third"); } @Test public void testB() { System.out.println("second"); } } 

will execute:

testA(), testB(), testC()

In your case:

 @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class ThisTestsEverything{ @Test public void T1_registerUser(){ // code } @Test public void T2_welcomeNewUser(){ // code } @Test public void T3_questionaireNewUser(){ // code } } 
+6
source share

You cannot run your test methods in the order they are written. The test point must be independent. JUnit does not encourage independent tests.

But if you really want to ...

There is an annotation @FixMethodOrder. Please read the following FixMethodOrder annotation type

+3
source share

Use the following command on the class from which you will run your tests

 @FixMethodOrder(MethodSorters.JVM) public class TestMethodOrder { @Test public void signup() { System.out.println("Signup"); } @Test public void login() { System.out.println("Login"); } @Test public void navigate() { System.out.println("Navigate"); } } 

Annotation MethodSorters.JVM will run your tests the way you wrote them in your file. (Just like Java code, line by line)

0
source share

You can sort methods with the @FixMethodOrder(MethodSorters.NAME_ASCENDING) annotations @FixMethodOrder(MethodSorters.NAME_ASCENDING) . Like,

 @FixMethodOrder(MethodSorters.DEFAULT) 

public class DefaultOrderOfExecutionTest {private static StringBuilder output = new StringBuilder ("");

 @Test public void secondTest() { output.append("b"); } @Test public void thirdTest() { output.append("c"); } @Test public void firstTest() { output.append("a"); } @AfterClass public static void assertOutput() { assertEquals(output.toString(), "cab"); } 

}

You can sort in three ways:

  1. MethodSorters.DEFAULT - This default strategy compares test methods using their hash codes. In the event of a hash collision, the lexicographic order is used.
  2. MethodSorters.JVM - This strategy uses the natural JVM ordering, which may be different for each run.
  3. MethodSorters.NAME_ASCENDING - this strategy can be used to run the test in their lexicographical order.

For more information, please refer to: JUnit Test Procedure

0
source share

All Articles