简体   繁体   中英

TestNG run once before running all test suites mentioned in the pom.xml

I have set up multiple testng.xml files in the Surefire plugin so that I can run the automation test from command prompt using Maven .

Now, I am facing a problem. How I set up the suiteListener to perform a few tasks like delete the files and screenshots that was captured from the previous run. (A single run consists of multiple suite files)

Now what is happening is that the 1st test suite runs and captures the screenshots and creates logs. When the 2nd suite is run, it clears the screenshots and logs that was captured earlier and creates a new screenshots for this run.

Is there a way that we can have this method run once for each run and not before every test suite .

import java.io.IOException;
import org.testng.ISuite;
import org.testng.ISuiteListener;
import com.company.appium.base.BaseTest;
public class suiteListener extends BaseTest implements ISuiteListener {

    @Override
    public void onStart(ISuite suite) {
        // This method will be executed before Test Suite run
        try {
            deletePreviousScreenShots();
            System.out.println("Inside onStart of suiteListener");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("Before starting test suite: " + suite.getName() + " in onStart() method");
    }
    @Override
    public void onFinish(ISuite suite) {
        // This method will be executed at the end of the Test Suite run
        System.out.println("After executing the test suite: " + suite.getName() + " in onFinish() method");
    }
} 

Since I am not sure if a new instance of suiteListener is created for each suite run. So what you could do is to proceed with the deletion only if the name of the suite received via ISuite suite parameter matches with the "first" xml suite file you have provided.

public class suiteListener extends BaseTest implements ISuiteListener {

    private static final String firstTest = "suite1";
    @Override
    public void onStart(ISuite suite) {
        if(!suite.getName().equals(firstTest)) {
            return;
        }

        // rest of the code
    }
}

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