简体   繁体   English

如何使用JUnit和Ant运行测试? 在同一个JVM或不同的JVM中?

[英]How to run tests with JUnit and Ant? In the same JVM or different JVM?

I am facing a small problem with JUnit and Ant. 我面临着JUnit和Ant的一个小问题。 I do not know how I should run tests. 我不知道应该如何运行测试。 Should I use the forked="yes" option witch make a new VM for each test,or should I use the same VM? 我应该使用forked="yes"选项为每个测试创建一个新的VM,还是应该使用相同的VM?

Which is the better way, or how do I know when to use the same JVM or not? 哪种方法更好,或者我怎么知道何时使用相同的JVM?

Thanks 谢谢

To run JUnit tests in Ant use the junit task. 要在Ant中运行JUnit测试,请使用junit任务。 There is an option to run the tests in a separate JVM (the fork attribute). 有一个选项可以在单独的JVM中运行测试( fork属性)。

In general, it is better to run the tests in a separate VM to isolate them from the Ant JVM and its classpath. 通常,最好在单独的VM中运行测试,以将其与Ant JVM及其类路径隔离。 This also allows to configure properties of the JVM for the tests, like the maximum amount of memory (many attributes of the task are only applied if fork is enabled). 这还允许为测试配置JVM的属性,例如最大内存量(仅在启用fork情况下才应用任务的许多属性)。

However, this makes the tests take longer because of the overhead of creating a new JVM. 但是,由于创建新JVM的开销,这会使测试花费更长的时间。 This also depends on the forkmode attribute. 这也取决于forkmode属性。 If set to perTest , each test will run in its own VM. 如果设置为perTest ,则每个测试将在其自己的VM中运行。

forkmode

Controls how many Java Virtual Machines get created if you want to fork some tests. 如果要派生一些测试,控制创建多少个Java虚拟机。 Possible values are "perTest" (the default), "perBatch" and "once". 可能的值为“ perTest”(默认值),“ perBatch”和“一次”。 "once" creates only a single Java VM for all tests while "perTest" creates a new VM for each TestCase class. “一次”为所有测试仅创建一个Java VM,而“ perTest”为每个TestCase类创建一个新VM。 "perBatch" creates a VM for each nested and one collecting all nested s. “ perBatch”为每个嵌套的VM创建一个VM,并为所有嵌套的s创建VM。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM