Thursday, July 23, 2009

Running Eclipse Plugin Tests with SWTBot in Headless Mode

This is a followup to my previous post on SWTBot. Headless mode is very useful in running tests outsode of eclipse through Ant or from the command line.

Headless?

Well, it's not exactly headless. A better title would be "running Eclipse Plugin Tests with SWTBot with Ant or From The Command Line." The tests are not run from an Eclipse workbench, but they require Eclipse, and actually open up a workbench when they are run.

Install

Here's the place to start: http://wiki.eclipse.org/SWTBot/Ant

Download the "Headless Testing Framework" from here: http://www.eclipse.org/swtbot/downloads.php and install it into your eclipse /plugins dir. You need to install both the junit4.headless and optional.junit4 files. For example:

org.eclipse.swtbot.eclipse.junit4.headless_2.0.0.371-dev-e34
org.eclipse.swtbot.ant.optional.junit4_2.0.0.371-dev-e34.jar

Important note: Be sure to install only these files for either JUnit3 or JUnit4. If you install the files for both versions of JUnit, you'll see many class cast exceptions.

Configuration

Be sure to define a class to serve as a test suite for your tests. For example;
package org.company.test;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses( { TestPositive.class, TestNegative.class })
public class AllTests {
}
Setup

Export your plugin tests (as a plugin) and install it into the /plugins directory of your eclipse installation.

Running with Ant

Build a build.xml file that looks like this:

<?xml version="1.0" encoding="UTF-8" ?>

<project name="testsuite" default="run" basedir=".">
<property name="eclipse-home" value="/opt/local/eclipse_swtbot/eclipse" />
<property name="plugin-name" value="org.company.test" />
<property name="test-classname" value="org.company.test.AllTests" />
<property name="library-file" value="${eclipse-home}/plugins/org.eclipse.swtbot.eclipse.junit4.headless_2.0.0.371-dev-e34/library.xml" />

<target name="suite">

<property name="jvmOption" value=""></property>
<property name="temp-workspace" value="workspaceJuly23" />
<delete dir="${temp-workspace}" quiet="true" />

<ant target="swtbot-test" antfile="${library-file}" dir="${eclipse-home}">
<property name="data-dir" value="${temp-workspace}" />
<property name="plugin-name" value="${plugin-name}" />
<property name="os" value="linux" />
<property name="ws" value="gtk" />
<property name="arch" value="x86" />
<property name="classname" value="${test-classname}" />
<property name="vmargs" value="-Xms128M -XX:MaxPermSize=512m -Xmx512M" />
</ant>
</target>

<target name="cleanup" />

<target name="run" depends="suite,cleanup">
</target>

</project>
Important note: The supported values for os, ws (workspace) and arch are:

os: win32/linux/macosx
ws: win32/wpf/gtk/cocoa/carbon
arch: x86/x86_64

Important note: Be sure to specify a non-existent workspace name for temp-workspace as this will be overwritten when the test is run.

Running from the CLI

export ECLIPSE_HOME=/opt/local/eclipse_swtbot/eclipse
export TEST_CLASS=org.company.test.AllTests

java -Xms128M -Xmx368M -XX:MaxPermSize=256M -DPLUGIN_PATH= -classpath $ECLIPSE_HOME/plugins/org.eclipse.equinox.launcher_1.0.101.R34x_v20081125.jar org.eclipse.core.launcher.Main -application org.eclipse.swtbot.eclipse.junit4.headless.swtbottestapplication -data workspace formatter=org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter,$ECLIPSE_HOME/$TEST_CLASS.xml formatter=org.apache.tools.ant.taskdefs.optional.junit.PlainJUnitResultFormatter -testPluginName org.jboss.qa.guvnor.egt -className $TEST_CLASS -os linux -ws gtk -arch x86 -consoleLog -debug

Test Output

It can be hard to find in all the output, but it's there:

Testcase: checkPerspective took 3.529 sec
Testcase: canConnectToRepo took 5.784 sec
Testcase: goIntoBackHomePropertiesTest took 34.526 sec
Testcase: doubleClickTest took 51.74 sec
Testcase: cannotConnectBadPath took 6.701 sec
Testcase: cannotConnectBadPassword took 7.747 sec

Other Useful Links

Ketan added some movies to the SWTBot site in July 2009. Here's one on running SWTBot from Ant: http://download.eclipse.org/technology/swtbot/docs/videos/beginners/SWTBotHeadlessTestingForNovices

No comments: