0

Our automation framework is written in Java and uses Testng. We use the @Test annotation to mark out our tests and provide the groups and a description for the test which we write in Gherkin. We use the test report created by Serenity

I have been trying to create a static constant class that we can use to build a test description so that it handle HTML tags rather than putting this into the String itself so its easier to read and so any HTML format that the test report uses can be done within this class rather than each

For example:

@Test( groups = { TestGroup.One, TestGroup.TWO }, description = 
                "<b>Given</b> I am in Scenario One</br>" +
                "<b>When</b>I do something</br>" +
                "<b>Then</b>I get a result")
        protected void testScript() {
    ...
    }

would actually look like:

@Test( groups = { TestGroup.One, TestGroup.TWO }, description = 
            TestDescription
                .given("I am in Scenario One")
                .when("I do something")
                .then("I get a result");)
    protected void testScript() {
...
}

The code I currently have:

public final class TestDescription {

    private static String description = "";

    public static String given(final String given) {

        return "<b>Given</b>" + given + "</br>";
    }

    public static TestDescription when(final String when) {

        description = description + "<b>When</b>" + when + "</br>";
        return null;
    }

    public static TestDescription then(final String then) {

        description = description + "<b>Then</b>" + then + "</br>";
        return null;
    }

    public static TestDescription and(final String and) {

        description = description + "<b>And</b>" + and + "</br>";
        return null;
    }

    public static String build() {

        return description;
    }
}

1 Answers1

2

Annotation attribute values must be compile-time constants in Java. See allowed expressions in Java documentation.

In TestNG the formatting of description attribute can be done through creating a custom TestNG @Test annotation transformer.

Sample code:

public class MyAnnotationTransformer implements IAnnotationTransformer {

    private static final String GIVEN = "given: ";
    private static final String WHEN = "when: ";
    private static final String THEN = "then: ";

    @Override
    public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
        if (testMethod != null) {
            String description = annotation.getDescription();
            if (description != null && !description.isEmpty()) {
                StringBuilder sb = new StringBuilder();
                for (String item : description.split("\\|")) {
                    if (item.startsWith(GIVEN)) {
                        sb.append("<b>Given</b> ").append(item.substring(GIVEN.length())).append("<br />");
                    } else if (item.startsWith(WHEN)) {
                        sb.append("<b>When</b> ").append(item.substring(WHEN.length())).append("<br />");
                    } else if (item.startsWith(THEN)) {
                        sb.append("<b>Then</b> ").append(item.substring(THEN.length())).append("<br />");
                    } else {
                        sb.append(item).append("<br />");
                    }
                }
                description = sb.toString();
                annotation.setDescription(description);
            }
        }
    }
}

Then the method would be annotated like this:

@Test( groups = { TestGroup.ONE, TestGroup.TWO }, description =
        "given: I am in Scenario One" +
        "|when: I do something" +
        "|then: I get a result")
protected void testScript() {
    //...
}

Note: when implementing custom TestNG listeners, we need to make sure theyare used by TestNG. (Listeners can be specified in the TestNG XML test suite or through other means.)

The resulting description in this case would be:

<b>Given</b> I am in Scenario One<br /><b>When</b> I do something<br /><b>Then</b> I get a result<br />

I hope this helps.

Edvins
  • 406
  • 5
  • 9