Reputation: 861
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
Upvotes: 1
Views: 1557
Reputation: 85
I rewrote part of launche junit services, then inform the project it calls the debug and run.
See my implementation to call the JUnitLaunch
Upvotes: 0
Reputation: 61695
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.
Upvotes: 2