Reputation: 1772
I am trying to write a test method in TestNG, that after it fails - the entire test suite will stop running.
@Test
public void stopTestingIfThisFailed() throws Exception
{
someTestStesp();
if (softAsserter.isOneFailed()) {
asserter.fail("stopTestingIfThisFailed test Failed");
throw new Exception("Test can't continue, fail here!");
}
}
The exception is being thrown, but other test methods are running.
How to solve this?
Upvotes: 5
Views: 11137
Reputation: 1772
I solved the problem like this: after a test that mustn't fail fails - I'm writing data to a temporary text file.
Later, in the next test I added code in the @BeforeClass that checks that data in the former mentioned text file. If a show stopper was found I'm killing the current process.
If a test the "can't" fail actually fails:
public static void saveShowStopper() {
try {
General.createFile("ShowStopper","tempShowStopper.txt");
} catch (ParseException e) {
e.printStackTrace();
}
}
The @BeforeClass validating code:
@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;
}
}
Upvotes: 0
Reputation: 1036
It depends on what you expect (there is no direct support for this in TestNG). You can create ShowStopperException
which is thrown in @Test
and then in your ITestListener
implementation (see docs) you can call System.exit(1 (or whatever number))
when you find this exeption in result but there will be no report and in general it's not good practice. Second option is to have some base class which is parent of all test classes and some context variable which will handle ShowStopperException
in @BeforeMethod
in parent class and throw SkipException
so workflow can be like:
test passed
test passed
showstopper exception in some test
test skipped
test skipped
test skipped
...
Upvotes: 3
Reputation: 864
It behaves if you throw a specific exception, SkipException
, from the @BeforeSuite
setup method.
See (possible dupe) TestNG - How to force end the entire test suite from the BeforeSuite annotation if a condition is met
If you want to do it from an arbitrary test, it doesn't appear there is a framework mechanism. But you could always flip a flag, and check that flag in the @BeforeTest
setup method. Before you jump to that, maybe have a think if you could check once before the whole suite runs, and just abort there (ie @BeforeSuite
).
Upvotes: -1
Reputation: 10964
You can use the dependsOnMethods
or dependsOnGroups
annotation parameter in your other test methods:
@Test(dependsOnMethods = {"stopTestingIfThisFailed"})
public void testAnotherTestMehtod() {
}
JavaDoc of the dependsOnMethods
parameter:
The list of methods this method depends on. There is no guarantee on the order on which the methods depended upon will be run, but you are guaranteed that all these methods will be run before the test method that contains this annotation is run. Furthermore, if any of these methods was not a SUCCESS, this test method will not be run and will be flagged as a SKIP. If some of these methods have been overloaded, all the overloaded versions will be run.
See https://testng.org/doc/documentation-main.html#dependent-methods
Upvotes: 3