Thursday, April 19, 2007

Testing Eclipse plug-ins

When you write Eclipse plug-ins sooner or later you will start thinking about testing your work. The very first steps will lead you to the Internet. But to my (and maybe your ;) ) surprise this information is not very easy to find. I know two ways to accomplish this task and I want to describe the first one – testing Eclipse plug-ins using build-in JUnit support.

Lets imagine that we have plug-ins that provides a projects view (very useful example ;) ).
When you start with empty workspace it should show nothing, in other case it should show projects names from your workspace.

The first think which springs to your mind is to build simple JUnit test and run it.

This is an option when you are trying to test functionality which is not dependent on Eclipse API.

So to test this functionality following steps are needed:
  • build test plug-in
  • implement tests
  • create launch configuration
  • run tests

Whole procedure is not very big deal. The first thing you'll need is new plug-in. I assume that you can do it (you've just implement plug-in you want to test).

Now you need to add JUnit plug-in to your dependencies (In my case it will be JUnit 4)

We also need dependency to our tested plug-in.

There are also two others plug-ins needed so our dependencies list can be seen in the picture .


Another thing to do is to create Junit test case. While we use JUnit 4 it can be simple class (there is no need to extend TestCase class).

All we need at this level is to build empty test method to check if we are able to run our test. The code looks this way:


package org.test.views;

import static org.junit.Assert.*;

import org.junit.Test;

public class SimpleTest {

@Test
public void simpleTestMethod() {
fail("implement this method");
}

}



Now is the moment to create run configuration. From Run menu we choose in case of Eclipse >= 3.3M5 Open run dialog... (in older versions Run...). Next we create new configuration in "JUnit Plugin test".


The difference from normal plug-in is that we have another tab called Test. On this tab we are able to specify version of JUnit and how we are going to use our test (as a single test or as a group of test from specific location).

Other tabs are also available in normal plug-in configuration so I believe you will manage to config it by yourself ;).

Now you should be able to run your test. Enjoy ;)

If you are looking for inspiration you should look into Eclipse CVS. There is thousands of test in each Eclipse project from witch you can learn.

Source code for this short tutorial can be found here.

1 comment:

விஷாஹ் மானஸ்வி said...

Hi sir,
How to get the HTML report out of the JUnit test results..? Thanks..