0

I want to store the number of passed/failed tests in Java Junit and Cucumber. I have the following code:

public class CucumberE2ETest {

static int passedTest = 0;
static int failedTests = 0;

@Before
public void before() {
    System.out.println("Before tests");
}

@AfterEach
public void afterScenario(Scenario scenario) {
    if (scenario.isFailed()) {
        failedTests++;
    }
    else {
        passedTest++;
    }
}

@After
public static void teardown() {
    System.out.println("Passed: " + passedTest + " Failed: " + failedTests);
}

}

But I receive Passed: 0 Failed: 0, when I run my tests.

1 Answers1

1

You can Implement an EventListener or ConcurrentEventListenerlike below in cucumber and then handle TestCaseFinished events. This example is handling TestCaseFinished and TestStepStarted events. TestCaseFinished event has getResult method . From there you can build up the logic to count pass/fail

    import io.cucumber.plugin.EventListener;
    import io.cucumber.plugin.event.*;
    
    import java.net.URI;
    import java.util.Map;
    import java.util.TreeMap;
    import java.util.UUID;
    
    public class    ReportPlugin implements EventListener {
    
     private final Map<String, UUID> startedSteps = new TreeMap<String, UUID>();
     private final Map<String, Status> finishedCases = new TreeMap<String, Status>();
    
       @Override
        public void setEventPublisher(EventPublisher publisher) {
            
            publisher.registerHandlerFor(TestStepStarted.class, this::handleTestStepStarted);
        publisher.registerHandlerFor(TestCaseFinished.class, this::handleTestCaseFinished);

        }
    
    private void handleTestStepStarted(TestStepStarted event) {
            startedSteps.put(event.getTestStep().toString(), event.getTestStep().getId());
            for (Map.Entry<String, UUID> entry : startedSteps.entrySet()) {
                    String location = entry.getKey();
                    UUID uuid = entry.getValue();
                    System.out.println(location + " ###fromTestStepStarted### " + uuid);
            
             //above prints
            //io.cucumber.core.runner.PickleStepTestStep@5a5c128 ###fromTestStepStarted### 7f964f1c-9442-43fc-97e9-9ec6717eb47f
           // io.cucumber.core.runner.PickleStepTestStep@77b919a3 ###fromTestStepStarted### a5d57753-aecb-40a0-a0cf-76bef7526dd8

                }
        }

      //If you would like to get each test step text you do this

    private void handleTestCaseFinished(TestCaseFinished event) {
        
        
        TestCase testCase = event.getTestCase();
        String scenarioName = testCase.getName();

        TestStep testStep = testCase.getTestSteps().get(0);
        if (testStep instanceof PickleStepTestStep) {
            PickleStepTestStep pickleStepTestStep  = (PickleStepTestStep) testStep;
            String text = pickleStepTestStep.getStep().getText();
            System.out.println("****Pickle Step TestStep*****"+  text);

           //above prints
          //****Pickle Step TestStep*****I open the site ""  
            }

      
        }

   }

To run the above class - put the class with your step defs or your supporting classes and then in junit-platform.properties (for Junit5) mention plugin like this

cucumber.plugin = com.test.support.ReportPlugin

For Junit4 you might have to add plugin to your runner class

When you run your tests you should see everything printed on console

user1207289
  • 3,060
  • 6
  • 30
  • 66