allure-framework/allure-java

๐Ÿž: Not able to update statusDetails

IlyaHarlamov opened this issue ยท 2 comments

What happened?

Hello!

I am not able to update the status details of the test case.
My goal is to update the trace of the failing tests (add additional information before or after the actual trace). It will help me to implement better categories in the Allure report.

Here is what I have tried:

  1. With Cucumber's @after hook
@After()
public void updateTrace(Scenario scenario) {
    Allure.getLifeCycle().updateTestCase(scenario.getId(), testResult -> {
        testResult.getStatusDetails().setTrace("New Trace");
    }
}

// This approach fails, because at the moment of updating, the statusDetails is null, so there is a NullPointerException
image

I have also tried with testResult.setStatusDetails(new StatusDetails().setTrace("NewTrace"));, in this case there is no error, but in the generated report, the change is not applied.

  1. Implementing the Cucumber's ConcurrentEventListener and using it as a plugin in Cucumber.
public class TestListener implements ConcurrentEventListener {
    @Override
    public void setEventPublisher(EventPublisher publisher) {
        publisher.registerHandlerFor(TestCaseStarted.class, this::handleTestCaseFinished);
    }

    private void handleTestCaseFinished(TestCaseFinished event) {
        // the same code as in first approach, trying to modify Allure's Lifecycle
    }
}

// Same issue here as in the first approach. I have also tried to modify the trace in the the test case's result .json file from my TestListener, however, the file is not generated yet at this stage.

I would appreciate to hear any suggestions or workarounds if you have any in mind!

Thanks!

What Allure Integration are you using?

allure-cucumber7-jvm, allure-junit5, allure-selenide

What version of Allure Integration you are using?

2.28.1

What version of Allure Report you are using?

2.30.0

Code of Conduct

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

I have managed to achieve the desired outcome implementing TestLifecycleListener. Wasn't aware of this interface, just found it out by browsing other issues.

For anyone looking for the same issue, here is what you can do:

  1. Implement the TestLifecycleListener
import io.qameta.allure.listener.TestLifecycleListener;
import io.qameta.allure.model.Status;
import io.qameta.allure.model.StatusDetails;
import io.qameta.allure.model.TestResult;

public class MyTestLifecycleListener implements TestLifecycleListener {
    @Override
    public void afterTestStop(TestResult testResult) {
        if (!testResult.getStatus().equals(Status.PASSED)) {
            StatusDetails statusDetails = testResult.getStatusDetails();
            statusDetails.setTrace("Custom Trace :). " + statusDetails.getTrace()); 
        }
    }
}
  1. Register the listener
  • Create the file io.qameta.allure.listener.TestLifecycleListener under the path src/test/resources/META-INF/services.
  • Add the fully qualified name of your listener to the file (e.g. com.example.MyTestLifecycleListener)
baev commented

Using TestLifecycleListener is the way to go. You can't update test results before/after hooks and usually can't use the same events that Allure uses for reporting. Allure will close the context before your listener if the extension's execution order isn't guaranteed.