Running Junit5, Serenity and Cucumber from main class does not generate a report

68 Views Asked by At

I have a test project that runs e2e tests and until now I built a .jar file, upload it to a test environment and then executed the tests against a test environment. The tests run serenity with cucumber. I would like to migrate the project now to junit5. The tests are started in a main method like this:

  public static void main(String args[]) throws IOException {

    Launcher launcher = LauncherFactory.create();
    SummaryGeneratingListener summaryGeneratingListener = new SummaryGeneratingListener();
    LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
            .selectors(selectClass(AllTests.class))
            .build();
    launcher.execute(
            request,
            summaryGeneratingListener);

    boolean wasSuccessful = summaryGeneratingListener.getSummary().getFailures().isEmpty();
    
    HtmlAggregateStoryReporter reporter = new HtmlAggregateStoryReporter("default");
    File reportTarget = new File("target");
    reporter.setOutputDirectory(reportTarget);
    reporter.setGenerateTestOutcomeReports();
    reporter.generateReportsForTestResultsFrom(reportTarget);

    if (!wasSuccessful) {
      System.exit(1);
    } else {
      if (isAutomaticRetryThresholdBreached()) {
        System.exit(1);
      }
      System.exit(0);
    }
  }

This code executes the tests but does not generate a HTML report with serenity.

I managed to generate serenity reports with maven-surefire-plugin, but I would like to run tests as a jar. Do you know how to edit this code so that it generates a report?

1

There are 1 best solutions below

0
user1819111 On

I solved it with using the main method from serenity-cucumber.jar file. Here the example:

public class Main {

public static void main(String[] args) throws Throwable {
    RuntimeOptions propertiesFileOptions = (new CucumberPropertiesParser()).parse(CucumberProperties.fromPropertiesFile()).build();
    RuntimeOptions environmentOptions = (new CucumberPropertiesParser()).parse(CucumberProperties.fromEnvironment()).build(propertiesFileOptions);
    RuntimeOptions systemOptions = (new CucumberPropertiesParser()).parse(CucumberProperties.fromSystemProperties()).build(environmentOptions);
    CommandlineOptionsParser commandlineOptionsParser = new CommandlineOptionsParser(System.out);
    RuntimeOptions runtimeOptions = commandlineOptionsParser.parse(args).addDefaultGlueIfAbsent().addDefaultFeaturePathIfAbsent().addDefaultSummaryPrinterIfNotDisabled().enablePublishPlugin().build(systemOptions);

    Runtime runtime = CucumberWithSerenityRuntime.using(Main.class::getClassLoader, runtimeOptions);

    try {
        runtime.run();
    } finally {
        // byte exitStatus = net.serenitybdd.cucumber.cli.Main.run(args, ClassLoaders::getDefaultClassLoader);
        HtmlAggregateStoryReporter reporter = new HtmlAggregateStoryReporter("default");
        File reportOutput = new File("target");
        File reportInput = new File("target");
        if (!reportOutput.exists()) {
            Files.createDirectory(reportOutput.toPath());
        }
        reporter.setOutputDirectory(reportOutput);
        reporter.setGenerateTestOutcomeReports();
        reporter.generateReportsForTestResultsFrom(reportInput);

        System.exit(runtime.exitStatus());
    }
}

}