Maven surefire插件是否使用多个线程运行测试? [英] Does Maven surefire plugin run tests using multiple threads?

查看:232
本文介绍了Maven surefire插件是否使用多个线程运行测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道Maven surefire插件是否默认运行多线程测试(是否可以控制线程数?),或者它是否以随机顺序或可预测的顺序运行Test类中的测试,或者该命令是否可以通过某种方式决定.

I'm wondering if the Maven surefire plugin either runs tests multi-threaded by default (and if so can the number of threads be controlled? ) or if it runs tests from the Test classes in a random order or predictable order, or if the order can dictated by some means.

我还没有验证这一点(明天我会在这里进行一些提示指导和验证,以便进行验证),但是看来我的各种JUnit Test类正在以某种混杂的顺序运行测试.编排测试资源的创建确实很痛苦(在我的情况下这非常繁琐).

I haven't verified this yet (I'll do so tomorrow just looking for some heads up guidance and verification at this point), but it looks as if my various JUnit Test classes are getting the tests run in some intermixed order. Which makes it a real pain to orchestrate the creating of the test resources (which are quite hefty in my case).

这可能是一个经典问题,我用Eclipse JUnit运行程序运行我的套件,并且一切运行都非常线性,并且运行良好.我去了Maven cmd线,事情似乎正在一步步向前.

Its probably a classic problem I run my suite with the Eclipse JUnit runner and everything runs very linear and plays nice. I go to Maven cmd line and things seems to be stepping all over each other.

推荐答案

默认情况下,Maven在单独的(分叉")过程中运行测试,仅此而已(可以使用

By default, Maven runs your tests in a separate ("forked") process, nothing more (this can be controlled using the forkMode optional parameter).

如果您使用的是TestNG Junit 4.7+(因为 SUREFIRE-555 ),则可以并行运行测试(请参见

If you are using TestNG or Junit 4.7+ (since SUREFIRE-555), it is possible to run tests in parallel (see the parallel and the threadCount optional parameters) but that's not a default.

现在,虽然我不确定surefire插件的行为是否与JUnit相同,但是可以通过手动创建TestSuite并指定执行测试的顺序来获得一些控制权:

Now, while I'm not sure if the surefire plugin behaves the same as JUnit, it is possible to get some control by manually creating a TestSuite and specify the order in which tests are executed:

TestSuite suite= new TestSuite();
suite.addTest(new MathTest("testAdd"));
suite.addTest(new MathTest("testDivideByZero")); 

但是强烈建议您不要依赖测试执行顺序,单元测试实际上应该确实是独立的.

You are however strongly advised never to depend upon test execution order, unit tests really should be indeed independent.

PS:以防万一,还有一个请求 SUREFIRE-321 (按字母顺序运行测试).

P.S.: Just in case, there is also this request SUREFIRE-321 (to run tests in alphabetical order) that you might want to vote for.

这篇关于Maven surefire插件是否使用多个线程运行测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆