忽略 testng 中的一个类 [英] Ignore a class in testng

查看:28
本文介绍了忽略 testng 中的一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行我的 testng 套件时,有一些测试类需要被忽略.我尝试对需要忽略的类和方法使用 @Test(enabled=false) 注释.但我的问题是,需要忽略的类扩展了一个抽象类,即使我在基类上有 @Test(enabled=false) 注释,也不会忽略这个抽象类测试方法.在 Junit 中,我可以在基类上使用 @ignore,并且根本不会调用扩展类上的测试方法.如何在 testng 中复制此行为.

There are some test classes which needs to be ignored when I run my testng suite. I tried using the @Test(enabled=false) annotation for the class and methods that needs to be ignored. But my problem is that the class that needs to be ignored extends an abstract class and this abstract class test methods are not ignored even when I have @Test(enabled=false) annotation on the base class. In Junit I could use @ignore on the base class and the test methods on the extended class would not be invoked at all. How can I replicate this behaviour in testng.

也在我的 testng 套件中,我按包而不是按类运行测试.因此,即使我尝试将班级分组并忽略该组,它也不起作用.<代码><测试名称="测试"><组><运行><exclude name="testClass"/></运行></组>
<包>

Also In my testng suite I run the test by packages and not by classes. Hence even if I try to group the class and ignore the group it is not working either. <test name="Test"> <groups> <run> <exclude name="testClass"/> </run> </groups>
<packages>

请帮忙

推荐答案

我从谷歌组 (Testng) 收到了 Cedric 对此的回复.我把它贴在下面以供参考

I received a response from Cedric on this from the google groups(Testng) . I am pasting it below for reference

保罗,

您所看到的是在 TestNG 中注释继承和默认属性相互交互的方式的不幸后果,并且已经提交了一个错误以使其更好地工作(我没有手头的 id).

What you are seeing is an unfortunate consequence of the way inheritance of annotations and default attributes interact with each other in TestNG, and a bug has been filed to make it work better (I don't have the id handy).

考虑:

@Test(enabled = false)
public class C {
  @Test
  public void f()
}

当TestNG解析这些注解时,发现方法上的@Test没有指定启用",所以给它赋了默认值.示例变为:

When TestNG resolves these annotations, it finds that the @Test on the method doesn't specify "enabled", so it assigns it its default value. The example becomes:

@Test(enabled = false)
public class C {
  @Test(enabled = true)
  public void f()
}

现在,在方法级别定义的值会覆盖在类中指定的值.

And now, the value defined at the method level overrides the one specified on the class.

修复将涉及使用枚举而不是布尔值(启用、禁用、继承,这将是默认值),但这会破坏向后兼容性,因此我需要为此引入一个新注释(例如runStatus" 或类似的东西),你的例子就会变成

The fix would involve using an enum instead of a boolean (ENABLED, DISABLED, INHERITED, which would be the default), but this would break backward compatibility, so I would need to introduce a new annotation for this (e.g. "runStatus" or something like that), and your example would become

@Test(runStatus = DISABLED)
public class C {
  @Test
  public void f()
}

这有意义吗?

--塞德里克

这篇关于忽略 testng 中的一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆