TestNG - 如何在BeforeSuite注释中满足条件时强制结束整个测试套件

7
如果在@BeforeSuite注释中满足条件,有一种方法可以退出整个测试套件吗?也许有一种调用@AfterSuite并绕过整个测试的方法?
我在@BeforeSuite中进行数据库调用。如果查询返回任何结果,我会发送电子邮件,现在想要杀死整个测试套件。
我已经尝试使用System.exit(1);和org.testng.Assert.fail("There are unpaid invoices");,但是两者都无法终止整个套件。我的脚本设置为并行运行类,当我从test.xml文件运行测试时,每个类都会尝试启动并打开一个窗口,然后立即关闭它。
FYI,驱动程序不会在@BeforeClass或@BeforeMethod(取决于我为并行方法或类创建的开关)之前创建。因此,实际上甚至不应该尝试打开浏览器窗口。

1
尝试使用新的SkipException("message");,如果提供的条件不为真,则会跳过测试。 - Shek
没错!我实际上在另一个测试中使用过它,然后忘记了。谢谢。 - Dustin N.
2个回答

8

尝试使用new SkipException(“message”);如果提供的条件不为真,则会跳过测试。


它似乎也跳过了@afterSuite(alwaysRun = true)方法,这是不希望的。 - George Barbosa

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

throw new skipException("desired Message")

请记住,在try和catch块中不要捕获skip异常。否则,抛出skip异常后会执行该套件中的测试用例。
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");


        } 


    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接