3

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

Kirill
  • 6,762
  • 4
  • 51
  • 81

4 Answers4

3

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.

Mikhail
  • 665
  • 4
  • 16
  • Yeah, it worked in `@AfterClass`. I see in console that it also throws ` java.lang.IllegalStateException: Shutdown in progress` when log4j inside of reporter tries to register another shutdownhook, but report is built :) – Kirill Jun 09 '17 at 08:33
2

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

Viet Pham
  • 214
  • 2
  • 5
1

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

Kirill
  • 6,762
  • 4
  • 51
  • 81
0

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

"

MikeJRamsey56
  • 2,779
  • 1
  • 20
  • 34
  • I tried jUnit's `@AfterClass`, but it is invoked too early, the Cucumber JSON is empty at this moment :S – Kirill Jun 09 '17 at 08:02
  • Have you tried waiting for it to not be empty? See this [SO Q&A](https://stackoverflow.com/questions/23452527/watching-a-directory-for-changes-in-java). – MikeJRamsey56 Jun 09 '17 at 17:39