Monday, January 7, 2013

Tycho build 7: Plug-in unit tests

During the previous tutorials we focused on building stuff from our productive code. But good development is test driven and requires a lot of unit testing. Tycho can integrate tests in the build process which automatically gives you test coverage on every build you do.

Tycho Tutorials

For a list of all tycho related tutorials see Tycho Tutorials Overview

Source code for this tutorial is available on github as a single zip archive, as a Team Project Set or you can browse the files online.

Step 1: Create a sample unit test

Unit tests for plug-ins should normally be provided as a fragment to the plug-in under test. Fragments allow to access all classes of the main plug-in without exporting them. If you use the same package name for your unit test class and the class under test you can even access package private methods.

Create a new Fragment Project named com.codeandme.tycho.plugin.test. Set the Host Plug-in ID to com.codeandme.tycho.plugin and adjust the version ranges accordingly.


Create a new JUnit Test Case named com.codeandme.tycho.plugin.ExampleViewTest.java with following content.
package com.example.tycho.plugin;

import static org.junit.Assert.*;

import org.junit.Test;

public class ExampleViewTest {

 @Test
 public void testID() {
  assertEquals("com.example.tycho.views.example", ExampleView.VIEW_ID);
 }
}
I admit, this is a very stupid test, but this tutorial is not about writing good unit tests, right?

Step 2: Add test fragment to tycho build

Once again convert your fragment project to a maven project. Set Packaging to eclipse-test-plugin and like before add the project to our master pom file.

That's it! Build your product and your tests will automatically run as part of your build process. Try changing the assertion to provoke an error.

The tycho surefire plugin works a bit differently compared to a JUnit Plug-in Test. Surefire will only load plugins that are referenced via dependencies by the test plugin. This means it will start the minimal set of bundles defined by the dependency tree. In contrary JUnit Plug-in Tests will load everything available in your workspace.

Check the documentation to see how to add additional dependencies.

9 comments:

  1. This is great information. Thanks!

    One other thing I'd mention is that Tycho Surefire tests run in the integration-test phase while regular Surefire tests run in the test phase. The integration-test phase occurs between the package and install phases.

    I mention this because I once spent an hour trying to figure out why my tests weren't running when doing an mvn package :-)

    --- Patrick

    ReplyDelete
  2. Hi Christian,

    When I run 'mvn integration-test' it fails because my test classes could not be found, I've even added to the POM file:


    **/AllTests.class


    With no avail.

    Thanks & Regards
    Setya

    ReplyDelete
    Replies
    1. Hi Christian,

      After some fiddling around, it can find my test classes, but then it fails for missing dependencies declared in the host bundle.

      Regards,
      Setya

      Delete
    2. See the last paragraph for an explanation. Your test fragment needs to declare all dependencies you need to run the test. Alternatively you may declare these dependencies in your test pom. This is different to how RCP Plugin tests work from the IDE. They automatically import everything that is available.

      Delete
    3. Hi Christian,

      Dependencies have been added to test pom, but Tycho still complains for not finding them, I've even added pomDependencies=consider to configuration of target-platform-configuration plugin in the parent pom with no luck.

      Adding dependencies to the MANIFEST.MF works though, but I prefer not to touch it.

      Regards,
      Setya

      Delete
    4. if you added dependency-resolution nodes to the target platform (as described in the link in the tutorial) with no luck you should best pose your question to the tycho-users mailing list.

      Delete
  3. Hi Christian,

    Is it possible to put test classes in a plain Java project and run them outside of OSGI env using Maven ?

    Regards,
    Mike

    ReplyDelete
    Replies
    1. Never did that, but I guess the surefire plugin can handle that. However in the context of an eclipse application this makes no sense. You would be missing your test target.

      Delete
  4. In the pom.xml of the .tests fragment, what should the parent pom be? Should it be the parent pom of the multimodule project, or should it be the host plugin of the fragment?

    ReplyDelete