Thursday, May 20, 2010

Tycho Tests Without OSGi

Goal:


The tycho project brings the power and convenience of Maven to Eclipse plug-in development. Having automated the builds for and developed (test-driven) two commercial Eclipse plug-in and RCP applications, I have always found PDE build to be the soft underbelly of the Eclipse armadillo. But I muddled my way through and over time built comprehensive suites of both JUnit microtests and Plug-in integration tests. While tycho's facility for integration tests is particularly impressive, the vast majority of my suites do not need the OSGi runtime, and some, in fact, would break when run as integration tests. In addition, some Eclipse plug-ins are implemented with the intention that they will be used outside Eclipse (EMF, for example) as well as in, and I would imagine that tests for these types of plug-ins would need to run as plain ol' JUnits for certain scenarios. The goal of this entry is to present one possibility for running microtests under tycho without OSGi.

Generate Default Poms:


First, we will need to install tycho. I primarily followed Mattias Holmqvist's instructions, along with a few others, to install Maven 3 (alpha 7), to build tycho from source, and to generate poms from my existing plug-ins and test fragments.
After generating a parent pom in the plugins directory above the individual plug-in and test fragment projects, running integration tests is simple:

  mvn integration-test -Dtycho.targetPlatform=$TYCHO_TARGET_PLATFORM


Run The Microtests:


Now that we have tycho building our plug-ins and running integration tests, we can modify the generated poms in test fragments which do not need the OSGi runtime. Fortunately, this is only a matter of configuring the surefire plug-in (and adding a JUnit 4 dependency if necessary):

<?xml version="1.0" encoding="UTF-8"?>
<project ....>
  ....
  <packaging>eclipse-test-plugin</packaging>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <executions>
          <execution>
            <id>test</id>
            <phase>test</phase>
            <configuration>
              <testClassesDirectory>${project.build.outputDirectory}</testClassesDirectory>
            </configuration>
            <goals>
              <goal>test</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.7</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>



From the plugins directory, running microtests is simple:

  mvn test -Dtycho.targetPlatform=$TYCHO_TARGET_PLATFORM


Conclusion:


With the ability to run JUnit tests for Eclipse plug-ins without OSGi, we are now able to migrate existing projects incrementally to tycho. Hopefully, others who are facing similar problems will find this tutorial useful.

5 comments:

Karsten Thoms said...

Thanks Tim for this article! It helped me to set up the test project for my Tycho build.

~Karsten

Tim Myer said...

Thank you, Karsten. I really appreciate the feedback. If you find improvements or suggestions, please let me know.

---Tim---

Marcel said...

Thanks for this article!

However, when I try this on our plugins, I get NoClassDefFoundErrors. Our situation is that we have plugins, such as, com.my.a and com.my.a.test, where com.my.a has a MANIFEST.MF containing a dependency to, say, org.codehaus.woodstox.

Now, when surefire runs our com.my.a.test, these dependencies from the MANIFEST.MF don't seem to get interpreted. (So the woodstox classes do not resolve -hence- the NCDFEs.)

Have you come across this issue? If so, how did you solve this?

__
Marcel

Tim Myer said...

Hi Marcel,
Thank you for the feedback and for the question.
Unfortunately, I have not come across that situation as I have been out of the Eclipse development space for the last few months. If you have found a solution, I would be very interested to hear how you resolved the problem.
Sorry I could not be of more help!
---Tim---

Jörn Guy Süß said...

Great job. Was a missing piece of the puzzle...