Overview of Extent Reporting Integration
For automated Java applications, integrating Extent Reporting with Jenkins improves test result visibility, which is essential for continuous integration settings. TestNG, Maven, and Extent Reporter are usually included in this configuration, which is controlled by SureFire and enables nightly builds and thorough reporting.
To include particular statistics in Jenkins email notifications, like as test counts and pass/fail ratios, can be a typical difficulty when utilizing the Extent Reporter HTML dashboard. To efficiently extract these details from the HTML page for automatic distribution, a script or other technique is needed.
| Command | Description |
|---|---|
| groovy.json.JsonSlurper | Used to parse JSON-formatted data in Groovy, making it easier to handle data from JSON files or responses. |
| new URL().text | Reads data straight from web resources by creating a new URL object and fetching the material as text. |
| jenkins.model.Jenkins.instance | Jenkins's singleton pattern can be used to access the instance that is now executing and manipulate job configurations and settings. |
| Thread.currentThread().executable | Used to retrieve a reference to the build or job that is executing at that moment in the Jenkins scripted pipeline, frequently for dynamic handling. |
| hudson.util.RemotingDiagnostics | Permits the running of Groovy scripts on distant Jenkins nodes; this is mostly utilized for script diagnostics. |
| Transport.send(message) | A portion of the JavaMail API that is necessary for notification systems to deliver an email message that has been created in a script. |
Script Implementation Explanation
The supplied scripts are made to automatically retrieve testing data from Jenkins's Extent Reports and email it to yourself as a part of a continuous integration feedback loop. groovy.json.JsonSlurper is the initial command that is crucial for parsing JSON data in the Jenkins environment. This makes it possible for the script to handle JSON files or answers effectively, which is essential for extracting the test results in JSON format from Extent Reports. new URL().text is another important command that is used to view the HTML report of Extent Reports that is hosted on Jenkins. By fetching the HTML information as plain text, this command makes it possible for the script to scrape necessary data, such as the total tests, passed tests, and failed tests.
The data extraction process is further controlled by employing regular expressions to look for particular patterns in the HTML content and determine the numbers corresponding to succeeded, failed, and total tests. The current Jenkins instance is then referenced using the jenkins.model.Jenkins.instance command, which is required to retrieve different job data and configure settings programmatically. The script sends the created email using Transport.send(message) from the JavaMail API after the data has been extracted. This command is essential for informing stakeholders via email of the most recent testing findings, improving communication and reaction times during development cycles. It sends email alerts including the extracted test results.
Data Extraction from Jenkins Extent Reports
Groovy and Java Scripting for Pipelines in Jenkins
import hudson.model.*import hudson.util.RemotingDiagnosticsimport groovy.json.JsonSlurperdef extractData() {def build = Thread.currentThread().executabledef reportUrl = "${build.getProject().url}${build.number}/HTML_20Report/index.html"def jenkinsConsole = new URL(reportUrl).textdef matcher = jenkinsConsole =~ "<span class=\\"param_name\\">\\s*Total Tests:\\s*</span>(\\d+)</br>"def totalTests = matcher ? Integer.parseInt(matcher[0][1]) : 0matcher = jenkinsConsole =~ "<span class=\\"param_name\\">\\s*Passed Tests:\\s*</span>(\\d+)</br>"def passedTests = matcher ? Integer.parseInt(matcher[0][1]) : 0matcher = jenkinsConsole =~ "<span class=\\"param_name\\">\\s*Failed Tests:\\s*</span>(\\d+)</br>"def failedTests = matcher ? Integer.parseInt(matcher[0][1]) : 0return [totalTests, passedTests, failedTests]}def sendEmail(testResults) {def emailExt = Jenkins.instance.getExtensionList('hudson.tasks.MailSender')[0]def emailBody = "Total Tests: ${testResults[0]}, Passed: ${testResults[1]}, Failed: ${testResults[2]}"emailExt.sendMail(emailBody, "jenkins@example.com", "Test Report Summary")}def results = extractData()sendEmail(results)
A Script to Improve Jenkins' Email Notifications
Jenkins Post-Build Actions Using Groovy
import groovy.json.JsonSlurperimport jenkins.model.Jenkinsimport javax.mail.Messageimport javax.mail.Transportimport javax.mail.internet.InternetAddressimport javax.mail.internet.MimeMessagedef fetchReportData() {def job = Jenkins.instance.getItemByFullName("YourJobName")def lastBuild = job.lastBuilddef reportUrl = "${lastBuild.url}HTML_20Report/index.html"new URL(reportUrl).withReader { reader ->def data = reader.textdef jsonSlurper = new JsonSlurper()def object = jsonSlurper.parseText(data)return object}}def sendNotification(buildData) {def session = Jenkins.instance.getMailSession()def message = new MimeMessage(session)message.setFrom(new InternetAddress("jenkins@example.com"))message.setRecipients(Message.RecipientType.TO, "developer@example.com")message.setSubject("Automated Test Results")message.setText("Test Results: ${buildData.totalTests} Total, ${buildData.passed} Passed, ${buildData.failed} Failed.")Transport.send(message)}def reportData = fetchReportData()sendNotification(reportData)
Improvements Made to Jenkins' Automated Reporting
The continuous integration (CI) process is greatly enhanced by utilizing Extent Reports to automate data extraction and email notifications within Jenkins. This methodology gives stakeholders instant access to test results, which guarantees timely updates and promotes proactive issue solutions. The procedure makes use of Jenkins' capabilities to plan and execute automated tests over night. The HTML reports produced by Extent Reporter are then parsed to extract important metrics like the total number of tests, passes, and failures.
Agile development environments require a streamlined feedback process, which is achieved through automated extraction and reporting. Teams can better manage test results and uphold high standards of code quality through continuous monitoring and review by integrating Extent Reports with Jenkins. These procedures are essential for keeping an effective development pipeline running smoothly and making sure that everyone in the team is aware of the most recent testing results and project statuses.
Frequently Asked Questions about Integrating Jenkins Reports
- How can I set up Jenkins so that following a build, an email is sent?
- Using the Email Notification option, you can set this up in the post-build activities of your task configuration.
- In the context of Jenkins, what are Extent Reports?
- An open-source reporting tool called Extent results can be quickly and simply incorporated into Jenkins workflows to provide comprehensive and interactive results on automated tests.
- Is it possible for Jenkins to interface with reporting technologies other than Extent Reports?
- With the appropriate plugins, Jenkins does indeed provide integration with a number of additional reporting tools, including JUnit, TestNG, and others.
- How can I use Jenkins to get test data from an HTML report?
- Typically, you utilize Jenkins' Groovy or Python programming to parse HTML text and retrieve the necessary data.
- What advantages do Jenkins' automated email notifications offer?
- Automated emails help teams maintain continuous deployment procedures and address issues more quickly by giving them instant feedback on build and test statuses.
Concluding Remarks about Automatic Jenkins Reporting
The ability to automatically extract test metrics from Extent Reports and include them into email notifications sent by Jenkins greatly improves a continuous integration pipeline's monitoring capabilities. Teams can take prompt action to improve code and fix failures by using this strategy, which enables teams to receive rapid updates regarding test outcomes. By guaranteeing that all stakeholders are instantly informed about the status of the nightly builds, the streamlined procedure not only saves time but also optimizes resource allocation, maintaining a continuous loop of feedback and development.