javanoob
javanoob

Reputation: 6410

testng - Invocationcount - stop test on first failure

I have a test with invocationcount set to 20 and would like the test to stop if the iteration 4 or 5 fails.

Is there any way for this? I just googled for the same but could not find any

Upvotes: 1

Views: 1992

Answers (1)

niharika_neo
niharika_neo

Reputation: 8531

You can make use of IInvokedMethodListener.

Override both methods for the interface. In afterInvocation, check the result and probably add to a map of Map<method, failureCount>

In beforeInvocation, check if failureCount > 4 then throw a SkipException, will cause the rest of the invocations to get skipped.

Something like :

static Map<String, Integer> methodFailCount = new HashMap<String, Integer>();
    public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {

        if(methodFailCount.get(method.getTestMethod().getMethodName())!=  null && methodFailCount.get(method.getTestMethod().getMethodName()) > 4)
            throw new SkipException("Skipped due to failure count > 4");
    }

    public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
        if(testResult.getStatus() == TestResult.FAILURE){
            if(methodFailCount.get(method.getTestMethod().getMethodName() ) == null)
                methodFailCount.put(method.getTestMethod().getMethodName(),1);
            else{
                methodFailCount.put(method.getTestMethod().getMethodName(),
                        methodFailCount.get(method.getTestMethod().getMethodName() )+1);
            }

        }   

    }

Upvotes: 2

Related Questions