java jUnit 忽略来自基类的 @Test 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2207070/
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-10-29 20:02:54  来源:igfitidea点击:

jUnit ignore @Test methods from base class

javainheritancejunit

提问by mgamer

Let's say I have a test class called testFixtureAwith several methods testA, testB, testC, etc, each with @Testannotation.

比方说,我有一个名为测试类testFixtureA有几种方法testAtestBtestC,等,每个@Test注释。

Let's now say I subclass testFixtureAinto class called testFixtureABand I don't overwrite anything. testFixtureABis empty as for now.

现在假设我将子类testFixtureA化为称为类的子类,testFixtureAB并且不覆盖任何内容。testFixtureAB现在是空的。

When I run tests from testFixtureAB, methods testA, testBand testCare executed by test runner because test runner doesn't distinguish between test methods from class and baseclass.

当我从运行测试testFixtureAB,方法testAtestB以及testC因为测试运行不从类和基类的测试方法区分是通过测试运行执行。

How can I force test runner to leave out tests from baseclass?

如何强制测试运行程序从基类中删除测试?

回答by Matthew Flynn

and I don't overwrite anything. testFixtureAB is empty as for now

我不会覆盖任何东西。testFixtureAB 目前为空

There's your answer. If you want to not run testB from the main class, overrride it:

这就是你的答案。如果您不想从主类运行 testB,请覆盖它:

public class testFixtureAB extends testFixtureA {
   @Override
   public void testB() {}
}

回答by Bozho

Restructure your test classes.

重组你的测试类。

  • If you don't want to use the tests from the baseclass, then don't extend it
  • If you need other functionality from the base class, split that class in two - the tests, and the other functionality
  • 如果您不想使用基类中的测试,则不要扩展它
  • 如果您需要基类的其他功能,请将该类分为两部分 - 测试和其他功能

回答by dfa

ignoring the whole base class:

忽略整个基类:

@Ignore
class BaseClass {
   // ...
}

check out this example

看看这个例子

回答by user1607938

It's quite easy to achieve implementing some few classes:

实现一些类很容易实现:

  • Create your own TestRunner
  • Create an annotation like @IgnoreInheritedTests
  • Create a class that extends org.junit.runner.manipulation.Filter
  • 创建自己的 TestRunner
  • 创建一个注释,如 @IgnoreInheritedTests
  • 创建一个扩展的类 org.junit.runner.manipulation.Filter

On the filter class:

在过滤器类上:

public class InheritedTestsFilter extends Filter {

    @Override
    public boolean shouldRun(Description description) {
        Class<?> clazz = description.getTestClass();
        String methodName = description.getMethodName();
        if (clazz.isAnnotationPresent(IgnoreInheritedTests.class)) {
            try {
                return clazz.getDeclaredMethod(methodName) != null;
            } catch (Exception e) {
                return false;
            }
        }
        return true;
    }

    @Override
    public String describe() {
        // TODO Auto-generated method stub
        return null;
    }

}

on your custom runner:

在您的自定义跑步者上:

 /**
 * @param klass
 * @throws InitializationError
 * @since
 */
public CustomBaseRunner(Class<?> klass) throws InitializationError {
    super(klass);
    try {
        this.filter(new InheritedTestsFilter());
    } catch (NoTestsRemainException e) {
        throw new IllegalStateException("class should contain at least one runnable test", e);
    }
}

回答by topchef

I know, it's not the answer...

我知道,这不是答案......

Consider the reason why you extend concrete test classes. You do duplicate test methods that way.

考虑扩展具体测试类的原因。您以这种方式重复测试方法。

If you share code between tests then consider writing base test classes with helper and fixture setup methodsor test helper class.

如果您在测试之间共享代码,请考虑使用帮助器和夹具设置方法测试帮助器类编写基础测试

If for running tests then try organizing tests with suites and categories.

如果要运行测试,请尝试使用套件和类别组织测试。

回答by martosoler

What if you want to execute the same test for different configurations of the same test suite?

如果您想对同一测试套件的不同配置执行相同的测试怎么办?

For example, let's say you have Class A with test1, test2 and test3 methods that hit an embedded database then you want to create separated "setUp" and "tearDown" for every embedded vendor (H2, HyperSQL, etc) but runs the same testing for each one.

例如,假设您有一个带有 test1、test2 和 test3 方法的 A 类访问嵌入式数据库,然后您想为每个嵌入式供应商(H2、HyperSQL 等)创建单独的“setUp”和“tearDown”,但运行相同的测试每一个人。

I would like to extend a class that contain those test methods and configure it in a subclass. My problem is that the super class SHOULD NOT be considered as eligible for the test runner. The problem arises when the test runner executes the super class and given that don't found the corresponding setup and teardown methods, it crashs :(

我想扩展一个包含这些测试方法的类并在子类中配置它。我的问题是超类不应该被视为符合测试运行器的条件。当测试运行器执行超类时出现问题,并且由于没有找到相应的设置和拆卸方法,它会崩溃:(

回答by Yishai

In the latest JUnit you can use the @Rule annotation on the subclass to inspect the test name and intercept the test run to ignore the test dynamically. But I would suggest that @Bozho's idea is the better one - the fact that you need to do this indicates a bigger problem that probably shows inheritance is not the right solution here.

在最新的 JUnit 中,您可以在子类上使用 @Rule 注释来检查测试名称并拦截测试运行以动态忽略测试。但我建议@Bozho 的想法更好 - 您需要这样做的事实表明一个更大的问题可能表明继承在这里不是正确的解决方案。

回答by Mark Mosher

In the base test class' @Test methods:

在基础测试类的@Test 方法中:

assumeTrue(getClass().equals(BaseClassTest.class));

It will ignore those in the subclass tests but not completely leave them out.

它将忽略子类测试中的那些,但不会完全忽略它们。

回答by Ermal

If for any reason you need two JUnit classes for same functionality, the best approach for me is:

如果出于任何原因您需要两个 JUnit 类来实现相同的功能,对我来说最好的方法是:

  • put the common code in a parent class TestFixturewith only constants and mocked services.
  • create two subclasses: TestFixtureAand TestFixtureB
  • 将公共代码放在TestFixture只有常量和模拟服务的父类中。
  • 创建两个子类:TestFixtureATestFixtureB

This way you will not have duplicated code, nor double runs.

这样你就不会有重复的代码,也不会重复运行。