Fork me on GitHub

Introduction

By annotating your tests with TestLink test results will be written to a XML file which may be imported to a running Testlink instance as described in the user manual .

Another option is to log the start and end of an executed test to an injected Logger.

Usage

Annotating your tests

You have to annotate your tests and provide either an externalId or a numeric internalId

@Test
@TestLink(internalId=1)
public void testPassed() {
    assertTrue(true); // PASSED
}
@Test
@TestLink(externalId="PROJECT-1")
public void testExternalId() {
    assertTrue(true); // PASSED
}

Tests annotated with Ignore will be marked as BLOCKED as well as tests with failing assumptions.

@Test
@TestLink(internalId=2)
@Ignore("Just ignore this")
public void testIgnored() {
    assertTrue(true); // BLOCKED as test is ignored.
}
@Test
@TestLink(externalId="ASSUMPTION_FAILED")
public void testWithFailingAssumption() {
    assumeTrue(false); // BLOCKED as assumption failed.
}

Failing tests or tests in error are marked as FAILED.

@Test
@TestLink(internalId=5)
public void testFailed() {
    assertTrue(false); // FAILED
}

Running tests with the maven-surefire-plugin

You have to configure the surefire plugin to use the additional TestLinkXmlRunListener or TestLinkLoggingRunListener (For more information, take a look into the classes' documentation above.). As can be seen below, username and file-location for the result file or the logger name may be provided as system properties.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.10</version>
            <dependencies>
                <dependency>
                    <groupId>net.oneandone.testlinkjunit</groupId>
                    <artifactId>tljunit-surefire</artifactId>
                    <version>3.0.3-SNAPSHOT</version>
                </dependency>
            </dependencies>
            <configuration>
                <properties>
                    <property>
                        <name>listener</name>
                        <value>net.oneandone.testlinkjunit.tljunit.TestLinkXmlRunListener,net.oneandone.testlinkjunit.tljunit.TestLinkLoggingRunListener
</value>
                    </property>
                </properties>
                <systemPropertyVariables>
                    <testlink.results>target/my-testlink.xml</testlink.results>
                    <testlink.userName>memyselfandi</testlink.userName>
                    <teslink.loggername>MY_LOGGER</teslink.loggername>
                </systemPropertyVariables>
            </configuration>
        </plugin>
    </plugins>
</build>

Now running mvn test will run your tests and put the resulting TestLink XML file into target/my-testlink.xml using memyselfandi as name of the user who executed the test run.

Running tests in Eclipse

To run a test from Eclipse, add a main method which will collect the tests:

package net.oneandone.testlinkjunit.eclipse;

import static org.junit.Assert.assertTrue;

import java.io.FileNotFoundException;

import net.oneandone.testlinkjunit.tljunit.TestLink;
import net.oneandone.testlinkjunit.tljunit.TestLinkXmlRunListener;

import org.junit.Test;
import org.junit.runner.JUnitCore;

public class EclipseIT {

    @Test
    @TestLink(externalId="ECLIPSE_TEST")
    public void test() {
        assertTrue(true);
    }

    public static void main(String[] args) throws FileNotFoundException {
        final JUnitCore core = new JUnitCore();
        core.addListener(new TestLinkXmlRunListener());
        core.run(EclipseIT.class);
    }
}