我在使用 JUnit 和 Ant 时面临一个小问题。我不知道应该如何进行测试。我应该使用 forked="yes"
选项为每个测试创建一个新的虚拟机,还是应该使用相同的虚拟机?
哪个是更好的方法,或者我如何知道何时使用相同的 JVM?
谢谢
最佳答案
要在 Ant 中运行 JUnit 测试,请使用 junit
任务。可以选择在单独的 JVM 中运行测试(fork
属性)。
一般来说,最好在单独的 VM 中运行测试,以将它们与 Ant JVM 及其类路径隔离。这还允许为测试配置 JVM 的属性,例如最大内存量(任务的许多属性仅在启用 fork
时应用)。
但是,由于创建新 JVM 的开销,这会使测试花费更长的时间。这还取决于 forkmode
属性。如果设置为 perTest
,每个测试将在其自己的虚拟机中运行。
fork 模式
Controls how many Java Virtual Machines get created if you want to fork some tests. Possible values are "perTest" (the default), "perBatch" and "once". "once" creates only a single Java VM for all tests while "perTest" creates a new VM for each TestCase class. "perBatch" creates a VM for each nested and one collecting all nested s.
关于java - 如何使用 JUnit 和 Ant 运行测试?在同一个 JVM 中还是不同的 JVM 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26873889/