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

查看:50
本文介绍了如何为 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 和盒子的 - 因此,如果我切换盒子(我在多台机器上开发)或我的一位同事尝试运行测试,则不会传输这些设置.(此外,我的同事也使用 Eclipse 和 NetBeans 等其他 IDE.)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 中,您可以为每个运行配置指定默认设置.在 Run/Debug 配置对话框(用于配置每个测试的堆)中,单击 DefaultsJUnit.这些设置将自动应用于每个新的 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-plugin 设置这些.好的.这是最便携的方式(示例参见 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.

举个例子:在开发 servlet 时不能定义 Xmx 参数 - 由容器来定义.你不能说:这个 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天全站免登陆