【问题标题】:View the number of passed/failed tests in Java Junit查看 Java Junit 中通过/失败的测试数量
【发布时间】:2021-12-22 08:29:27
【问题描述】:

我想在 Java Junit 和 Cucumber 中存储通过/失败测试的数量。 我有以下代码:

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

}

但是当我运行测试时,我收到 Passed: 0 Failed: 0。

【问题讨论】:

    标签: java junit cucumber


    【解决方案1】:

    您可以在黄瓜中实现EventListenerConcurrentEventListener,如下所示,然后处理TestCaseFinished 事件。此示例正在处理 TestCaseFinishedTestStepStarted 事件。 TestCaseFinished 事件有 getResult 方法。从那里你可以建立逻辑来计算通过/失败

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

    要运行上述类 - 将类与您的步骤定义或支持类放在一起,然后在 junit-platform.properties(对于 Junit5)中提及这样的插件

    cucumber.plugin = com.test.support.ReportPlugin

    对于 Junit4,您可能需要将插件添加到您的运行器类

    当您运行测试时,您应该会在控制台上看到打印的所有内容

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      • 2014-11-24
      • 2011-03-22
      • 2014-02-09
      • 2013-11-08
      相关资源
      最近更新 更多