views:

457

answers:

3

How do I run a Eclipse JUnit plug-in test in a non-ui thread when running the tests from the command-line? In the launch configuration dialog I can uncheck the checkbox "Run in UI thread", but how do I do that when running plug-in tests on the command-line?

EDIT: It seems as if org.eclipse.pde.junit.runtime.nonuithreadtestapplication is what the PDE launch uses when running tests in a non-UI thread, but when I try using that, I get "parameter '-port' not found":

Loading logger configuration: c:\work\dev\tooticki\core\ide\eclipse\plugins\com.iar.ide.tests\logging.properties
23:42:51,349 [main           ] INFO  ew                    - Starting application: class com.iar.ew.Application
Exception in thread "WorkbenchTestable" java.lang.IllegalArgumentException: Error: parameter '-port' not specified
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.defaultInit(RemoteTestRunner.java:303)
    at org.eclipse.pde.internal.junit.runtime.RemotePluginTestRunner.init(RemotePluginTestRunner.java:83)
    at org.eclipse.pde.internal.junit.runtime.RemotePluginTestRunner.main(RemotePluginTestRunner.java:61)
    at org.eclipse.pde.internal.junit.runtime.NonUIThreadTestApplication.runTests(NonUIThreadTestApplication.java:23)
    at org.eclipse.ui.internal.testing.WorkbenchTestable$1.run(WorkbenchTestable.java:71)
    at java.lang.Thread.run(Thread.java:619)
+1  A: 

When you say running the tests from the command-line, do you mean by invoking the PDE ant goals?

The ant task to run the PDE tests has two targets, core-test and ui-test for testing headlessly and with the UI respectively. There's an article on the Eclipse Test Framework with more details, but essentially you can just pick the relevant target and as long as your code under test doesn't need access to the UI your tests will run under the core-test target. You may also find this Eclipse Corner article on PDE and Ant helpful

If you are invoking the tests by another means. You can still have a look at the source of the PDE ant tasks to understand how they setup the environment

Rich Seller
No, I'm not using the PDE ant goals. I'm invoking my product launcher directly.
JesperE
In that case you can copy the processing that the ant tasks use, see the source link in my answer
Rich Seller
+1  A: 

Create a new application class that extends from org.eclipse.pde.internal.junit.runtime.UITestApplication:

public class NonUIThreadTestApplication extends UITestApplication {
  public void runTests() {
    fTestableObject.testingStarting();
    try {
      EclipseTestRunner.run(Platform.getCommandLineArgs());
    } catch (IOException e) {
      e.printStackTrace();
    }
    fTestableObject.testingFinished();
  }
}

I think you must copy EclipseTestRunner as is from org.eclipse.test plug-in into your plug-in due to dependency restrictions.

Add a new application into your plugin.xml:

<extension
         id="nonuithreadtestapplication"
         point="org.eclipse.core.runtime.applications">
  <application visible="false">
    <run class="com.my.test.NonUIThreadTestApplication" />
  </application>
</extension>

Add a new section into org.eclipse.test/library.xml:

<target name="nonui-thread-ui-test" description="Eclipse application used to launch UI plugin tests in a non-UI thread." depends="init">
  <antcall target="${launchTarget}">
    <param name="application" value="com.my.test.nonuithreadtestapplication" />
  </antcall>
</target>

After this is done, you can run your tests using nonui-thread-ui-test target instead of ui-test or core-test.

Good luck!

spektom
A: 

Apparently, org.eclipse.pde.junit.runtime.nonuithreadtestapplication needs a dedicated test results listener. How to do this is described in this article.

JesperE