Suppose I want to manually run from my IDE (Intellij IDEA, or eclipse) 4000 JUnit tests; the first 1000 tests run pretty smoothly (say they take 3 minutes all 1000) but the test 1001 takes alone over 30 minutes. 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. I do not want to @Ignore the test 1001 and rerun the suite because I already have the answer for tests 1-1000; also I do not want to select tests 1001-4000 because it takes too much time.

假设我想从我的IDE(Intellij IDEA或eclipse)4000 JUnit测试中手动运行;前1000次测试运行得非常顺利(比如他们需要3分钟全部1000次),但测试1001单独使用超过30分钟。有没有办法可以跳过测试1001(当它仍在运行时)并让测试1002(和其他人)继续前进。我不想@Ignore测试1001并重新运行套件,因为我已经有测试1-1000的答案;我也不想选择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开发人员需要对其进行增强吗?

5 个解决方案

#1


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 [*].

基本上,JUnit执行所有@Before方法(包括@Rules),然后执行@Test方法,然后执行@After方法(同样包括@Rules)。即使假设JUnit有一种机制来停止执行它的代码(它没有),大部分时间都花在你的代码上。因此,要“跳过”已经启动的测试,您需要修改测试代码(以及可能正在测试的代码),以便您可以彻底停止它。干净地停止正在执行的线程本身就是一个问题[*]。

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.

    停止执行测试,并修复您正在处理的测试。大多数IDE都可以选择终止运行测试的JVM。这绝对是最简单的选择。

  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.

    实现自己的测试运行器,它在一个单独的线程中运行测试。然后,该测试运行器要么等待线程完成执行,要么在某处检查一个标志,这将是它停止的信号。这听起来很复杂,因为您需要管理线程,还要在正在运行的jvm中设置标志。也许在某处创建一个文件?这个跑步者将失败当前正在运行的测试,你可以继续下一个。请注意,中途“停止”测试可能会使事物处于不一致状态,或者您可能最终并行执行测试。

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.

那里有并行的JUnit运行程序,我认为你不会得到IDE开发人员的帮助(至少在短期内)。另外,看看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. This involves checking out the source code somewhere else on my machine and building there.

对于使用类别,我使用的一个解决方案是使用maven surefire或类似方法单独运行长时间运行的测试,而不是通过IDE。这包括在我的机器上的其他地方检查源代码并在那里构建。

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

[*]:Java,如何停止线程,关于停止线程

更多相关文章

  1. java多线程爬虫
  2. javafx 和netty 混合使用出现线程不一致问题,求大神指点
  3. 基于Java的应用程序的GUI测试工具
  4. Java多线程之Thread、Runnable、Callable及线程池
  5. 测试技术大牛谈成长经历:一个好的软件测试工程师应该做到这些!
  6. Java,Socket&TCP编程 实现多线程端对端通信与文件传输
  7. java高并发测试实例(精确到几百纳秒)
  8. Java错误:线程“main”中的异常java.lang.ArrayIndexOutOfBoundsE
  9. Java并发面试题:三个线程轮流打印十次abc

随机推荐

  1. 21款优秀Android开源库整理推荐
  2. Anddroid各种布局总结
  3. 使用Android Studio与ArcGIS Android SDK
  4. Android技术内幕
  5. Android热修复(2):AndFix热修复框架的使用
  6. android仿网易云音乐、即时通讯、bilibil
  7. [转]android:layout_gravity 和 android:
  8. Android(安卓)-- StateMachine瑙f瀽
  9. Android 多线程之几个基本问题
  10. Android通知栏微技巧,8.0系统中通知栏的适