Integrate JIRA with Cucumber on Fail Test Case

Viewed 186

I had an example of code in github that create jira ticket when automation test case failed. It collect the ITestResult information and used that to create ticket.

I can use @After on each test scenario but that is too many repetitive code. I want to execute once in the final test execution - something in the hooks or listener.

Questions: Is there any similar mechanism in cucumber to simulate ITestResult? Is it possible to use QAF library?

@Override
    public void onTestFailure(ITestResult result) {
            JiraPolicy jiraPolicy = result.getMethod().getConstructorOrMethod().getMethod().getAnnotation(JiraPolicy.class);
            boolean isTicketReady = jiraPolicy.logTicketReady();
            if (isTicketReady) {
                // raise jira ticket:
                System.out.println("is ticket ready for JIRA: " + isTicketReady);
                JiraServiceProvider jiraSp = new JiraServiceProvider("https://naveenautomationlabs.atlassian.net",
                        "naveenanimation20@gmail.com", "jaizMlfaUkTfffNOVPq29B29", "TA");
                String issueSummary = result.getMethod().getConstructorOrMethod().getMethod().getName()
                        + "got failed due to some assertion or exception";
                String issueDescription = result.getThrowable().getMessage() + "\n";
                issueDescription.concat(ExceptionUtils.getFullStackTrace(result.getThrowable()));
    
                jiraSp.createJiraTicket("Bug", issueSummary, issueDescription, "Naveen");
            }
    
        }

Please help me. Thanks.

1 Answers

QAF tries to provide seamless user experience and features regardless of test authoring approach used. This case is use of meta-data with test case.

When authoring test case in BDD approach, BDD syntax has meta-data statement that can be used. BDD2 syntax, meta-data can be provided as separate key value pair: Example BDD syntax:

Scenario: scenario in BDD syntax
Meta-data: {'logTicketReady':true,'groups':['smoke','p1']}
   Given ...
   When ...
End

Example BDD2:

@smoke @p1
@logTicketReady:true
Scenario: scenario in BDD2 syntax
   Given ...
   When ...

When authoring test case in java meta data can be provided with custom annotation (for example in your case JiraPolicy) or Metadata annotation.

Example Java:

@MetaData("{'logTicketReady':true}")
@Test(groups={"smoke","p1"})
public void sampleJavaTest(){
  //implementation goes here
}

If you have created custom annotation, you can use it as well.

Example Java with custom annotation:

@JiraPolicy(logTicketReady=true)
@Test(groups={"smoke","p1"})
public void sampleJavaTest(){
  //implementation goes here

}

For execution, use appropriate factory class in xml configuration.

The method you shared can be modified as below to work with both Coded and BDD approach.

@Override
public void onTestFailure(ITestResult result) {
        TestNGScenario scenario = (TestNGScenario) result.getMethod();
        Map<String, Object> metadata = scenario.getMetaData();
      
        boolean isTicketReady = (boolean)metadata.getOrDefault("logTicketReady",false);
        if (isTicketReady) {
            // raise jira ticket:
            System.out.println("is ticket ready for JIRA: " + isTicketReady);
            JiraServiceProvider jiraSp = new JiraServiceProvider("https://naveenautomationlabs.atlassian.net",
                    "naveenanimation20@gmail.com", "jaizMlfaUkTfffNOVPq29B29", "TA");
            String issueSummary = metadata.get("name")
                    + "got failed due to some assertion or exception";
            String issueDescription = result.getThrowable().getMessage() + "\n";
            issueDescription.concat(ExceptionUtils.getFullStackTrace(result.getThrowable()));

            jiraSp.createJiraTicket("Bug", issueSummary, issueDescription, "Naveen");
        }

    }

Note: Cucumber is not mandatory when using QAF. Still if you want to use standard Cucumber runner or have cucumber-5 step implementation you need to use qaf-cucumber. If you have step implementation in cucumber 1.x refer documentation

Related