0

I have seen many answers on how to skip the whole @Test, but I am trying to skip a specific iteration without having to adding extra metadata to dozens of existing tests:

public class TestNgPlayground {

        public static final DayOfWeek SOME_CONST = DayOfWeek.TUESDAY;

        @DataProvider
        public Object[][] getStuff(){
            return new Object[][] {
                    { "param1", DayOfWeek.MONDAY},    // Skip this
                    { "param2", DayOfWeek.TUESDAY },  // Run this
                    { "param3", DayOfWeek.WEDNESDAY } // Skip this
            };
        }

        // Ideally I want to detect and Skip in @BeforeMethod / @BeforeClass

        @BeforeMethod  // testArgs fetches the DataProvider values
        public void setUp(Object[] testArgs,ITestResult result ){ 

            DayOfWeek dataProviderValue = (DayOfWeek) testArgs[1];

            if(dataProviderValue!= SOME_CONST ) {
    //            throw new SkipException("skip test"); // No good - skips the entire Test
    //            result.setStatus(ITestResult.SKIP);   // Doesn't do anything
            }
        }


        @Test(dataProvider = "getStuff")
        public void testt(String param1, DayOfWeek param2){
            // some testing
        }

        // 2nd best option - overwrite to Skip after test has run, 
        // though time has been wasted
        @AfterMethod
        public void tearDown(ITestResult result){

            DayOfWeek dow = (DayOfWeek) result.getParameters()[1];

            if(dow != SOME_CONST){
                result.setStatus(ITestResult.SKIP); // Doesn't do anything either... ?
            }


        }
    }

For the above example, I want the final report to show:

Iteration 1 - Skipped

Iteration 2 - Passed

Iteration 3 - Skipped

Andrejs
  • 10,803
  • 4
  • 43
  • 48

1 Answers1

1

You can do this very easily by leveraging the IHookable interface that TestNG provides. Now within the run() method, you would get the ability to decide as to what you want to do with a particular iteration and even alter the status accordingly.

Here's a sample that shows how to do this.

import org.testng.IHookCallBack;
import org.testng.IHookable;
import org.testng.ITestResult;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.time.DayOfWeek;

public class TestNgPlayground implements IHookable {

    private static final DayOfWeek SOME_CONST = DayOfWeek.TUESDAY;

    @DataProvider
    public Object[][] getStuff() {
        return new Object[][]{
                {"param1", DayOfWeek.MONDAY},    // Skip this
                {"param2", DayOfWeek.TUESDAY},  // Run this
                {"param3", DayOfWeek.WEDNESDAY} // Skip this
        };
    }

    @Test(dataProvider = "getStuff")
    public void testt(String param1, DayOfWeek param2) {
        System.err.println("[" + param1 + ", " + param2 + "]");
    }

    @Override
    public void run(IHookCallBack callBack, ITestResult testResult) {
        Object[] parameters = callBack.getParameters();
        DayOfWeek dataProviderValue = (DayOfWeek) parameters[1];
        if (dataProviderValue != SOME_CONST) {
            callBack.runTestMethod(testResult);
        } else {
            testResult.setStatus(ITestResult.SKIP);
        }
    }
}

Here's the output:

[param1, MONDAY]
[param3, WEDNESDAY]

Test ignored.

===============================================
Default Suite
Total tests run: 3, Failures: 0, Skips: 1
===============================================
Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66
  • I've literally copy pasted your entire example into my Intellij, even ran in debug mode to ensure `testResult.setStatus(ITestResult.SKIP);` is hit, yet the output is still that all 3 tests are run (no skips). Any idea what the discrepancy might be? – Andrejs Oct 28 '17 at 10:33
  • What version of TestNG are you using. Can you please try with 6.12 ( as of today this is the latest released version ) – Krishnan Mahadevan Oct 28 '17 at 10:34
  • Magic! Was on 6.9; Moved to 6.11 and it works. Thank you. Where do you see 6.12, given that -> https://mvnrepository.com/artifact/org.testng/testng shows 6.11 only? – Andrejs Oct 28 '17 at 10:39
  • 1
    TestNG 6.12 hasn't made it to Maven central yet. It's available in JCenter though. You merely need to add a repository entry to your pom file to also refer to JCenter. Please see [here](https://stackoverflow.com/a/39164063) for more details. – Krishnan Mahadevan Oct 28 '17 at 10:44
  • Need to actually throw a SkipException for it to be seen as skipped – javydreamercsw Jul 08 '19 at 15:47