Testing Bundles / Plugins with Tycho
There are different ways to test bundles / plug-ins with Tycho:
maven-surefire-plugin
Using maven-surefire-plugin is the preferred way whenever you want to write a plain unit-test. This is a unit test that either:
- doesn't need an OSGi runtime
- uses some kind of mocking technique for OSGi (e.g. Apache Sling OSGi Mocks)
- or starts an embedded OSGi Framework (e.g. osgi-test-framework).
This requires:
- setting up your project using a test-source folder (see below), alternatively using the standard maven layout
- a configured execution of the
maven-surefire-plugin:test
goal - packaging
eclipse-plugin
is used
A sample snippet looks like this:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
...
<build>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire-plugin-version}</version>
<executions>
<execution>
<id>execute-tests</id>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
</build>
</project>
To execute the tests, one has to invoke maven with mvn test
.
The following demo projects are provided as an example:
- Project with a configured source folder: https://github.com/eclipse-tycho/tycho/tree/master/demo/testing/surefire/with-source-folder
- Project using maven standard layout: https://github.com/eclipse-tycho/tycho/tree/master/demo/testing/surefire/with-maven-layout
tycho-surefire-plugin
The tycho-surefire-plugin is the preferred whenever you want to write tests that require an OSGi Framework running and is executed in the integration-test phase of your build, this is similar to what PDE offers as Plugin Tests.
There are two ways to use this:
- You use the
eclipse-test-plugin
packaging, and with those your plugin must only contain test-classes and they will be executed automatically as part of the integration-test phase of your build. This approach is not recommended for new designs. - You use
eclipse-plugin
packaging and configure an additional execution of thetycho-surefire-plugin:plugin-test
goal with either a test-source folder (see below), alternatively using the standard maven layout.
A sample snippet looks like this:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
...
<build>
...
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-surefire-plugin</artifactId>
<version>${tycho-version}</version>
<executions>
<execution>
<id>execute-integration-tests</id>
<goals>
<goal>plugin-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</build>
</project>
To execute the tests, one must invoke maven with mvn verify
, the following demo projects are provided as an example:
- Project with a configured source folder as a standalone project similar to the discouraged
eclipse-test-plugin
packaging: https://github.com/eclipse-tycho/tycho/tree/master/demo/testing/tycho/standalone - Project using maven standard layout having the tests in the same module: https://github.com/eclipse-tycho/tycho/tree/master/demo/testing/tycho/samemodule
bnd-testing
The tycho-surefire-plugin has also support for bnd-testing,
this is like plugin-test
but uses the BND testing framework. There is currently no JDT/PDE equivalent but this integrates nicely with the OSGi Testing Support and allows to execute pre-built test-bundles.
A sample snippet looks like this:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
...
<build>
...
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-surefire-plugin</artifactId>
<version>${tycho-version}</version>
<executions>
<execution>
<id>execute-integration-tests</id>
<goals>
<goal>bnd-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</build>
</project>
To execute the tests, one has to invoke maven with mvn verify
, the following demo projects are provided as an example:
-
Project with maven standard layout having the tests in the same module and using OSGi JUnit5 annotations to automatically inject services:
-
https://github.com/eclipse-tycho/tycho/tree/master/demo/testing/bnd/osgi-test
tycho-test-plugin
The tycho-test-plugin is a new plugin introduced in Tycho 6 to provide unified testing of OSGi bundles. Unlike previous approaches, it is no longer bound to surefire and offers better integration with modern testing frameworks.
junit-platform mojo
The tycho-test:junit-platform
mojo integrates the JUnit Platform Console Launcher into any OSGi Framework.
This approach has several advantages:
- Tycho is completely independent from the used JUnit framework version (since it calls it via a command-line interface)
- Better and more natural integration of selecting test engines in the pom.xml or with the target platform
- You can use any of the JUnit provided test engines or new features that might be added
This requires:
- packaging
eclipse-plugin
is used - a configured execution of the
tycho-test:junit-platform
goal - JUnit Platform dependencies (console launcher and test engines) as test-scoped dependencies
A sample snippet looks like this:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
...
<build>
...
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-test-plugin</artifactId>
<version>${tycho-version}</version>
<executions>
<execution>
<id>execute-tests</id>
<goals>
<goal>junit-platform</goal>
</goals>
</execution>
</executions>
</plugin>
</build>
<dependencies>
<!-- The API is used at compile time of the bundle -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit-version}</version>
<scope>compile</scope>
</dependency>
<!-- The console and the engine are only required at test execution time -->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-console</artifactId>
<version>${junit-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
To execute the tests, one has to invoke maven with mvn verify
. The following demo project is provided as an example: