如何为Junit单元测试设置JVM参数? [英] How to set JVM parameters for Junit Unit Tests?

查看:3994
本文介绍了如何为Junit单元测试设置JVM参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些需要大量堆空间运行的Junit单元测试 - 即1G。 (他们测试了只能在足够的堆空间运行的webstart应用程序的内存密集型功能,并且将在Win 7 64位计算机上内部运行 - 因此重新设计测试并不是一个实用的建议。)

I have some Junit unit tests that require a large amount of heap-space to run - i.e. 1G. (They test memory-intensive functionality for a webstart app that will only run with sufficient heap-space, and will be run internally on Win 7 64-bit machines - so redesigning the tests isn't a practical suggestion.)

我在Intellij IDEA中开发,所以我知道我可以为测试类设置JVM参数(例如-Xmx1024M)。但是,这仅用于运行整个测试类 - 如果我想运行单独的测试,我必须重新创建该测试方法的运行配置。

I am developing in Intellij IDEA, so I know I can set the JVM parameters (e.g. -Xmx1024M) for the test class. However, this is only for running the whole test class - if I want to run an individual test, I have to recreate the run congfigurations for that test method.

此外,这些是IDE和特定于盒子的 - 所以如果我切换盒子(我在多台机器上开发)或者我的一个同事尝试运行测试,那些设置是没有转移。 (此外,我的同事也使用其他IDE,如Eclipse和NetBeans。)FWIW,我们使用mercurial进行源代码控制。

Also, those are IDE and box specific - so if I switch boxes (I develop on multiple machines) or one of my colleagues tries to run the tests, those settings are not transferred. (Also, other IDEs like Eclipse and NetBeans are used by my colleagues.) FWIW, we're using mercurial for source code control.

对于构建周期,我们'重新使用Maven,所以我知道如何为它指定JVM参数。

For the build cycle, we're using Maven, so I know how to specify the JVM parameters for that.

所以:
- 我正在寻找一种指定JVM参数的方法这将适用于整个测试类和各个测试方法;和
- 我想在任何机器上的IDE之间共享这些规范(从存储库中获取代码)。

So: - I'm looking for a way of specifying the JVM parameters that will apply for the whole test class and the individual test methods; and - I'd like to share those specification across IDEs on any machine (having picked up the code from the repository).

推荐答案

在IntelliJ中,您可以为每个运行配置指定默认设置。在运行/调试配置对话框(用于为每个测试配置堆的对话框)中,单击默认值 JUnit 。这些设置将自动应用于每个新的JUnit测试配置。我想Eclipse中存在类似的设置。

In IntelliJ you can specify default settings for each run configuration. In Run/Debug configuration dialog (the one you use to configure heap per test) click on Defaults and JUnit. These settings will be automatically applied to each new JUnit test configuration. I guess similar setting exists for Eclipse.

然而,没有简单的选项来跨环境传输这些设置(至少在IntelliJ中)。您可以将IntelliJ项目文件提交到您的存储库:它可能有效,但我不推荐它。

However there is no simple option to transfer such settings (at least in IntelliJ) across environments. You can commit IntelliJ project files to your repository: it might work, but I do not recommend it.

您知道如何为 maven设置这些文件-surefire-插件。好。这是最便携的方式(参见Ptomli的答案)。

You know how to set these for maven-surefire-plugin. Good. This is the most portable way (see Ptomli's answer for an example).

其余的 - 你必须记住JUnit测试用例只是一堆Java类,而不是一个独立的程序。这取决于跑步者(让它成为一个独立的JUnit跑步者,你的IDE, maven-surefire-plugin 来设置这些选项。据说没有便携式设置它们的方法,以便无论跑步者如何都应用内存设置。

For the rest - you must remember that JUnit test cases are just a bunch of Java classes, not a standalone program. It is up to the runner (let it be a standalone JUnit runner, your IDE, maven-surefire-plugin to set those options. That being said there is no "portable" way to set them, so that memory settings are applied irrespective to the runner.

举个例子:你不能定义 Xmx 开发servlet时的参数 - 由容器来定义它。你不能说:这个servlet应该始终用 Xmx = 1G 运行。

To give you an example: you cannot define Xmx parameter when developing a servlet - it is up to the container to define that. You can't say: "this servlet should always be run with Xmx=1G.

这篇关于如何为Junit单元测试设置JVM参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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