How to get the exception that was thrown when a Cucumber test failed in Java?

前端 未结 7 815
攒了一身酷
攒了一身酷 2021-01-13 02:07

I can perform actions on test failure by using:

@After
public void afterTest(Scenario scenario) {
    if (scenario.isFailed()) {
        /*Do stuff*/
    }
}         


        
7条回答
  •  感动是毒
    2021-01-13 02:43

    If you just want to massage the result being sent to the report then you can extend the CucumberJSONFormatter and override the result method like this:

    public class CustomReporter extends CucumberJSONFormatter {
    
        CustomReporter(Appendable out) {
            super(out);
        }
    
        /**
         * Truncate the error in the output to the testresult.json file.
         * @param result the error result
        */
        @Override
        void result(Result result) {
            String errorMessage = null;
            if (result.error) {
                errorMessage = "Error: " + truncateError(result.error);
            }
            Result myResult = new Result(result.status, result.duration, errorMessage);
            // Log the truncated error to the JSON report
            super.result(myResult);
        }
    }
    

    Then set the plugin option to:

    plugin = ["com.myimpl.CustomReporter:build/reports/json/testresult.json"]
    

提交回复
热议问题