Duncan Jones
Duncan Jones

Reputation: 69339

Running JUnit tests from multiple projects in Eclipse

It's possible I'm losing my mind, but this morning when I upgraded to Eclipse Kepler I tried to run unit tests for multiple Eclipse projects at the same time and failed. I selected multiple projects, opened the right-click menu and the "Run as..." options were gone.

"Damn Kepler!" I thought and fired up my old Juno install to convince myself the behaviour had changed. However, it didn't work in there either.

I'm now questioning my sanity over whether this was ever truly possible. Many other Stack Overflow questions and other sites suggest this is a difficult, if not impossible task. However, I'm so sure I was doing this in the recent past that it's left me a little rattled.

So... has this even been possible, either in Kepler or Juno? Can anyone suggest a popular plugin that might have enabled that feature (which I incorrectly assumed to be default behaviour)?

Background: I have a multi-module Maven project managed through m2e and expected I could select several of the projects and run all the unit tests.

Upvotes: 6

Views: 8661

Answers (4)

Tomas
Tomas

Reputation: 1323

If you, instead of using the context menu, use the keyboard shortcut (default = Alt-Shift-X T), you will get the following dialog:

enter image description here

This is a hint about why Eclipse doesn't show the option in the menu – it thinks there are no tests. This is clearly wrong.

Digging through the Eclipse source code for JUnitLaunchShortcut (lines 160-191), I found this:

private void launch(Object[] elements, String mode) {
    try {
        IJavaElement elementToLaunch= null;

        if (elements.length == 1) {
...
        }
        if (elementToLaunch == null) {
            showNoTestsFoundDialog();
            return;
        }

It only works if you select exactly one test class.

The menu option visibility is controlled by the org.eclipse.jdt.junit plugin configuration (plugin.xml), and that has the same issue (lines 221-234):

<contextualLaunch>
    <enablement>
        <with variable="selection">
            <count value="1"/>
            <iterate>
                <adapt type="org.eclipse.jdt.core.IJavaElement">
                    <test property="org.eclipse.jdt.core.isInJavaProject"/>
                    <test property="org.eclipse.jdt.core.hasTypeOnClasspath" value="junit.framework.Test"/>
                    <test property="org.eclipse.jdt.junit.canLaunchAsJUnit" forcePluginActivation="true"/>
                </adapt>
            </iterate>
        </with>
    </enablement>
</contextualLaunch>

The <count value="1"/> bit in the beginning acts as a selector, and it means the same thing: you have to have exactly one item selected, or the menu item won't show up.

I think we have found the problem :)

I also checked the file history for both these, and they haven't been changed since September 2006. So if you managed to do this with a more recent version you most likely had some kind of plugin installed that let you do it.

Upvotes: 3

jdwyah
jdwyah

Reputation: 1282

Bizarrely, installing the C developer tools gives you a feature that lets you run multiple groups. See http://wiki.eclipse.org/CDT/User/FAQ#HOWTO_use_C.2FC.2B.2B_Unit_Testing_Support

Upvotes: 1

Effi Bar-She&#39;an
Effi Bar-She&#39;an

Reputation: 792

You can perform JUnit tests across multiple projects using Classpath Suite. In general all you need to do is:

  1. Create an Eclipse project depending on all the projects you want to test.
  2. Write a suite:

    @RunWith(ClasspathSuite.class)
    public class MySuite

Take a look on this article: Roger Rabbit - JUnit Tests Runner Across Multiple Projects, it includes a step by step example and a code sample.

Upvotes: 6

Matthew Farwell
Matthew Farwell

Reputation: 61705

You can't run the tests for multiple projects in Eclipse, at least using the normal runners. There are, however a number of options:

  1. Create an ant/maven script which runs everything
  2. If you want to run your tests every time you save, you can use Infinitest.

From the site of Infinitest:

Infinitest is a Continuous Testing plugin for Eclipse and IntelliJ. Each time a change is made on the source code, Infinitest runs all the tests that might fail because of these changes.

Infinitest can potentially run all of the tests in all projects.

Upvotes: 1

Related Questions