0

I am running these TestNG tests, through a java main method (thanks to another post - how to call testng.xml from java main method?)

public class MainTest {     //main
  public static void main(String[] args) {
     TestListenerAdapter tla = new TestListenerAdapter();
     TestNG testng = new TestNG();
     List<String> suites = Lists.newArrayList();
     suites.add("c:/AbcTests/xyztest.xml");
     testng.setTestSuites(suites);
     testng.run();
   }
}


public class Test001 {   //test
  @Test(priority=1)
  public void a1() {
    System.out.println("In a1 run");
  }
  @Test(priority=2)
  public void a2() {
    System.out.println("In a2 run");
  }
  @Test(priority=3)
  public void b1() {
    System.out.println("In b1 run");
  }
}

But when I run it, the tests are randomly picked.

Output:

In a2 run
In a1 run
In b1 run

When running from TestNG, it is fine.

Is there a way to prioritize tests in TestNG when running from a Java main?

------- EDIT ----------

Thank you tim-slifer. I had a look at the xml. It was :-

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Just Test" verbose="3" parallel="methods">
  <test name="Smoke">
    <classes>
        <class name="test2.Test001" />
    </classes>
  </test>
</suite>

I removed the parallel="methods". And changed it to

<suite name="Just Test" verbose="3">

And it works fine now. I think this was the mistake? Is it correct?

Community
  • 1
  • 1
Raj
  • 1
  • 3

1 Answers1

0

Following up to your edit to confirm your (and niharika_neo's) findings. The parallel attribute was the cause of your issue.

I was thinking either you had enabled parallel execution or perhaps you had included the test methods in a different order you wanted them executed. But, by including the class, as you've done, TestNG will execute the methods in order... So, the parallel was what was causing the unexpected results.

tim-slifer
  • 1,068
  • 1
  • 9
  • 17