Running JUnit tests from multiple projects in Eclipse

I may be losing my mind, but this morning, when I upgraded to Eclipse Kepler, I tried to run unit tests for several Eclipse projects at the same time and failed. I selected several projects, opened the right-click menu and the option "Run from ..." disappeared.

"Damn Kepler!" I thought and ran my old Juno installation to convince myself that the behavior had changed. However, he did not work either.

Now I ask myself the question of whether this is really possible. Many other Questions and other sites suggest that this is a difficult, if not impossible task. However, I am so sure that I have been doing this in the recent past that it trembled a little.

So ... is this even possible in Kepler or Juno? Can anyone suggest a popular plugin that could enable this feature (which I misunderstood by default)?

Background: I have a multi-module Maven project managed via m2e, and I expected that I could select several projects and complete all unit tests.

+6
source share
4 answers

You can run JUnit tests for multiple projects using the Classpath Suite . In general, all you have to do is:

  • Create an Eclipse project based on all the projects you want to test.
  • Write a set:

    @RunWith (ClasspathSuite.class)
    public class MySuite

Take a look at this article: Roger Rabbit - JUnit Runner tests in several projects , it includes a step-by-step example and sample code.

+5
source

You cannot run tests for multiple projects in Eclipse, at least using regular runners. There are, however, several options:

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

From the Infinitest website:

Infinitest is the Continuous Testing plugin for Eclipse and IntelliJ. Each time a source code change occurs, Infinitest runs all tests that may fail due to these changes.

Infinitest can potentially run all tests in all projects.

+1
source

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

+1
source

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

enter image description here

This is a hint about why Eclipse does not show this option in the menu - it thinks there are no tests. This is clearly wrong.

Digging into 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 visibility of the menu parameter is controlled by the plugin configuration org.eclipse.jdt.junit ( plugin.xml ) and has the same problem (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 bit <count value="1"/> at the beginning acts as a selector, and this means the same thing: you only have to select one item or the menu item will not be displayed.

I think we found the problem :)

I also checked the file history for both of these, and they have not been changed since September 2006. So, if you managed to do this with a newer version, you most likely have some kind of plugin installed that will allow you to do this.

+1
source

Source: https://habr.com/ru/post/948211/


All Articles