问题描述
我在 Eclipse 和 Ant 中运行 Junit 测试时遇到了差异.这是场景:
I am running into a discrepancy when running Junit tests in Eclipse and Ant. Here is the scenario:
一切都在 Eclipse 中按预期运行,但是,在运行我编写的 Ant 构建脚本时,我无法获得准确的 Junit 报告.我对我们的测试运行程序和测试用例进行了一些更改(简而言之,我向所有测试用例添加了 Testsuite() 方法)返回一个新的 Junit4TestAdapter,并让我们的自定义运行程序执行 RunNotifier.fireTestAssumptionFailed(Failure)而不是 fireTestAssumption.现在一切都在 Ant 中运行良好,但在 Eclipse 中运行时失败被标记为通过.
Everything ran as intended in Eclipse, however, I could not get an accurate Junit report when running through an Ant build script I whipped up. I made a couple of changes to our test runner and test cases (in a nutshell I added the Test suite() method to all of my test cases) which returns a new Junit4TestAdapter, and had our custom runner execute RunNotifier.fireTestAssumptionFailed(Failure) instead of fireTestAssumption. Now everything runs fine in Ant, but failures are marked as passed when run in Eclipse.
是否有任何 Eclipse 文档可以准确解释它如何运行 Junit 测试?我基本上想知道 Eclipse 是如何执行 Junit 测试的,它是否直接通过 Ant 运行,它是否使用 Java 与 Junit 交互等等.如果有人知道该问题的实际解决方案,我也欢迎,但我会真的很想尝试自己解决这个问题,我只需要一个方向正确的点.
Is there any documentation for Eclipse that explains exactly how it runs Junit tests? I am basically looking to know exactly how Eclipse executes Junit tests, whether it's run directly through Ant, if it uses Java to interface with Junit, etc. If anyone knows an actual solution to the issue, I welcome that as well, but I would really like to try solving this one on my own, I just need a point in the right direction.
推荐答案
首先回答您的问题,如果您在 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:
- org.eclipse.jdt.junit: git://dev.eclipse.org/org.eclipse.jdt/org.eclipse.jdt.junit.git
- org.eclipse.jdt.junit.core: git://dev.eclipse.org/org.eclipse.jdt/org.eclipse.jdt.junit.core.git
- org.eclipse.jdt.junit.runtime:git://dev.eclipse.org/org.eclipse.jdt/org.eclipse.jdt.junit.runtime.git
- 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.JUnitLaunchConfigurationDelegate
和org.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.JUnit4TestReference
和 org.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.
这篇关于Eclipse 实际上是如何运行 Junit 测试的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!