1

how to run all methods one by one in multiple class by setting priority using testng?

public class test1 {
    @Test(priority = 1)
    public void test1()
    {
    System.out.println("test1");
    }
    @Test(priority = 2)
    public void test2()
    {
     System.out.println("test2");
    }
}
public class test2 {
    @Test(priority = 1)
    public void test3()
    {
    System.out.println("test3");
    }
    @Test(priority = 2)
    public void test4()
    {
    System.out.println("test4");
    }
}

Expected output

test1
test2
test3
test4

but getting

test1
test3
test2
test4

how to run class 1 firs and then class 2?

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="SANITY SUITE">
<test name="TESTCASE1" >
<classes>
<class name="demo.demo.test1"/>
<class name="demo.demo.test2"/>
</classes>
</test>
</suite>
James Z
  • 12,209
  • 10
  • 24
  • 44
  • Have you tried any of the proposed answers? Would be nice if you could leave some feedback for future readers. A full working project used for my answer can be found at https://github.com/SubOptimal/stackoverflow-answers/tree/master/question-59639168 – SubOptimal Jan 09 '20 at 07:42

2 Answers2

1

In your suite xml use this: <test name="TESTCASE1" group-by-instances="true" >

https://stackoverflow.com/a/26635229/8794926

Vladucu Voican
  • 283
  • 1
  • 10
0

From the documentation (search for preserve-order)

By default, TestNG will run your tests in the order they are found in the XML file.

Assuming following structure

pom.xml
src/test/java/testng.xml
src/test/java/demo/demo/test2.java
src/test/java/demo/demo/test1.java

snippet in the pom.xml

<properties>
    <maven.compiler.source>13</maven.compiler.source>
    <maven.compiler.target>13</maven.compiler.target>
</properties>
<dependencies>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.0.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.0</version>
            <configuration>
                <suiteXmlFiles>
                    <suiteXmlFile>${project.build.testSourceDirectory}/testng.xml</suiteXmlFile>
                </suiteXmlFiles>
            </configuration>
        </plugin>
    </plugins>
</build>

testnng.xml - the one you provided

test1.java and test2.java - the ones you provided with added import and package statement

Running the tests as mvn test will produce the expected output

[INFO] --- maven-surefire-plugin:2.22.0:test (default-test) @ playground.testng ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running TestSuite
test1
test2
test3
test4
[INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.758 s - in TestSuite

Some possible reasons why it fails in your case:

  • somehow the default behavior of preserve-order is overwritten
  • you call your tests in a way the testng.xml is not taken into account
SubOptimal
  • 22,518
  • 3
  • 53
  • 69