Đorđe Zeljić
Đorđe Zeljić

Reputation: 1825

Stop Testng test from beforeInvocation method

Can I "stop" running test from beforeInvocation method of IInvokedMethodListener2?

I do some kind of soft skip with "throw new SkipException" in beforeInvocation, but I need to do "hard skip" - stop test without any trace of execution.

I first need to check some test method's annotations and then run or not run that specific test.

Thanks ...

Upvotes: 1

Views: 1338

Answers (1)

patrungel
patrungel

Reputation: 841

Throwing new SkipException in IInvokedMethodListener2.beforeInvocation() yields the same result (and report) as for a test depending on a failed test: test method makes into the report and is duly marked as skipped. So the test method does not get executed.

If it's not hard enough, I'd recommend test exclusion, not skipping. There is

BeanShell expression feature to run custom inclusions and exclusions.

Also, IAnnotationTransformer would allow you to disable (equivalent of "enabled=false" attribute) a test method at runtime, the simplest example being:

public class SkippingTransformer implements IAnnotationTransformer {
    @Override
    public void transform(ITestAnnotation ita, Class type, Constructor c, Method method) {
        if(method.getName().startsWith("skipMe")) {
            ita.setEnabled(false);
        }
    }
}

This will opt out test methods completely (that is, test count reduces). But you'll most likely need to take care of disabling chains of dependent methods. There may be other aspects to consider.

Upvotes: 1

Related Questions