13.1. Writing a Thucydides tags plugin

Thucydides tags are easy to integrate with other applications, such as issue tracking or agile project management systems. In this section, we look at how to write a plugin that will let Thucydides automatically assign tags to your tests, based on your specific requirements.

In Thucydides, tags are arbitrary tuples of String values (name and type), represented by the TagType class. You can create a tag using the TagTest.withName() method, as shown here:

  TestTag specialFeatureTag = TestTag.withName("special feature").andType("feature");

Any feature types you provide will be displayed as separate tabs at the top of the reports screen, and will provide all of the usual aggregation and filtering features that come with the standard reports.

To define your own tags, you need to write your own tag provider, by implementing the TagProvider interface, shown below:

  public interface TagProvider {
      Set<TestTag> getTagsFor(final TestOutcome testOutcome);
  }

The unique method of this interface, getTagsFor(), takes a TestOutcome object, and returns the set of tags associated with this test outcome. The TestOutcome class provides a large number of fields describing the test and it’s results. For example, to obtain the list of the issues specified for this test using the getIssues() method. The following code is an example of a tag provider that provides a list of tags based on the test’s associated issues (specified by the @Issue and @Issues annotations).

  import ch.lambdaj.function.convert.Converter;
  import net.thucydides.core.model.TestOutcome;
  import net.thucydides.core.model.TestTag;
  import java.util.Set;
  import static ch.lambdaj.Lambda.convert;

  public class IssueBasedTagProvider implements TagProvider {

      public IssueBasedTagProvider() {
      }

      public Set<TestTag> getTagsFor(final TestOutcome testOutcome) {

          Set<String> issues = testOutcome.getIssues();
          return Sets.newHashSet(convert(issues, toTestTags()));
      }

      private Converter<String, String> toTestTags() {
          return new Converter<Object, TestTag>() {

              @Override
              public TestTag convert(String issue) {
                  String tagName = getNameForTag(issue);
                  String tagType = getTypeForTag(issue);
                  return TestTag.withName(tagName).andType(tagType);
              }
          };
      }

      String getNameForTag(String issue) {...}
      String getTypeForTag(String issue) {...}
  }

You also need to provide a service definition in the /META-INF/services folder on the classpath, so that Thucydides can register and use your plugin. A simple way to do this is to create a Maven project with a file called net.thucydides.core.statistics.service.TagProvider in the src/resources/META-INF/sevices folder. This file is a text file containing the fully-qualified name of your tag provider, e.g.

  com.mycompany.thucydides.MyThucydidesTagProvider

Now just include the generated JAR file in your dependencies, and Thucydides will use it automatically to include your custom tags in the reports.