简体   繁体   中英

How to run TestNG classes one after another

I would like to run a testng.xml file that have 4 different classes . I want to run each one after the other is done. So when class 1 finish then class 2 starts , and when class 2 finish then class 3 starts executing. So far I have this code that i wrote but its triggering all classes in same time. Even I tried parallel = false and it didn't work. Any Help Will be much appreciated.Thanks

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Smoke Test"  parallel="false" preserve-order="true" verbose="2">
    <test name="Test 1">
        <classes>
            <class name="class1"></class>
        </classes>
    </test>
    <test name="Test 2">
        <classes>
            <class name="class2"></class>
        </classes>
    </test>
    <test name="Test 2">
        <classes>
            <class name="class3"></class>
        </classes>
    </test>
</suite>

What you need to do is use both the @BeforeClass and @AfterClass annotations for starting and quitting the webdriver. You need to define that the webdriver starts in the @BeforeClass and also that the webdriver quits in the @AfterClass method (if you so choose to close this webdriver at the completion of this class).

As an example...

@BeforeClass
public void initClass()
{
    //Set the system property
    String driverPath = System.getProperty("user.dir") +"\\chromedriver\\chromedriver.exe";
    String driverType = "webdriver.chrome.driver";
    
    System.setProperty(driverType, driverPath);

    driver = new ChromeDriver();
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
}

and at the end of this class, if I want to quit this webdriver instance:

@AfterClass
public void finish()
{             
    driver.manage().deleteAllCookies();
    driver.quit();
}

Do this for each of the test classes, and then the next class will only begin once the previous class completes.

TestNG xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
    <test thread-count="5" name="Test" preserve-order="true">
        <classes>
            <class name="Pkg.ClassA" />
            <class name="Pkg.ClassB" />
            <class name="Pkg.ClassC" />
        </classes>
    </test> <!-- Test -->
</suite> <!-- Suite -->

It's not quite the same setup, but we use "next" attributes in some of our jobs to have sequential processing:

<batch:job id="JOB_NAME" job-repository="jobRepository"> 
    <batch:step id="process1" next="process2"> 
        <batch:tasklet>
            <batch:chunk>
                processing info
            </batch:chunk>
        </batch:tasklet>
    </batch:step>

    <batch:step id="process2" next="process3">
        <batch:tasklet>
            <batch:chunk>
                processing info
            </batch:chunk>
        </batch:tasklet>
        </batch:tasklet>
    </batch:step>

Something like this may work for you.

Why not just add all your test classes into the same <test> tag and then run them ?

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Smoke Test"  parallel="false" preserve-order="true" verbose="2">
    <test name="Test 1">
        <classes>
            <class name="class1"></class>
            <class name="class2"></class>
            <class name="class3"></class>
        </classes>
    </test>
</suite>

This would basically execute test methods in each of the Test classes one after the other. You can confirm this by adding a System.out.println(Thread.currentThread().getId()); to each of your @Test methods. You should see the same thread id being printed.

Maybe you use "priority" and "dependsOnGroups" in your @Test annotation. This parameters are more important for order (see 6.10 changes https://github.com/cbeust/testng/blob/master/CHANGES.txt ).

Example with "priority".

This code:

public class Class1 {

    @Test(priority = 1)
    public void test1() {
        System.out.println("Class1 test1");
    }

    @Test(priority = 2)
    public void test2() {
        System.out.println("Class1 test2");
    }

}

public class Class2 {

    @Test(priority = 1)
    public void test1() {
        System.out.println("Class2 test1");
    }

    @Test(priority = 2)
    public void test2() {
        System.out.println("Class2 test2");
    }

}

Has result:

Class1 test1
Class2 test1
Class1 test2
Class2 test2

Example with "dependsOnGroups".

This code:

public class Class1 {

    @Test(groups = {"someGroup"})
    public void test1() {
        System.out.println("Class1 test1");
    }

    @Test(dependsOnGroups = {"someGroup"})
    public void test2() {
        System.out.println("Class1 test2");
    }

}

public class Class2 {

    @Test(groups = {"someGroup"})
    public void test1() {
        System.out.println("Class2 test1");
    }

    @Test(dependsOnGroups = {"someGroup"})
    public void test2() {
        System.out.println("Class2 test2");
    }

}

Has same result:

Class1 test1
Class2 test1
Class1 test2
Class2 test2

test name tag should be unique, I see test name for class 2 & class 3 are same, that's why you are seeing issue.

If you still seeing the issue please add logs/ additional information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM