-1

I'm using a TestNG framework for my automation project.

While running from command line i'm giving the following command.

mvn clean test -Dtest=Login,OpenImage,Logout

By running the above command it the order of execution was Login->Logout->OpenImage (may be in alphabetical order).

Can anyone help me how to run tests in the given order.

Note: As per my requirement I need to run my tests in the above way it self. If it was through testNG.xml file then i guess preserve-order will work.

can anyone help me on this.....!!!!! Thank you in advance..

  • 1
    First why do you need to run tests in a particula order because units should never rely on a particular order... If you need to run in order defined dependencies between the tests (`@Test(dependsOnMethods = { "serverStartedOk" })`)... Or you could use JUnit Jupiter which supports an order annotation... – khmarbaise May 17 '22 at 16:40
  • 1
    TestNG allows to organize tests using suites (xml files) - that is what you actually need. – Andrey B. Panfilov May 17 '22 at 16:55
  • 1
    It most of the time easier to use dependsOnGroups etc. in combination with dependsOnMethods than manually mantain test suites... – khmarbaise May 17 '22 at 17:27
  • BTW: What I missed you should use maven-failsafe-plugin (responsible for integration tests) in such case because as already mentioned `mvn test` runs unit tests executed by maven-surefire-plugin... – khmarbaise May 17 '22 at 17:28

1 Answers1

1

First why do you need to run tests in a particula order because units should never rely on a particular order. But your question looks like more an integration tests.

If you need to run in order defined dependencies between the tests

@Test
public void serverStartedOk() {}
 
@Test(dependsOnMethods = { "serverStartedOk" })
public void method1() {}

The above defines the order that serverStartedOk will run before method1..based on the dependsOnMethods...

khmarbaise
  • 92,914
  • 28
  • 189
  • 235
  • > because units should never rely on a particular order - it is so wrong for e2e tests. – Andrey B. Panfilov May 17 '22 at 16:47
  • We are not talking about E2E tests.. here we are talking about IT's ... which are not unit test which are given by the usage of `mvn test` which will run surefire-plugin which is for unit tests... – khmarbaise May 17 '22 at 17:25