testng - 如何从 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);
}
}
}
}
解决方案
如果您想执行测试并将其从结果报告中隐藏,您可以查看 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 侦听器。这些也很有用。
推荐阅读
- python-3.x - 修复 'ImmutableDenseNDimArray' 对象没有属性 'could_extract_minus_sign'
- kubernetes - 在 GKE 上为 nginx 入口使用静态 IP 时,通过 helm 安装的 Jenkins 未公开
- php - 让 Apache 通过 PHP FPM 传递 JS 文件
- ios - 对 Firebase/Crashlytics 的多个应用使用相同的谷歌应用 ID
- r - 主成分分析循环不起作用
- python - 如何将脚本中的函数作为包从同级文件夹导入
- python - 在 Python 中通过“ElementTree”解析 XML
- python - 有没有办法使用 Flask Socket IO 登录用户?
- angular - Angular 反应式表单根据其他两个输入验证输入
- r - 将数据框转换为结构化列表