且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Eclipse 实际上是如何运行 Junit 测试的?

更新时间:2023-01-18 08:04:05

首先回答您的问题,如果您在 Ant junit 和 Eclipse JUnit 之间存在差异,则可能是类路径或环境问题.最简单的方法是找到一个在两者之间执行不同的测试并打印出系统属性,然后从那个方向进行处理.另一件要尝试的事情是在 Eclipse 中运行 ant 脚本,看看这是否有什么不同(因为环境会改变)

To answer your problem first, if you have a discrepancy between the Ant junit and Eclipse JUnit, it's probably a classpath or environmental problem. The easiest way is to find a test that performs differently between the two and print out the System properties, and work at it from that direction. Another thing to try would be to run the ant scripts from within Eclipse, to see if this makes any difference (because the environment will change)

Eclipse 不使用 Ant 来运行测试.

Eclipse does not use Ant to run the tests.

至于 Eclipse 如何运行 JUnit 测试,这里是一个快速概述.请注意:Eclipse JUnit 插件中有一些深奥的魔法.

As for how Eclipse runs JUnit tests, here is a quick overview. Be warned: there is some deep magic in the Eclipse JUnit plugin.

Eclipse 有 4 个 JUnit 插件,在大多数配置中都是默认安装的:

Eclipse has 4 JUnit plugins, which are all installed by default in most configurations:

  1. org.eclipse.jdt.junit: git://dev.eclipse.org/org.eclipse.jdt/org.eclipse.jdt.junit.git
  2. org.eclipse.jdt.junit.core: git://dev.eclipse.org/org.eclipse.jdt/org.eclipse.jdt.junit.core.git
  3. org.eclipse.jdt.junit.runtime:git://dev.eclipse.org/org.eclipse.jdt/org.eclipse.jdt.junit.runtime.git
  4. org.eclipse.jdt.junit4.runtime:git://dev.eclipse.org/org.eclipse.jdt/org.eclipse.jdt.junit4.runtime.git

这些是实际 CVS 存储库的 git 镜像.上次我尝试使用它们时,它们没有编译,但它们会给你代码,你至少可以将项目导入 Eclipse 并查看它们.

These are git mirrors of the actual CVS repositories. Last time I tried to use them, they didn't compile, but they'll give you the code and you can at least import the projects into Eclipse and look at them.

如果我们忽略配置页面、插件如何创建运行配置、JUnit 视图本身的代码以及它如何找到要运行的相关测试,我们可以专注于它如何运行测试.

If we ignore the configuration pages, how the plugin creates run configurations, the code for the JUnit view itself and how it finds the relevant tests to run, we can concentrate on how it runs the tests.

核心类是org.eclipse.jdt.junit.launcher.JUnitLaunchConfigurationDelegateorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.JUnitLaunchConfigurationDelegate 读取启动配置并分叉一个 JVM,测试将在其中运行.这个新 JVM 的主要类是 RemoteTestRunner.要运行的测试作为参数传递给分叉的 JVM,如果您在项目上执行 Run as JUnit,则可以作为单个测试或作为临时文件中的测试列表.如果您正在调试,可以通过选中运行配置中的 Keep alive when debugging 复选框来保持这个新的 JVM 处于活动状态.在这种情况下,JVM 将继续存在,并且现有测试的重新运行将通过套接字发送.

The core classes are org.eclipse.jdt.junit.launcher.JUnitLaunchConfigurationDelegate and org.eclipse.jdt.internal.junit.runner.RemoteTestRunner. JUnitLaunchConfigurationDelegate reads the launch configuration and forks a JVM in which the tests will be run. The main class for this new JVM is RemoteTestRunner. The tests to be run are passed as parameters to the forked JVM, either as a single test or as a list of tests in a temporary file if you're doing Run as JUnit on a project. If you're debugging, this new JVM can be kept alive by checking the Keep alive when debugging checkbox in the run configuration. In this case, the JVM will stick around and reruns of existing tests will be sent via sockets.

RemoteTestRunner 运行测试并通过套接字将结果传回 Eclipse,然后 Eclipse 更新 JUnit 视图.其核心是运行测试(针对 JUnit 4)的 org.eclipse.jdt.internal.junit4.runner.JUnit4TestReferenceorg.eclipse.jdt.internal.junit4.runner.JUnit4TestListener,这是这些测试的 RunListener.JUnit4TestListener 扩展了 RunListener,但不会覆盖 testAssumptionFailure,这可能是您的测试在 Eclipse 中通过的原因.RunListener.testAssumptionFailure 是一个空方法,它什么都不做,所以你的通知将被忽略.

RemoteTestRunner runs the tests and passes back the results via sockets to Eclipse, which then updates the JUnit view. The heart of this is org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference, which runs the test (for JUnit 4), and org.eclipse.jdt.internal.junit4.runner.JUnit4TestListener, which is the RunListener for these tests. JUnit4TestListener extends RunListener, but doesn't override testAssumptionFailure, which is probably why your tests are passing in Eclipse. RunListener.testAssumptionFailure is an empty method, it does nothing, so your notifications will be ignored.

我将首先克隆 git 存储库,将项目导入 Eclipse 并尝试处理代码.

I would start by cloning the git repos, importing the projects into Eclipse and trying to work through the code.