【问题标题】:Using a constant class to create test descriptions for Testng test description使用常量类为 Testng 测试描述创建测试描述
【发布时间】:2017-12-01 18:07:32
【问题描述】:

我们的自动化框架是用 Java 编写的并使用 Testng。我们使用@Test 注释来标记我们的测试,并为我们用 Gherkin 编写的测试提供组和描述。我们使用 Serenity 创建的测试报告

我一直在尝试创建一个静态常量类,我们可以使用它来构建测试描述,以便它处理 HTML 标记,而不是将其放入 String 本身,这样它更易于阅读,因此任何 HTML 格式测试报告的使用可以在这个类中完成,而不是每个

例如:

@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() {
    ...
    }

实际上看起来像:

@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() {
...
}

我目前拥有的代码:

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;
    }
}

【问题讨论】:

标签: java testng


【解决方案1】:

注解属性值在 Java 中必须是编译时常量。请参阅 Java 文档中的 allowed expressions

在TestNG中,描述属性的格式化可以通过创建自定义TestNG @Test annotation transformer来完成。

示例代码:

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);
            }
        }
    }
}

然后方法会被这样注释:

@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() {
    //...
}

注意:在实现自定义 TestNG 监听器时,我们需要确保它们被 TestNG 使用。 (可以在 TestNG XML 测试套件或through other means 中指定监听器。)

这种情况下的结果描述是:

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

我希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多