allure-java icon indicating copy to clipboard operation
allure-java copied to clipboard

🐞: No execution info when running tests via JUnit5 Launcher API

Open korpus95 opened this issue 1 year ago • 1 comments

What happened?

I run tests this way, but the results show an incomplete report, there is no data with annotations @Step and @Attachment

private static void executeTest(String testIdentifier) {
        LauncherDiscoveryRequest request;

        if (testIdentifier.contains("#")) {
            request = LauncherDiscoveryRequestBuilder.request()
                    .selectors(selectMethod(testIdentifier))
                    .build();
        } else {
            request = LauncherDiscoveryRequestBuilder.request()
                    .selectors(selectClass(testIdentifier))
                    .build();
        }
        System.setProperty("allure.results.directory", "target/test-result/allure-results");
        System.setProperty("junit.jupiter.extensions.autodetection.enabled", "true");

        LauncherConfig launcherConfig = LauncherConfig.builder()
                .enableTestExecutionListenerAutoRegistration(true)
                .build();

        SummaryGeneratingListener summaryGeneratingListener = new SummaryGeneratingListener();

        try (LauncherSession session = LauncherFactory.openSession(launcherConfig)) {
            Launcher launcher = session.getLauncher();
            launcher.registerTestExecutionListeners(summaryGeneratingListener);
            TestPlan testPlan = launcher.discover(request);
            launcher.execute(testPlan);
        }
    }

Can this be fixed?

What Allure Integration are you using?

allure-junit5

What version of Allure Integration you are using?

2.29.0

What version of Allure Report you are using?

2.29.0

Code of Conduct

  • [x] I agree to follow this project's Code of Conduct

korpus95 avatar Apr 05 '25 16:04 korpus95

This is not a bug, but a logical behavior, since allure-java uses aspectj javaagent to work with @Step and @Attachment annotations.

If you look at the documentation in the settings section, you will see that aspectj is called on the command line when you run the surefire plugin - https://allurereport.org/docs/junit5/#configure-aspectj -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar" which your runner is missing.

In my opinion, you have 3 options:

  1. Use manually written Allure.step("Step Name", () -> {}) and Allure.addAttachment("name", "value"), which are not requires aspectj
  • https://allurereport.org/docs/steps/#step-parameters
  • https://allurereport.org/docs/steps/#step-attachments
  1. Add -javaagent to jvm parameters as described in aspectj configuration in maven-surefire-plugin, however the aspectjweaver-version.jar itself must be present in the file system, or there must be any maven/gradle/any package manager that will download aspectj before your call to the Main class But anyway - https://allurereport.org/docs/junit5/#configure-aspectj
  2. I can't recommend this solution because it will cause a lot of problems in the future, but atm you can load javaagent dynamically with byte-buddy-agent before your runner code:
        ByteBuddyAgent.attach(
                new File("/path/to/aspectjweaver-version.jar"),
                ManagementFactory.getRuntimeMXBean().getName().split("@")[0],
                "-Daj.weaving.verbose=true"
        );

a-simeshin avatar Apr 17 '25 18:04 a-simeshin