How to disable TestNG test based on a condition

前端 未结 7 1338
挽巷
挽巷 2020-12-05 07:23

Is there currently a way to disable TestNG test based on a condition

I know you can currently disable test as so in TestNG:

@Test(en         


        
7条回答
  •  再見小時候
    2020-12-05 08:03

    I prefer this annotation based way for disable/skip some tests based on environment settings. Easy to maintain and not requires any special coding technique.

    • Using the IInvokedMethodListener interface
    • Create a custom anntotation e.g.: @SkipInHeadlessMode
    • Throw SkipException
    public class ConditionalSkipTestAnalyzer implements IInvokedMethodListener {
        protected static PropertiesHandler properties = new PropertiesHandler();
    
        @Override
        public void beforeInvocation(IInvokedMethod invokedMethod, ITestResult result) {
            Method method = result.getMethod().getConstructorOrMethod().getMethod();
            if (method == null) {
                return;
            }
            if (method.isAnnotationPresent(SkipInHeadlessMode.class)
                    && properties.isHeadlessMode()) {
                throw new SkipException("These Tests shouldn't be run in HEADLESS mode!");
            }
        }
    
        @Override
        public void afterInvocation(IInvokedMethod iInvokedMethod, ITestResult iTestResult) {
            //Auto generated
        }
    }
    

    Check for the details: https://www.lenar.io/skip-testng-tests-based-condition-using-iinvokedmethodlistener/

提交回复
热议问题