首页 文章

TestNG - 如果满足条件,如何强制从BeforeSuite注释结束整个测试套件

提问于
浏览
3

如果在@BeforeSuite注释中满足条件,是否有办法退出整个测试套件?也许是一种调用@AfterSuite并绕过整个测试的方法?

我在@BeforeSuite中进行数据库调用 . 如果查询返回任何结果,我发送一封电子邮件,现在我想要杀死整个测试套件 .

我尝试了 System.exit(1);org.testng.Assert.fail("There are unpaid invoices"); ,但这些都没有终止整个套件 . 我的脚本设置为并行运行类,当我从test.xml文件运行测试时,每个类都尝试启动并打开一个窗口,然后立即关闭它 .

仅供参考,在@BeforeClass或@BeforeMethod之前不会创建驱动程序(取决于我为并行方法或类创建的开关) . 所以在所有现实中,甚至都不应该尝试打开浏览器窗口 .

2 回答

  • 5

    试试 new SkipException("message"); 如果提供的条件不成立,这将跳过测试 .

  • 1

    在beforesuite注释方法中尝试以下代码,检查套件的运行模式是否为Y / N.如果那是N然后抛出异常

    throw new skipException(“所需消息”)

    请记住,不要在try和catch块中捕获跳过异常 . 否则它将在抛出跳过异常后在该套件中执行测试用例

    package com.qtpselenium.suiteA;
    
    import org.testng.SkipException;
    import org.testng.annotations.BeforeSuite;
    
    import com.qtpselenium.base.TestBase;
    import com.qtpselenium.util.TestUtil;
    
    public class TestSuiteBase extends TestBase{
    
    
        @BeforeSuite
        public void checksuiteskip(){ 
    
    
                //Initialize method of Test BASE Class to Initialize the logs and all the excel files
                try {
                    Initialize();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                 App_Logs.debug("checking run mode of SuiteA");
                if( !TestUtil.isSuiterunnable(suitexlsx, "suiteA")){
    
                   App_Logs.debug("Run mode for SuiteA is N");
                   throw new SkipException("Run mode for suiiteA is N");
    
    
               }else
    
                   App_Logs.debug("Run mode for SuiteA is Y");
    
    
            } 
    
    
        }
    

相关问题