简体   繁体   English

我可以在测试运行时跳过JUnit测试吗?

[英]Can I skip a JUnit test while the test is running?

Suppose I want to manually run from my IDE (Intellij IDEA, or eclipse) 4000 JUnit tests; 假设我想从我的IDE(Intellij IDEA或eclipse)4000 JUnit测试手动运行; the first 1000 tests run pretty smoothly (say they take 3 minutes all 1000) but the test 1001 takes alone over 30 minutes. 前1000次测试运行非常顺利(比如他们需要3分钟全部1000次),但测试1001单独使用超过30分钟。 Is there a way I can skip the test 1001 (while it's still running) and to let the test 1002 (and the others) keep going. 有没有办法可以跳过测试1001(当它仍在运行时)并让测试1002(和其他人)继续前进。 I do not want to @Ignore the test 1001 and rerun the suite because I already have the answer for tests 1-1000; 我不想@Ignore测试1001并重新运行套件,因为我已经有了测试1-1000的答案; also I do not want to select tests 1001-4000 because it takes too much time. 我也不想选择1001-4000测试,因为它需要太多时间。

I would some kind of button - Skip Current Test - which can be pressed when the test is running. 我会使用某种按钮 - 跳过电流测试 - 可以在测试运行时按下。

In case such feature does not exist, an enhancement for it needs to be done by the IDE developers or by JUnit developers? 如果不存在此类功能,IDE开发人员或JUnit开发人员需要对其进行增强吗?

This is actually pretty simple with JUnit 4 using Assume . 使用Assume ,JUnit 4实际上非常简单。 Assume is a helper class like Assert . Assume是一个像Assert这样的辅助类。 The difference is that Assert will make the test fail while Assume will skip it. 区别在于Assert会使测试失败而Assume会跳过它。

The common use case is Assume.assumeTrue( isWindows() ) for tests that only work on, say, a Windows file system. 常见的用例是Assume.assumeTrue( isWindows() )用于仅适用于Windows文件系统的测试。

So what you can do is define a system property skipSlowTests and add 所以你可以做的是定义一个系统属性skipSlowTests并添加

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

at the beginning of slow tests that you usually want to skip. 在您通常想要跳过的慢速测试开始时。 Create an Eclipse launch configuration which defines the property to true and you have a convenient way to switch between the two. 创建Eclipse启动配置,将属性定义为true并且您可以方便地在两者之间切换。

If you want to run a slow test, select the method in Eclipse (or the whole class) and use "Run as JUnit Test" from the context menu. 如果要运行慢速测试,请在Eclipse(或整个类)中选择方法,然后从上下文菜单中使用“Run as JUnit Test”。 Since the property is false by default, the tests will be run. 由于默认情况下该属性为false ,因此将运行测试。

No, you cannot skip tests if they are already running. 不,如果测试已经在运行,则不能跳过测试。

What I suggest you do is use Categories to separate your slow tests from the rest of your tests. 我建议你做的是使用Categories来将慢速测试与其他测试分开。

For example: 例如:

public interface SlowTests {
}

public class MyTest {

     @Test
     public void test1{
     }

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

Create a test suite for the fast tests. 为快速测试创建一个测试套件。

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

}

Now execute the FastTestSuite if you don't want to run the slow tests (eg test1001). 现在,如果您不想运行慢速测试(例如test1001),请执行FastTestSuite Execute MyTest as normal if you want to run all the tests. 如果要运行所有测试,请正常执行MyTest

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). 基本上,JUnit执行所有@Before方法(包括@Rules),然后执行@Test方法,然后执行@After方法(同样包括@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 . 即使假设JUnit有一种机制来停止执行它的代码(它没有),大部分时间都花在你的代码上 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. 大多数IDE都可以选择终止运行测试的JVM。 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. 这听起来很复杂,因为您需要管理线程,还要在正在运行的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). 那里有并行的JUnit运行器,我认为你不会得到IDE开发人员的帮助(至少在短期内)。 Also, look at TestNG, which allows stuff to be run in parallel. 另外,查看TestNG,它允许并行运行的东西。

For using categories, one solution I use is to run the long running tests separately using maven surefire or similar, not through the IDE. 对于使用类别,我使用的一个解决方案是使用maven surefire或类似方法单独运行长时间运行的测试,而不是通过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 [*]: Java,如何停止线程关于停止线程

I think a more common solution is to have two test suites: one for the fast tests and another for the slow ones. 我认为更常见的解决方案是拥有两个测试套件:一个用于快速测试,另一个用于慢速测试。 This is typically the way you divide unit tests (fast) and integration tests (slow). 这通常是划分单元测试(快速)和集成测试(慢速)的方式。

It's highly unlikely that you'll get modifications to JUnit or IntelliJ for something like this. 你很可能不会对这样的东西进行JUnit或IntelliJ的修改。 Better to change the way you use them - it'll get you to an answer faster. 最好改变你使用它们的方式 - 它会让你更快地得到答案。

You can modify your thest and do something like 你可以修改你的东西并做一些类似的事情

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

and pass the environment variable if you want to skip the test 如果要跳过测试,则传递环境变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM