Maven Surefire 插件是否使用多线程运行测试?

发布于 2024-08-22 04:14:56 字数 303 浏览 6 评论 0原文

我想知道 Maven Surefire 插件是否默认运行多线程测试(如果是的话,可以控制线程数吗?),或者它是否以随机顺序或可预测的顺序运行测试类中的测试,或者如果顺序可以通过某种方式决定。

我还没有验证这一点(明天我会这样做,只是为了寻找一些提示指导和验证),但看起来好像我的各种 JUnit 测试类正在以某种混合顺序运行测试。这使得编排测试资源的创建变得非常痛苦(在我的例子中这是相当大的)。

这可能是一个经典问题,我使用 Eclipse JUnit 运行器运行我的套件,一切都运行得非常线性并且运行良好。我进入 Maven 命令行,一切似乎都在相互影响。

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.

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).

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

彩虹直至黑白 2024-08-29 04:14:56

默认情况下,Maven 在一个单独的(“分叉”)进程中运行您的测试,仅此而已(这可以使用 forkMode 可选参数)。

如果您使用 TestNG Junit 4.7+(自 SUREFIRE- 555),可以并行运行测试(请参阅并行threadCount 可选参数)但这不是默认值。

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

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

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

PS:以防万一,还有这个请求 SUREFIRE-321 (在按字母顺序排列)您可能想要投票。

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

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.

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.

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.

〃安静 2024-08-29 04:14:56

首先,您的单元测试应该彼此独立。这是因为即使 JUnit 也不能​​保证执行顺序,因此每个测试都应该独立于之前或之后发生的情况来设置和拆除其上下文(也称为测试装置)。

执行顺序绝对不是随机的,在 JUnit 中它往往是相同的(我猜是字母顺序),但你不应该在它的基础上构建 - 它可以随时改变,显然在 Surefire 中顺序不同。

这是一个很好的链接,说明为什么交互测试不是一个好主意。

First of all, your unit tests should be independent of each other. This is because the order of execution is not guaranteed even by JUnit, so each test should set up and tear down its context (aka test fixture) independent of what happens before or after.

The order of execution is definitely not random though, in JUnit it tends to be the same (I would guess alphabetical order), but you should not build on it - it can change anytime, and apparently in Surefire the order is different.

Here is a good link on why interacting tests are not a good idea.

往昔成烟 2024-08-29 04:14:56

JUnit 按照测试在 .java 文件中出现的顺序(不是按字母顺序)运行测试。 Maven-surefire 以不同的顺序运行它们,但不可预测(据我所知)。

理想情况下,测试应该彼此独立,但单例和静态上下文会使事情变得复杂。在单独的测试用例(但不是单独的测试)执行之间获取新的静态上下文的一个有用方法是在 pom.xml 中设置 forkMode 变量

JUnit runs tests in the order in which they appear in the .java file (not alphabetically). Maven-surefire runs them in a different order, but not predictably (as far as I can tell).

Ideally, tests would be independent of one another, but singletons and static context can complicate things. A helpful way to get new static contexts between executions of separate TestCase's (but not individual tests) is to set the forkMode variable in your pom.xml..

<forkMode>always</forkMode>

○愚か者の日 2024-08-29 04:14:56

我发现,如果您在 maven 命令中使用 -T 选项,Surefire 将分叉为 forkCount * <通过 -T 选项指定的线程数> 并发进程数。

为了使它们都在一个进程中运行,尽管有 -T 指定的多个线程,您可以通过添加选项 -Dsurefire.forkCount=0 来强制 forkCount 为 0

I have found that if you are using the -T option in your maven command, Surefire will then fork into forkCount * <specified number of threads by the -T option> number of concurrent processes.

To make them all run in one process despite having multiple threads specified by -T, you can force forkCount to be 0 by adding the option -Dsurefire.forkCount=0

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文