如何在测试失败后停止 TestNG 运行

Tal*_*gel 5 java testng selenium

我正在尝试在 TestNG 中编写一个测试方法,在失败后 - 整个测试套件将停止运行。

@Test
public void stopTestingIfThisFailed() throws Exception
{
    someTestStesp();
    if (softAsserter.isOneFailed()) {
        asserter.fail("stopTestingIfThisFailed test Failed");
        throw new Exception("Test can't continue, fail here!");
    }
}
Run Code Online (Sandbox Code Playgroud)

正在引发异常,但其他测试方法正在运行。

怎么解决这个问题呢?

Tal*_*gel 0

我解决了这样的问题:在一次不能失败的测试失败之后 - 我正在将数据写入临时文本文件。

后来,在下一个测试中,我在 @BeforeClass 中添加了代码,用于检查前面提到的文本文件中的数据。如果发现一个阻止者,我将终止当前进程。

如果“不能”失败的测试实际上失败了:

 public static void saveShowStopper() {

    try {
        General.createFile("ShowStopper","tempShowStopper.txt");
    } catch (ParseException e) {
        e.printStackTrace();
    }
}
Run Code Online (Sandbox Code Playgroud)

@BeforeClass 验证代码:

@BeforeClass(alwaysRun = true)
public void beforeClass(ITestContext testContext, @Optional String step, @Optional String suiteLoopData,
        @Optional String group) throws Exception
{
    boolean wasShowStopperFound = APIUtils.loadShowStopper();
    if (wasShowStopperFound){
        Thread.currentThread().interrupt();
        return;
    }
}
Run Code Online (Sandbox Code Playgroud)