top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Automating JIRA Issue Creation from Cucumber Test Failures

Introduction

Ensuring code quality and reliability is crucial in the software development lifecycle. This is where test automation tools like Cucumber come into play.


Cucumber is a powerful tool used in software development for Behavior Driven Development (BDD). BDD is a collaborative approach that improves communication between developers, testers, and business stakeholders by creating a shared understanding of how the application should behave.


Key Features of Cucumber:

  • Readable Specifications: Cucumber uses plain language, known as Gherkin, to write test scenarios. This makes the tests easy to understand for non-technical stakeholders.

  • Automation Framework: It integrates with various testing frameworks and languages, such as Java, Ruby, and JavaScript, to automate the execution of these tests.

  • Example-Driven Development: It promotes writing examples of expected outcomes before development begins, ensuring that the software meets user expectations.

  • Improves Collaboration: By involving all team members in the process, Cucumber ensures that everyone has a clear understanding of the requirements and expected behavior of the application.


JIRA, on the other hand, is a robust issue and project tracking tool widely used in agile software development. It helps teams plan, track, and manage their work effectively..


Key Features of JIRA:

  • Issue Tracking: JIRA allows teams to create, update, and track issues or tasks through their lifecycle, from creation to resolution.

  • Agile Project Management: It supports agile methodologies like Scrum and Kanban, providing tools for sprint planning, backlog grooming, and tracking progress through boards and charts.

  • Customization: JIRA can be customized to fit the needs of any project with custom workflows, fields, and issue types.

  • Integration: It integrates seamlessly with other tools in the development ecosystem, such as Confluence, Bitbucket, and various CI/CD tools.

  • Reporting: JIRA offers powerful reporting capabilities, allowing teams to generate reports on various metrics, such as velocity, burndown, and cycle time, to monitor and improve their processes.

Combining these tools, we can automate the creation of JIRA issues whenever a Cucumber test fails. This is done by configuring JIRA to accept issue creation via email and then programming our test automation setup to send an email with the failure details.


In this blog post, we will go through the step-by-step process of setting up this automation.


Prerequisites

  • A JIRA account with email integration enabled.

  • A working Java-based Cucumber test setup.

  • A Java utility code to send an email

  • An SMTP server or email service to send emails for example using a gmail account.


Step 1: Configure JIRA to Create Issues from Emails

1.      Enable Email Integration in JIRA:


  • Log in to your JIRA account.

  • Navigate to JIRA Administration > System > Incoming Mail.

  • Set up a new mail handler. Provide the email address JIRA will monitor for new issues.

  • Choose the "Create a new issue or add a comment to an existing issue" option.

  • Configure the necessary settings (server details, port, etc.) and test the connection.  


Below is the JIRA account configuration screenshot enabling above settings. Please refer JIRA documentation for instructions and recommended secure best practices to creating JIRA Issue.



 

Step 2: Configure Gmail account to Create Issues from Emails

Below is an option to configure a Gmail account to use in Java code to send an email. Please refer the detailed steps and instructions from GMAIL documentation for latest information and recommended secure best practices to send an email using Gmail account.


Use an App Password (Recommended for Accounts with 2-Step Verification)

  • Go to your Google Account.

  • Click on Security in the left sidebar.

  • Under Signing in to Google, click on App passwords.

  • Re-sign in again if required.

  • Select Mail as the app and Other as the device, then give it a name (e.g., Java Mail).

  • Click on Generate.

  • Copy the 16-character app password displayed. This password will be used in place of your regular Gmail password in the Java Mail code.

 

Step 3: Create Java Utility to send Emails

 

Here is an example Email Utility code to send an email using Gmail account configured in the above step using the email and app password

 

import javax.mail.*;

import javax.mail.internet.*;

 

public class EmailUtil {

 

    public static void sendEmail(String subject, String body, String jiraAccountEmail) {

        final String username = "sender_email@gmail.com";

        final String password = "App Password configured at Gmail account";

 

        Properties props = new Properties();

        props.put("mail.smtp.auth", "true");

        props.put("mail.smtp.starttls.enable", "true");

        props.put("mail.smtp.host", "smtp.gmail.com");

        props.put("mail.smtp.port", "587");

 

        Session session = Session.getInstance(props, new javax.mail.Authenticator() {

            protected PasswordAuthentication getPasswordAuthentication() {

                return new PasswordAuthentication(username, password);

            }

        });

 

        try {

            Message = new MimeMessage(session);

            message.setFrom(new InternetAddress("sender_email@gmail.com"));

            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(jiraAccountEmail));

// jiraAccountEmail example - jira@jiratest.atlassian.net

            message.setSubject(subject);

            message.setText(body); // set issue description deatils

            Transport.send(message);

            System.out.println("Email sent successfully!");

 

        } catch (MessagingException e) {

                // Handle exception

            throw new RuntimeException(e);

        }

    }

}

 

Step 4: Integrate Email sending into failed Cucumber test scenario

Create a cucumber hook to send an email when there is a failed test scenario. Screenshot also can be attached to JIRA issue by sending the screenshot as attachment to the JIRA account email.


Find below sample reference Hooks java code to send email


public class FailedCaseScreenShot extends BaseClass {

 

                @After(order=0)

                public void tearDown_close(Scenario sc) {

 

                                                String screenshotname = sc.getName().replaceAll(" ", "-");

                                                if (sc.isFailed()) {

                                                                sc.log("This is my failure message");

                                                                byte[] screenshot = ((TakesScreenshot) DriverFactory.getDriver()).getScreenshotAs(OutputType.BYTES);

                                                                sc.attach(screenshot, "image/png", screenshotname);

                                                                EmailUtil.sendEmail(sc.getName(),screenshotname, screenshot);

                                                }

                                }catch (Exception e) {

                                                System.out.println("Failed Case Screenshot get message...."+e.getMessage());

                                                // Handle exception

                                               

                                }              

                }

}

 

Step 5: Run the Cucumber Tests

Execute the Cucumber tests. If a test fails, the hook as coded above will trigger and send an email (sender gmail) to configured JIRA account (Recipient), which in-turn creates JIRA issue .


Conclusion

By following these steps, JIRA issue can be created automatically for failed Cucumber test cases via email. This integration helps in maintaining a robust testing and issue tracking process, ensuring that failures are promptly recorded and addressed.

 

Automation in testing increases efficiency. Also ensures consistency and reliability in the software development lifecycle. Cucumber's BDD capabilities combined with JIRA's project management strengths creates a powerful synergy for any development team.

61 views0 comments

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page