Kirill
Kirill

Reputation: 8056

How to execute some code after Cucumber report is built?

I use Cucumber for jUnit runner to run BDD tests like this:

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

I would like to have beautiful HTML reports from https://github.com/damianszczepanik/cucumber-reporting

And i made jUnit @AfterClass method:

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

The problem is that cucumber.json is empty when @AfterClass method executes. Hence i can't build pretty HTML report.

Is there any hook which i can use to execute some code after cucumber json report is already built?

PS: Cucumber v.1.1.8 is used and Java 1.7 so i was not able to try ExtendedCucumberRunner

Upvotes: 3

Views: 3574

Answers (4)

Kirill
Kirill

Reputation: 8056

Thank you for your suggestions but I just decided to use already existing Maven plugin and execute it's goal right after test goal.

Upvotes: 1

MikeJRamsey56
MikeJRamsey56

Reputation: 2819

wjpowell posted this suggestion in the cucumber-jvm issues:

"You don't need to do this in cucumber. Use the @beforeclass and @afterclass annotation from within the JUnit test used to run the cucumber tests. This has the benefit of running only for the features specified by the paths or tags options.

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

"

Upvotes: 0

Viet Pham
Viet Pham

Reputation: 214

You can take a look at custom formatter of cucumber: gherkin.formatter class

Upvotes: 2

Mikhail
Mikhail

Reputation: 665

Have you considered adding shutdown hook? Here is an example on how to add one. Code in run() method supposed to be executed before JVM shuts down.

Upvotes: 3

Related Questions