Eclipse如何实际运行Junit测试? [英] How does Eclipse actually run Junit tests?

查看:175
本文介绍了Eclipse如何实际运行Junit测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Eclipse和Ant中运行Junit测试时,我遇到了一个差异。以下是这种情况:



一切都在Eclipse中运行,但是当我运行Ant构建脚本时,我无法获得准确的Junit报告。我对我们的测试运行器和测试用例进行了几次更改(简而言之,我将Test suite()方法添加到我所有的测试用例),它返回一个新的Junit4TestAdapter,并且我们的自定义运行程序执行RunNotifier.fireTestAssumptionFailed(Failure)而不是fireTestAssumption。现在,一切都在Ant中运行正常,但是在Eclipse中运行时,失败被标记为传递。



有没有关于Eclipse的文档,能够准确地解释它运行Junit测试的方法?我基本上正在准确地知道Eclipse如何执行Junit测试,无论是直接通过Ant运行,如果它使用Java与Junit进行接口等。如果有人知道这个问题的实际解决方案,我也欢迎,但我会真的很想尝试自己解决这个问题,我只需要一个正确的方向。

解决方案

回答你的问题首先,如果Ant Junit和Eclipse JUnit之间存在差异,那么它可能是一个类路径或环境问题。最简单的方法是找到两者之间执行不同的测试,并打印出系统属性,并从该方向进行工作。另外要尝试的是从Eclipse中运行蚂蚁脚本,看看是否有任何区别(因为环境会改变)



Eclipse不使用Ant运行测试。






关于Eclipse如何运行JUnit测试,这里是一个快速的概述。请注意:Eclipse JUnit插件中有一些深刻的魔力。



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


  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中并查看它们。



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



核心类是 org.eclipse.jdt.junit.launcher.JUnitLaunchConfigurationDelegate org.eclipse.jdt.internal .junit.runner.RemoteTestRunner JUnitLaunchConfigurationDelegate 读取启动配置,并分配一个运行测试的JVM。这个新的JVM的主要类是 RemoteTestRunner 。要运行的测试将作为参数传递给分叉的JVM,作为单个测试或作为临时文件中的测试列表,如果您在项目上运行为JUnit。如果您正在调试,则可以通过在运行配置中调试复选框时检查保持活动来保持此新JVM。在这种情况下,JVM将会保留,现有测试的重新运行将通过套接字发送。



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



我会开始通过克隆git repos,将项目导入到Eclipse中并尝试使用代码。


I am running into a discrepancy when running Junit tests in Eclipse and Ant. Here is the scenario:

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.

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.

解决方案

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 does not use Ant to run the tests.


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 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

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.

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.

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 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.

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

这篇关于Eclipse如何实际运行Junit测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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