构建 Cucumber 报告后如何执行一些代码?

How to execute some code after Cucumber report is built?

我将 Cucumber 用于 jUnit 运行ner 运行 BDD 测试如下:

@RunWith(Cucumber.class)
@CucumberOptions(
    format = {"pretty", "json:target/cucumber.json"},
    glue = {"com.company.bdd.steps"},
    features = {"classpath:bdd-scenarios"},
    tags = {"~@skip"}
)
public class CucumberTests {
}

我想要 HTML 来自 https://github.com/damianszczepanik/cucumber-reporting

的漂亮报告

我制作了 jUnit @AfterClass 方法:

@AfterClass
public static void buildReport() throws Exception {
    List<String> srcReportJson = Collections.singletonList("target/cucumber.json");
    Configuration configuration = new Configuration(new File("target"), "AEOS BDD Integration Tests");
    new ReportBuilder(srcReportJson, configuration).generateReports();
}

问题是当@AfterClass方法执行时cucumber.json是空的。因此我无法构建漂亮的 HTML 报告。

在 Cucumber json 报告已经构建之后,是否有任何钩子可以用来执行一些代码?

PS:使用了 Cucumber v.1.1.8 和 Java 1.7,所以我无法尝试 ExtendedCucumberRunner

您是否考虑过添加关闭钩子? Here is 有关如何添加一个的示例。 run() 方法中的代码应该在 JVM 关闭之前执行。

你可以看一下 cucumber 的自定义格式化程序:

wjpowell posted this suggestion in the cucumber-jvm issues:

“您不需要在 Cucumber 中执行此操作。在用于 运行 Cucumber 测试的 JUnit 测试中使用 @beforeclass 和 @afterclass 注释。这样做的好处是 运行仅针对路径或标签选项指定的功能。

@RunWith(Cucumber.class)
@Cucumber.Options(format = {"html:target/cucumber-html-report", "json-pretty:target/cucumber-json-report.json"})
public class RunCukesTest {

    @BeforeClass
    public static void setup() {
        System.out.println("Ran the before");
    }

    @AfterClass
    public static void teardown() {
        System.out.println("Ran the after");
    }
}

"

感谢您的建议,但我只是决定使用现有的 Maven plugin 并在测试目标后立即执行它的目标。