从其他 Eclipse 插件调用 JUnit [英] Call JUnit from other Eclipse Plugin

查看:33
本文介绍了从其他 Eclipse 插件调用 JUnit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 Eclipse 开发,其中一项功能是运行 JUnit 测试.现在我的插件检测工作区中项目的 JUnit 测试,然后我想调用 JUnit 来运行这个测试.

I am developing for Eclipse, and one feature is run JUnit tests. Now my plugin detect JUnit tests on a project in workspace and after this I want to call JUnit to run this tests.

我听说过 ILaunch、ILaunchConfigurationDelegate、JUnitLaunchConfigurationDelegate,但我找不到这样的例子,而且我不确定我是否必须使用它!

I heard about ILaunch, ILaunchConfigurationDelegate, JUnitLaunchConfigurationDelegate but I cannot found an example of this and also I'm not sure if I have to use this!

-- 提前致谢

推荐答案

请看我对 Eclipse 实际上如何运行 Junit 测试?.您需要创建一个运行配置,然后使用配置调用 JUnitLaunchConfigurationDelegate#launch().

Please see my answer to How does Eclipse actually run Junit tests?. You will need to create a Run Configuration and then call JUnitLaunchConfigurationDelegate#launch() with the configuration.

最简单的方法是向扩展点org.eclipse.debug.ui.launchShortcuts 添加快捷方式.使用正确的 configurationType,您可以创建正确的类型,通常,Eclipse 将完成剩下的工作.事实上,这正是我们在 Scala IDE 中所做的.

The easiest way to do this is to add shortcuts to the extension point org.eclipse.debug.ui.launchShortcuts. With the correct configurationType, you can create the correct type and normally, Eclipse will do the rest. In fact this is exactly what we've done in the Scala IDE.

这里是来自 scala-ide 的相关 XML:

Here is the relevant XML from scala-ide:

<extension point="org.eclipse.debug.ui.launchShortcuts">
    <shortcut
          label="%JUnitShortcut.label"
          icon="$nl$/icons/full/obj16/julaunch.gif"
          helpContextId="org.eclipse.jdt.junit.launch_shortcut"
          class="org.eclipse.jdt.junit.launcher.JUnitLaunchShortcut"
          modes="run, debug"
          id="scala.tools.eclipse.scalatest.junitShortcut">
       <contextualLaunch>
          <enablement>
             <with variable="selection">
                <count value="1"/>
                <iterate>
                   <adapt type="org.eclipse.jdt.core.IJavaElement">
                      <test property="org.eclipse.debug.ui.matchesPattern" value="*.scala"/>
                      <test property="org.eclipse.jdt.core.isInJavaProject"/>
                      <test property="org.eclipse.jdt.core.hasTypeOnClasspath" value="junit.framework.Test"/>
                      <or>
                      <test property="scala.tools.eclipse.launching.canLaunchAsJUnit" forcePluginActivation="true"/>
                      <test property="scala.tools.eclipse.launching.junit.canLaunchAsJUnit" forcePluginActivation="true"/>
                      </or>
                   </adapt>
                </iterate>
             </with>
          </enablement>
       </contextualLaunch>
       <configurationType
             id="org.eclipse.jdt.junit.launchconfig">
       </configurationType>
       <description
             description="%DebugJUnitLaunchShortcut.description"
             mode="debug">
       </description>
       <description
             description="%RunJUnitLaunchShortcut.description"
             mode="run">
       </description>
    </shortcut>
</extension>

重要的元素是 <contextualLaunch>,它定义了一组测试,这些测试需要为真,以便向用户显示作为 JUnit 启动的选项.其中大部分都是不言自明的,但我们也有 scala.tools.eclipse.launching.canLaunchAsJUnit,它引用了一个扩展点 org.eclipse.core.expressions.propertyTesters.这些属性测试人员测试代码是否可以作为 JUnit 启动(例如,被测类扩展 TestCase 或其他).

The important element is the <contextualLaunch>, which defines a set of tests which need to be true in order that the option to launch as JUnit be presented to the user. Most of these are self explanatory, but we've also scala.tools.eclipse.launching.canLaunchAsJUnit, which references an extension point org.eclipse.core.expressions.propertyTesters. These property testers test whether the code can be launched as JUnit or not (for instance, the class under test extends TestCase or whatever).

如果您需要更多详细信息,我建议您下载 Scala IDE,然后查看代码,但它是用 Scala 编写的.

If you need more details, I recommend downloading Scala IDE, and looking at the code, but it is written in Scala.

这篇关于从其他 Eclipse 插件调用 JUnit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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