使用TestSuite时,我可以避免在eclipse中运行两次junit测试吗?

lex*_*ope 16 java eclipse junit junit4

我需要进行一些套件初始化(启动Web服务器).它运行正常,除了当我在eclipse中运行我的项目中的所有测试时,我的测试运行了两次.我的测试套件看起来有点像这样:

@RunWith(Suite.class)
@Suite.SuiteClasses({
   SubtestOne.class,
   SubtestTwo.class
})
public class TestSuite
{
   [...]
}

public class SubtestOne
{
   @Test public void testOne() { [...] }
}

public class SubtestTwo
{
   @Test public void testTwo() { [...] }
}
Run Code Online (Sandbox Code Playgroud)

当我在eclipse中运行项目中的所有测试时,这导致junit插件运行测试两次,如下所示:

  • SubtestOne
  • SubtestTwo
  • 的TestSuite
    • SubtestOne
    • SubtestTwo

是否可以使"在项目中运行所有测试"不进行两次子测试?我希望我的子测试只能作为套件的一部分运行.

use*_*890 5

I realize that this has been asked over 5 years ago, but as quite a few folks up-voted the question I thought I'd still chime in with a solution. Skip right to the end if you just want the solution; read the whole text if you also want to understand it ;-)

First of all, it is indeed possible to ensure that a particular JUnit test class gets only run inside a test suite. Also, it is irrelevant whether you want to run that test suite inside Eclipse (as asked here) or any other tool or environment; this is really a pure JUnit issue for the most part.

在我勾勒出解决方案之前,最好重新审视一下这里的确切问题是什么。所有 JUnit 测试都需要可见且可实例化,以便由 JUnit 框架及其各种运行程序拾取。这也适用于测试套件和测试套件中的各个测试。因此,如果 JUnit 选择测试套件,它也会选择单独的测试,并且套件中的所有测试将执行两次,一次单独执行,一次作为套件的一部分执行。

因此,如果您愿意的话,诀窍是防止 JUnit 获取各个测试,同时仍然能够将它们作为套件的一部分进行实例化和执行。

我想到的一件事是使测试类静态内部类嵌套在测试套件中。但是,嵌套类仍然需要是公共的(否则它们也不能在套件中运行),如果它们是公共类,尽管嵌套在套件的公共类中,它们也将被单独选取。不过,JUnit 不会尝试运行不可见的测试类。因此,将测试类嵌套在非公共类中可能足以隐藏它们,但我们不能将套件类设置为非公共类,因为这样 JUnit 就不会执行它。然而,我们可以做的是将各个测试嵌套在另一个嵌套在测试套件中的非公共类中,这使我们找到了这个难题的解决方案:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({AllTests.InSuiteOnly.Test1.class, AllTests.InSuiteOnly.Test2.class})
public class AllTests
{
    static class InSuiteOnly
    {
        public static class Test1
        {
            @Test
            public void test1()
            {
                //...
            }
        }

        public static class Test2
        {
            @Test
            public void test2()
            {
                //...
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

许多人可能会反对现在需要在单个源文件中进行所有测试。如果我想维护单独的 JUnit 测试类,这些测试类不会自行执行,但仍会在测试套件中执行,该怎么办?一个简单的解决方案是使各个测试类抽象(公共/非公共无关紧要),以便 JUnit 不会执行它们,并且在测试套件中我们只需使用原始抽象测试类的具体子类:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({AllTests.InSuiteOnly.SuiteTest1.class, AllTests.InSuiteOnly.SuiteTest2.class})
public class AllTests
{
    static class InSuiteOnly
    {
        public static class SuiteTest1 extends Test1 {}
        public static class SuiteTest2 extends Test2 {}
    }
}

abstract class Test1
{
    @Test
    public void test1()
    {
        //...
    }
}

abstract class Test2
{
    @Test
    public void test2()
    {
        //...
    }
}
Run Code Online (Sandbox Code Playgroud)

该方案适用于 Maven、Eclipse 和所有其他环境,这些环境要么直接利用 JUnit 的运行程序,要么实现自己的运行程序,严格遵循 JUnit 的原始行为和语义。


Kai*_*Kai 4

不,测试类将始终直接启动,然后通过套件中的“链接”启动。这正如预期的那样。

一种解决方法可能是在运行配置中设置为仅运行包含套件的包中的测试。打开运行配置并选择Run all tests in the selected project, package or source folder然后单击Search...并选择包。