我正在尝试用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!");
}
}
异常正在被抛出,但其他测试方法正在运行。
如何解决这个问题?
发布于 2019-05-28 17:21:27
我这样解决了这个问题:在一个不能失败的测试失败后,我将数据写入一个临时文本文件。
后来,在下一个测试中,我在@BeforeClass中添加了检查前面提到的文本文件中的数据的代码。如果找到了显示停止程序,我将终止当前进程。
如果“不能”的测试实际上失败了:
public static void saveShowStopper() {
try {
General.createFile("ShowStopper","tempShowStopper.txt");
} catch (ParseException e) {
e.printStackTrace();
}
}
@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;
}
}
发布于 2019-05-21 04:42:47
这取决于您的期望(TestNG中没有对此的直接支持)。您可以创建在@Test
中抛出的ShowStopperException
,然后在您的ITestListener
实现(see docs)中,当您在result中发现此异常时,您可以调用System.exit(1 (or whatever number))
,但是将不会有报告,通常这不是一个好的实践。第二种选择是有一些基类,它是所有测试类的父类,以及一些上下文变量,它将在父类中处理@BeforeMethod
中的ShowStopperException
,并抛出SkipException
,因此工作流程可以如下所示:
test passed
test passed
showstopper exception in some test
test skipped
test skipped
test skipped
...
发布于 2019-05-20 20:21:42
如果您从@BeforeSuite
setup方法抛出特定的异常SkipException
,它就会执行此操作。
请参阅(可能是dupe) TestNG - How to force end the entire test suite from the BeforeSuite annotation if a condition is met
如果你想从一个任意的测试中做到这一点,似乎没有一个框架机制。但是您可以随时翻转一个标志,并在@BeforeTest
设置方法中检查该标志。在你跳到那之前,如果你可以在整个套件运行之前检查一次,然后在那里中止(即@BeforeSuite
),可能需要考虑一下。
https://stackoverflow.com/questions/56220735
复制相似问题