Java类中的JUnit Fork模式 [英] JUnit Fork-Mode in Java Classes

查看:68
本文介绍了Java类中的JUnit Fork模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ant和Maven中对 forkMode 的支持,有时我们将它与值 perTest 一起使用.但是,当我们在类或项目上运行测试时(运行方式-> JUnit测试),Eclipse中的JUnit测试仍然失败.显然,JUnit使用默认设置或行为并并行执行测试,从而在JUnit视图中引起一些红叉.

There's support for forkMode in Ant and Maven and occasionally we use it with value perTest. However, the JUnit-tests in Eclipse still fail when we run the tests on a class or on a project (Run As -> JUnit Test). Obviously JUnit uses default settings or behaviour and executes the tests in parallel causing some red crosses in the JUnit-view.

  1. 是否有一种方法可以将某些代码编码到测试类中,从而使JUnit的行为类似于 forkMode 设置?我们不介意是否有针对Eclipse的解决方案.
  2. 或者这可以通过在Eclipse中运行配置来完成吗?
  1. Is there a way to code something into the test-class that lets JUnit behave like the forkMode setting? We don't mind if there's an Eclipse-only solution for this.
  2. Or can this be done with a Run Configuration in Eclipse?


我了解问题是基于测试后剩余的数据,因此进一步的测试将失败.尽管这很有意义,但是请您理解,这并不能回答我的问题.想想我的情况是某种老虎团队的一部分.我们有很多问题,解决现有测试的那部分只是其中之一.相信我,我们将竭尽所能...(我已经有一段时间没有听说过)

推荐答案

Eclipse在同一JVM中的单个线程中串行运行JUnit测试.如果您具有正常并行运行的测试,则这不应影响测试行为.但是,如果您假设可以更改VM中的设置(例如系统属性或类静态变量),而下一个测试将没有这些更改,则会破坏您的测试.

Eclipse runs the JUnit test serially, in a single thread, in the same JVM. If you have tests that normally operate in parallel, this should not affect the test behavior. However, if you assume that you can change settings in the VM, like system properties, or class static variables, and the next test will not have those changes, that will break your tests.

经验法则是,每个测试应完全按照其发现的状态离开系统(vm,数据库,文件系统),以便可以在任何时间,以任何顺序运行每个测试.

The rule of thumb is that each test should leave the system (vm, database, filesystem) exactly as it found it so that each test can be run at any time, in any order.

这篇关于Java类中的JUnit Fork模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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