0

I've created Maven project which has structure like following:

src/main/java
src/main/resources
src/test/java
src/test/resources

I've kept my TestNG tests in src/test/java folder. Suppose in this folder, I've three classes:

  1. YahooTest
  2. GoogleTest
  3. TwitterTest

and each of one have few TestNG test methods.

When I hit mvn clean test on command prompt, I observed tests running in following order:

  1. test from GoogleTest
  2. test from TwitterTest
  3. test from YahooTest
  4. test from GoogleTest
  5. test from TwitterTest
  6. test from YahooTest
  7. test from GoogleTest
  8. test from TwitterTest
  9. test from YahooTest

Means first classes are ordered in alphabetical order i.e. GoogleTest, TwitterTest, YahooTest and then one test from each class is executed and again one test from each class is executed. This continues until all test methods execution finishes.

But I want to customize the test execution.

How can I achieve following:

  1. Arrange test execution of test classes in the order I want rather than in alphabetical order
  2. Instead of executing one test method from each test class and then one test method from each test class ..., execute all test cases from same class and then move to another test class
Alpha
  • 13,320
  • 27
  • 96
  • 163

3 Answers3

1

Consider this link. you can specify the runOrder with the following code

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
    <runOrder>alphabetical</runOrder>
</configuration>

in your pom.xml

shikjohari
  • 2,278
  • 11
  • 23
1

Consider too this link

In TestNG, you use dependsOnMethods and/or dependsOnGroups:

@Test(groups = "a")
public void f1() {}

@Test(groups = "a")
public void f2() {}

@Test(dependsOnGroups = "a")
public void g() {}

In this case, g() will only run after f1() and f2() have completed and succeeded.

Community
  • 1
  • 1
Iker Aguayo
  • 3,980
  • 4
  • 37
  • 49
0

Alphabetical is how testng orders it. One way can be to use a suite.xml from which you can customize your run any way you like.

The below would run all your classes in alphabetical order but one after the other

  <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="Suite1" verbose="1" >

  <test name="Check" preserve-order="true"> //This is true by default
      <packages>
          <package name="testngtests.mytests"></package>
      </packages>

  </test>

</suite>

If you want to have a particular order of class execution then instead of specifying packages tag, use the classes tag and put the class names in the order you want them to run.

<suite name="TmpSuite" >
  <test name="TmpTest" >
    <classes>
      <class name="YahooTest"  />
      <class name="GoogleTest"  />
    <classes>
    </test>
</suite>

You would need to specify this xml in the surefire plugin configuration section - Refer the using suitexml file section here

niharika_neo
  • 8,441
  • 1
  • 19
  • 31
  • Niharika, thank you so much for this detailed information!!! The classes are run in alphabetical order but not one after another as I'm expecting. Rather tests are run in following order: `first test from first class, first test from second class, ..., second test from first class, second test from second class...` – Alpha Nov 17 '14 at 15:01
  • Do you have a parallel attribute set somewhere? – niharika_neo Nov 18 '14 at 05:06
  • No, I haven't mentioned parallel attribute anywhere in testng.xml file. – Alpha Nov 18 '14 at 06:24
  • Then it's not possible. What is the version of testng are you on? For me it works fine on 6.8.8. Entire class gets executed first, then second class is taken. – niharika_neo Nov 18 '14 at 06:34
  • Niharika, I was using Testng 6.1.1 and now updated it to 6.8.8 and now tests are running as expected. Thank you so much!!! :) – Alpha Nov 18 '14 at 08:31