Some time ago in 2011, I asked a question in stackoverflow about test report enrichment of JUnit tests. Because I was asked several times on this topic afterwards, and today again, I write down the solution Ideveloped and used that time. Currently, I do not have a better idea, yet.
The issue to solve
For a customer detailed test reports are needed for integration tests which not only show, that everything is pass, but also what the test did and to which requirement it is linked to. These reports should be created automatically to avoid repetitive tasks to save money and to not frustrate valuable developers and engineers.
For that, I thought about a way how to document the more complex integration tests (which is also valuable for component tests). A documentation would be needed for classes and test methods. For the colleagues responsible for QA it is a good help to see to which requirement, Jira ticket or whatever the test is linked to and what the test actually tries to do to double check it. If the report is good enough, it may also be sent out to the customers to prove quality.
The big question now is: How can the documentation for each method and each test class put into the JUnit reports? JUnit 4.9 was used at the beginning of this issue with Maven 3.0 and not I am on JUnit 4.11 and Maven 3.1.0.
The solution
The JUnit listener was attached to maven-surefire-plugin like:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>2.12</version> <configuration> <properties> <property> <name>listener</name> <value>fully.qualified.class.name.to.your.listener</value> </property> </properties> </configuration> </plugin>
@Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.TYPE, ElementType.METHOD}) @Documented public @interface TestDoc { public String testId(); public String rationale(); public String bugId() default ""; /* Put in what you need... */ }
@TestDoc(testId="123", rationale="Assure handling of invalid data does not cause a crash.", bugId="#42")
public class InvalidStreamingTests {
@Test
@TestDoc(testId="124", rationale="Check for stream which aborts and leads to incomplete content.")
public void testStreamAbort()
{
/* The actual test code */
}
}
String testClassName = description.getClassName();