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

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

问题描述

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



我听说过关于ILaunch,ILaunchConfigurationDelegate,JUnitLaunchConfigurationDelegate但是我不能发现了一个例子,我也不知道我是否必须使用这个!



- 提前感谢

解决方案

请参阅我的答案 Eclipse如何实际运行Junit测试?您将需要创建运行配置,然后使用配置调用JUnitLaunchConfigurationDelegate#launch()。



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



以下是scala-ide中的相关XML:

  ; extension point =org.eclipse.debug.ui.launchShortcuts> 
<捷径
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.matchesPatternvalue =*。scala/>
< test property =org.eclipse.jdt.core.isInJavaProject/>
< test property =org.eclipse.jdt.core.hasTypeOnClasspathvalue =junit.framework.Test/>
<或>
< test property =scala.tools.eclipse.launching.canLaunchAsJUnitforcePluginActivation =true/>
< test property =scala.tools.eclipse.launching.junit.canLaunchAsJUnitforcePluginActivation =true/>
< /或>
< / 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或其他任何内容)。



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


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.

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!

-- Thanks in advance

解决方案

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.

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.

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>

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

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天全站免登陆