首页 > 解决方案 > 如何从 TestNG 结果或 ITestContext 中删除任何测试

问题描述

背景: 我正在使用 TestNG DataProvider

要求: 执行完成后需要从TestNG报告中剔除1个测试。

我的解决方案: 假设我需要从报告中删除“XYZ”测试用例。

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);
                }
            }
        }
    }

标签: testngtestng-dataprovidertestng-annotation-test

解决方案


如果您想执行测试并将其从结果报告中隐藏,您可以查看 TestNG TestListenerAdapter。可以在执行后修改测试上下文,例如从中删除不需要的测试。有关详细示例,请参阅此链接

测试监听适配器:

public class MyTestListenerAdapter extends TestListenerAdapter {

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

测试:

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

以下是记录的所有 TestNG 侦听器。这些也很有用。


推荐阅读