如果单个测试执行为'TestNG Test'或使用插件提供的'Run'命令,如何避免执行@AfterSuite中编写的方法

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

基础测试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");
        
}

登录测试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");
    }
@Test(priority = 1, description = "Test case to verify login page title")
    public void loginPageTitleTest() throws IOException, InterruptedException {
        logger.info("starting loginPageTitleTest");
        Thread.sleep(2000);
        Assert.assertEquals(login.getLoginPageTitle(), "Console");
        logger.info("ending loginPageTitleTest");
    }
}

testng-console.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
    <listeners>
        <listener class-name="com.analyzer.MyTransformer" />
        <listener class-name="com.utilities.TestListener"></listener>
    </listeners>
    <test thread-count="5" name="Test">
        <classes>
            <class name="com.ui.tests.BaseTest" />
            <class name="com.ui.tests.LoginTest" />
            <class name="com.ui.tests.FilterTest" /> -->
        </classes>
    </test> <!-- Test -->
</suite> <!-- Suite -->

如果我只是从 LoginTest class 中的多个测试用例中执行单个测试用例而不使用 testng.xml 文件,我需要跳过 @AfterSuite 中的方法,以便我可以验证每个测试案例分开。现在,当执行单个测试时,@AfterSuite 中的方法也会被执行。我只想在使用 testng.xml 文件执行测试用例时才执行 @AfterSuite 中的方法。我如何在@AfterSuite 中添加一个条件来实现这一点。我也在命令提示符下使用这个命令“'mvn clean test -Dbrowsername=chrome'”。我的 testng 文件名是 testng-console.xml 。@AfterSuite 中的方法应该只在使用 tesng-console.xml 时执行。请帮忙。

您可以测试您的套件名称是否与 Default Suite 相等。假设您 testng.xml 喜欢:

<!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>

您在其中明确定义套件名称。然后你可以有这样的逻辑:

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().equalsIgnoreCase("Default Suite")){
            System.out.println("Doing after suite");
        }
    }

}

因为当您 运行 一个测试时,Default Suite 被创建,您可以只测试是否在测试上下文中设置了默认套件。

更新: Eclipse 插件将默认套件名称设置为 Default suite 因此我将 equals 更改为 EqualsIgnoreCase 以涵盖 Idea 和 Eclipse