Skip to content

Running tests in Eclipse

mdirkse edited this page Apr 10, 2012 · 1 revision

When you've setup your build environment and look in the Package Explorer you'll notice bunch of *.test packages which contain (wait for it...) tests.

Fantastic. But what if we want to actually run these tests? If you've previously used Eclipse for all kinds of things except developing OSGI plugins, then you might be tempted to simply right-click a package (or a class) and hit Run as -> Run as JUnit test. Alas, this won't work, as all tests will fail with a java.lang.IllegalStateException.

The tests fail because (most) Geppetto tests assume the existence of the OSGI container (aka the Eclipse RCP) as an environment in which to run. Luckily, starting an OSGI container in order to run tests is something that Eclipse is quite good at. To construct a setup that will work, go to Run -> Run Configurations and create a new JUnit Plug-in Test (at this point, you may notice that there's already a configuration of that type present, called AllTests). "JUnit Plug-in Test" in this instance means: a configuration for a JUnit test which exercises an OSGI plugin, or, put simply, a JUnit test that won't run until a particular OSGI environment is setup. Which is great, 'cause that's what we were looking for.

When you've created the new JUnit Plug-in Test configuration, give it an appropriate name, and choose the package and, optionally, class where the tests you want to run reside. At this point you could just hit Run and things should work. It might, however, be useful to configure a few more of the rather bewildering array of options that the test configuration exposes.

By default, the Plug-in Test runs in a kind of funky Eclipse-within-Eclipse mode which open some windows and flashes some progress bars across the screen. This is unnecessary for Geppetto tests, and can be turned off by going to the Main tab of the configuration, and selecting the [No Application] - Headless Mode option under Run an application. Not only will this save memory and make the test run faster, it'll also leave your Eclipse windows alone and provide for a less unsettling test experience.

The other setting worth tuning is exactly which parts of the Geppetto platform Eclipse launches in preparation for your test. The Plug-ins configuration tab has several options for the Launch with setting. The all workspace and enabled target plug-ins option simply launches the entire Geppetto platform before running your test. This is the most memory and processor intensive choice, but also the easiest, as you don't have to worry about which parts of the platform your test requires to run. However, if you're running in a memory constrained environment, or would simply like the test to run as fast as possible, the plug-ins selected below only option give you the ability to pick and choose which parts of the platform you'd like to load for your test.

Let's say you're working on the org.cloudsmith.geppetto.ruby.tests.PuppetFunctionTests test. You don't need the entire platform to run it, the org.cloudsmith.geppetto.ruby.jrubyparser plug-in will suffice. In order to make sure that only this plugin, and it's dependencies, is launched, set Launch with: to plug-ins selected below only, and select org.cloudsmith.geppetto.ruby.jrubyparser from the list of plug-ins. Then click the Add Required Plug-ins button. This causes eclipse to analyse the dependencies of the selected plug-in, and automatically add any required plug-ins to the configuration. This ensures that only the truly necessary parts of the platform are loaded for your test, thus saving memory and processor cycles.

That's it: you should now be able to run the tests in the Geppetto project. Happy testing!

Clone this wiki locally