0

Background: I am using TestNG DataProvider

Requirement: Need to eliminate 1 test from the TestNG report after execution is finished.

My Solution: Suppose, I need to remove the 'XYZ' test case from the report.

String testName = "XYZ";

private void removeTestFromResult(ITestContext context)
    {
        for (ITestNGMethod testMethodName : context.getAllTestMethods())
        {
            String testMethod = testMethodName.getMethodName().toLowerCase();

            if (testMethod.contains(testName))
            {
                if (context.getPassedTests().size() > 0)
                {
                    context.getPassedTests().removeResult(testMethodName);
                }
                if (context.getFailedTests().size() > 0)
                {
                    context.getFailedTests().removeResult(testMethodName);
                }
                if (context.getSkippedTests().size() > 0)
                {
                    context.getSkippedTests().removeResult(testMethodName);
                }
            }
        }
    }

2 Answers2

1

If you want to execute the test and hide it from the result report you can take a look at the TestNG TestListenerAdapter. It is possible to modify the test context after execution e.g. removing unwanted tests from it. See this link for a detailed example.

TestListenerAdapter:

public class MyTestListenerAdapter extends TestListenerAdapter {

    @Override
    public void onFinish(ITestContext context) {
        //TODO remove the unwanted tests from context.getPassedTests()
    }
}

Test:

@Listeners(MyTestListenerAdapter.class)
public class MyTest {
    //Your test methods here.
}

Here are all TestNG listeners documented. Those can be useful as well.

AndiCover
  • 1,724
  • 3
  • 17
  • 38
0

In the @Test annotation you can set enabled=false to avoid their execution:

https://testng.org/doc/documentation-main.html#annotations