8

假设我想从我的 IDE(Intellij IDEA 或 eclipse)手动运行 4000 个 JUnit 测试;前 1000 次测试运行得非常顺利(假设所有 1000 次测试都需要 3 分钟),但测试 1001 仅需要 30 多分钟。有没有办法我可以跳过测试 1001(当它仍在运行时)并让测试 1002(和其他测试)继续进行。我不想@Ignore 测试 1001 并重新运行套件,因为我已经有了测试 1-1000 的答案;我也不想选择测试 1001-4000,因为它需要太多时间。

我想要某种按钮 - 跳过当前测试 - 可以在测试运行时按下。

如果不存在这样的功能,IDE 开发人员或 JUnit 开发人员需要对其进行增强?

4

5 回答 5

15

这对于使用 JUnit 4 的Assume. Assume是一个像Assert. 不同的是,这Assert会使测试失败,而Assume会跳过它。

常见的用例是Assume.assumeTrue( isWindows() )仅适用于 Windows 文件系统的测试。

所以你可以做的是定义一个系统属性skipSlowTests并添加

Assume.assumeTrue( Boolean.getBoolean("skipSlowTests") )

在您通常想跳过的慢速测试开始时。创建一个定义属性的 Eclipse 启动配置,true您可以方便地在两者之间切换。

如果要运行慢速测试,请选择 Eclipse(或整个类)中的方法,然后从上下文菜单中使用“Run as JUnit Test”。由于该属性是false默认设置,因此将运行测试。

于 2013-07-02T12:08:54.170 回答
9

不,如果测试已经在运行,则不能跳过测试。

我建议您Categories将慢速测试与其他测试分开。

例如:

public interface SlowTests {
}

public class MyTest {

     @Test
     public void test1{
     }

     @Category(SlowTests.class)
     @Test
     public void test1001{
         // this is a slow test
     }
}

为快速测试创建一个测试套件。

@RunWith(Categories.class)
@ExcludeCategory(SlowTests.class)
@SuiteClasses(MyTest.class)
public class FastTestSuite {

}

现在执行FastTestSuite如果您不想运行慢速测试(例如 test1001)。MyTest如果要运行所有测试,请照常执行。

于 2012-05-02T08:44:06.797 回答
4

What you're asking for is to stop executing your code while it is in mid test. You can't stop executing a current test without having hooks in your code to allow it. Your best solution is to use Categories as others have suggested.

Basically, JUnit executes all of the @Before methods (including @Rules), then your @Test method, then the @After methods (again, including @Rules). Even assuming that JUnit had a mechanism for stopping execution of it's bits of the code (which it doesn't), most of the time is spent in your code. So to 'skip' a test which has already started requires you to modify your test code (and potentially the code that it's testing) in order that you can cleanly stop it. Cleanly stopping an executing thread is a question in itself [*].

So what are your options?

  1. Run the tests in parallel, then you don't have to wait as long for the tests to finish. This may work, but parallelizing the tests may well be a lot of work.

  2. Stop execution of the tests, and fix the one that's you're working on. Most IDEs have an option to kill the JVM in which the tests are running. This is definitely the easiest option.

  3. Implement your own test runner, which runs the test in a separate thread. This test runner then either waits for the thread to finish executing, or checks a flag somewhere which would be a signal for it to stop. This sounds complicated, because you need t manage your threads but also to set the flag in a running jvm. Maybe creating a file somewhere? This runner would then fail the currently running test, and you could move on to the next. Please note that 'stopping' a test midway may leave stuff in an inconsistent state, or you may end up executing stuff in parallel.

There are parallel JUnit runners out there, and I don't think you're going to get much help from IDE developers (at least in the short term). Also, look at TestNG, which allows stuff to be run in parallel.

For using categories, one solution I use is to run the long running tests separately using maven surefire or similar, not through the IDE. This involves checking out the source code somewhere else on my machine and building there.

[*]: Java, how to stop threads, Regarding stopping of a thread

于 2012-05-02T09:41:48.130 回答
1

我认为更常见的解决方案是拥有两个测试套件:一个用于快速测试,另一个用于慢速测试。这通常是您划分单元测试(快速)和集成测试(慢速)的方式。

对于这样的事情,您不太可能对 JUnit 或 IntelliJ 进行修改。最好改变你使用它们的方式——它会让你更快地得到答案。

于 2012-05-02T09:22:52.970 回答
-1

您可以修改您的主题并执行类似的操作

public void theTest(){
    if (System.getProperty("skipMyTest") == null){
         //execute the test
    }
}

如果要跳过测试,请传递环境变量

于 2012-05-02T08:20:02.217 回答