简体   繁体   中英

How to avoid executing the method written in @AfterSuite if a single test is executed as 'TestNG Test' or using the 'Run' command provided by plugin

BaseTest Class

public class BaseTest extends Base {
    LoginPage login;
    Logger logger = Logger.getLogger(Base.class);

    @BeforeMethod()
    public void setUp() throws IOException, IOException {
        logger.info("starting initialisation method from base class");
        initialisation();
        logger.info("completed initialisation method from base class");
        login = new LoginPage();
        logger.info("initialising login page and profile page object");
    }
    @AfterMethod
    public void tearDown() {
        logger.info("starting tear down method");
        Base.getDriver().close();
        logger.info("ending tear down method");
    }
    @AfterSuite
    public void sendEmailWithExtentReport() throws IOException {
        logger.info("starting sendEmailWithExtentReport");
        Utilities.sendJavaMailAfterExecution();
        logger.info("ending sendEmailWithExtentReport");
        
}

LoginTest class

public class LoginTest extends BaseTest {
@Test(priority = 0,description = "Test case to verify login button is present or not")
    public void loginPageLoadingTest() throws IOException, InterruptedException {
        logger.info("starting loginPageLoadingTest");
        Thread.sleep(2000);
        Assert.assertEquals(login.verifyLoginButton(), true);
        logger.info("ending loginPageLoadingTest");
    }
}

I need to skip the method in @AfterSuite if i am only executing the single test case in LoginTest class without using testng.xml file, so that I can verify each test case separately. Right now when individual test is executed the method in @AfterSuite is also getting executed. I want to execute the method in @AfterSuite only when test cases are executed using testng.xml file. How can i add a condition in @AfterSuite to achieve this. Please help.

You can test your suite name for equality to Default Suite . Say you have testng.xml like:

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

<suite name="Suite1" verbose="1" >
    <test name="MyTest" >
        <classes>
            <class name="click.webelement.Suites" />
        </classes>
    </test>
</suite>

where you define suite name explicitly. Then you can have the logic like this:

package click.webelement;

import org.testng.ITestContext;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.Test;

public class Suites {

    @Test
    public void doTest(){
        System.out.println("Doing test..");
    }

    @AfterSuite
    public void doingAfterSuite(ITestContext testContext){
        if(!testContext.getSuite().getName().equals("Default Suite")){
            System.out.println("Doing after suite");
        }
    }

}

Since when you run a single test, Default Suite is created you can just test if default suite is set in test context.

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