The annotations are set before runtime, so once you are running the test, you cannot change the invocationCount.
What you can do, is to use a listener to stop the execution when it reaches onTestFailure
.
So let's say you have the test class:
import org.testng.ITestContext;
import org.testng.annotations.Test;
import org.testng.asserts.Assertion;
import org.testng.annotations.Listeners;
@Listeners(TestListener.class)
public class CreateBookingTest extends TestConfig {
@Test(invocationCount = 20)
public void stopInvocationsTest(ITestContext testContext) {
int currentCount = testContext.getAllTestMethods()[0].getCurrentInvocationCount();
// We will force a failure in the invocation 5 (the 6th run).
if (currentCount==5) {
new Assertion().fail("Method failed in invocation " + currentCount);
}
}
}
Then the listener will abort th execution if there's a single failure and stop from running the rest of the tests:
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
import org.testng.asserts.Assertion;
public class TestListener implements ITestListener {
@Override
public void onTestStart(ITestResult result) {
System.out.println("\nSTARTING: " + result.getMethod().getCurrentInvocationCount());
}
@Override
public void onTestSuccess(ITestResult result) {
System.out.println("PASSED: " + result.getMethod().getCurrentInvocationCount());
}
@Override
public void onTestFailure(ITestResult result) {
System.out.println("FAILED: " + result.getMethod().getCurrentInvocationCount());
new Assertion().fail("Aborting");
}
@Override
public void onTestSkipped(ITestResult result) {
}
@Override
public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
}
@Override
public void onStart(ITestContext context) {
}
@Override
public void onFinish(ITestContext context) {
}
}