Sometimes you need to disable/skip some of your TestNG tests based on condition. You cannot use @Test(enabled = false) because you need to check the condition runtime.
Of course you can check the condition in your test just like this
Doesn’t look good, right? What if we could skip test based on a condition like this using annotations
Looks much more elegant, doesn’t it?
Using IInvokedMethodListener for conditional skipping TestNG tests
We can use that approach if we implement TestNG’s IInvokedMethodListener.
Here is how to do that.
You can use as many conditions for skipping TestNG tests as you need, just create annotations for each condition and check those conditions in IInvokedMethodListener’s beforeInvocation method.
Annotations for conditional skipping tests
Using annotations for conditional skipping tests
Here is an example how to use annotations for conditional skipping TestNG tests. You can even combine conditions - see test5()
Compatible with Spring Boot
That approach for conditional skipping tests also works if you use TestNG and Spring Boot together