java 如何在 TestNG 中跳过或忽略测试的执行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33476985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:43:40  来源:igfitidea点击:

How to skip or ignore execution of tests in TestNG

javatestng

提问by Galet

I am writing test in java using TestNG.

我正在使用 TestNG 在 Java 中编写测试。

I want to skip or ignore a all class methods using conditional inside the class file.

我想跳过或忽略在类文件中使用条件的所有类方法。

In ruby, I have followed this How to skip certain tests with Test::Unit

在 ruby​​ 中,我遵循了如何使用 Test::Unit 跳过某些测试

How can I do in java?

我该怎么做?

回答by hasnae

You can ignore test method using the annotation @Test(enabled = false)

您可以使用注释@Test(enabled = false) 忽略测试方法

回答by Raffaele

You can throw SkipException's in your tests if assumptions does not hold, and that mechanism is flexible enough to even mark the test as skipped or failed. This articleshows how to integrate this approach in a declarative manner in your test suite. Basically you annotate methods with @Assumesand have a custom IInvokedMethodListener.

SkipException如果假设不成立,您可以在测试中抛出's,并且该机制足够灵活,甚至可以将测试标记为跳过或失败。本文展示了如何以声明方式将这种方法集成到您的测试套件中。基本上你用注释方法@Assumes并有一个自定义的IInvokedMethodListener.

Or (I don't encourage this but it's an option), if you can determine what to skip statically, you may generate an XML spec on the fly and run it.

或者(我不鼓励这样做,但这是一个选项),如果您可以确定静态跳过哪些内容,则可以动态生成 XML 规范并运行它。

回答by juherr

Another solution is to use an IAnnotationTransformersand then disable the test like you can do with @Test(enabled = false).

另一种解决方案是使用IAnnotationTransformers,然后像使用@Test(enabled = false).

public void transform(ITest annotation, Class testClass,
      Constructor testConstructor, Method testMethod) {
    if (...determine if the test should be disabled) {
        annotation.setEnable(false);
    }
}