How To Generate TestNG Reports In Jenkins?

Piyusha

Posted On: February 4, 2021

view count67681 Views

Read time16 Min Read

TestNG is an open-source automated testing framework, where ‘NG’ of TestNG is Next Generation. It is similar to JUnit but designed to be better than JUnit, especially when testing integrated classes. With the help of simple annotations, grouping, sequencing & parametrization, TestNG overcomes most of the older system’s limitations and gives the developer the ability to write more versatile and efficient tests.

In our previous blogs, we have seen how to run Selenium tests in TestNG using an XML file, Parameterisation in TestNG, TestNG listeners, and much more. In this blog, we will find out how to generate a TestNG report in Jenkins. This will be particularly useful if you are into Selenium test automation.

Why Use TestNG in the First Place?

TestNG is quite popular among developers and testers for test creation. It offers several practical features like grouping, dependence, prioritization, ease of using multiple annotations, etc. If you are not a fan of TestNG, allow me to introduce you to a few of the several benefits offered by it, especially from Selenium perspective-

  1. It supports annotations.
  2. It has a flexible test configuration, ensuring that the code is multithread safe.
  3. Support for data-driven testing (with @DataProvider).
  4. Support for parameters.
  5. No more need for TestSuite and accompanies a powerful execution model.
  6. Supports a variety of tools and plugins, which include Jenkins, Eclipse, Maven, etc.
  7. Embeds BeanShell for further flexibility.
  8. It is designed to cover tests (almost all): Unit tests, Functional tests, End-to-end tests, Integration tests, etc.
  9. It uses more Java and OO concepts and features.

Importance of TestNG Annotations

Primarily, developers use annotation repeatedly to define a piece of code that is incorporated into the software or business logic used in the programming language Java to control the flow of methods. In Test Next Generation (TestNG), annotations play a significant role. When working on TestNG, testers need to consider the operations and uses of each annotation.

Although Annotations differ from project to project depending on their requirements, the execution flow remains intact for every single project. Below are different Annotations present in TestNG and their primary usage:

  • @BeforeSuite: The annotated method will run before all tests in this suite have run.
  • @AfterSuite: The annotated method will run after all tests in this suite have run.
  • @AfterTest: The annotated method will run after all the test methods belonging to the classes inside the tag have run.
  • @BeforeTest: The annotated method will run before any test method belonging to the classes inside the tag has run.
  • @AfterGroups: The annotated method is a list of groups that the configuration method will run after. This method will run shortly after invoking the last test method that belongs to any of these groups.
  • @BeforeGroups: The annotated method is a list of groups that the configuration method will run before. This method will run shortly before invoking the first test method that belongs to any of these groups.
  • @AfterClass: The annotated method will run after all the current class test methods have been run.
  • @BeforeClass: The annotated method will run before the first test method in the current class is invoked.
  • @AfterMethod: The annotated method will run after each test method has been run.
  • @BeforeMethod: The annotated method will run before each test method has been run.
  • @Test: This annotated method is a part of a test case.

There are several benefits of the available TestNG Annotations

  1. By looking up annotations, it describes the methods that it is interested in. Method names are also not limited to any pattern or format.
  2. We can pass annotations with additional parameters.
  3. Annotations are strongly typed so that the compiler can flag any errors right away.
  4. Test classes are no longer needed for extensions.

Watch this video to learn about the TestNG Annotations and how they help provide better structure and readability to the code.

Writing a Basic Test in TestNG

Now that we have essential elements in place let us see the primary steps involved in creating a TestNG test.

  1. Write the business logic of the test.
  2. Insert TestNG annotations in the code.
  3. Include the test information (e.g., the methods names, class names, group names, etc.) in a testng.xml file.
  4. Run TestNG.

Note: TestNG requires JDK 8 or higher. You can refer to our complete TestNG Tutorial to start from the basics.

Below is a straightforward test is written in TestNG:

Using @BeforeClass and @Test, i.e., after the test class is built & before any test method is run, the setUp() method will be invoked. We will run the group quickly in this example, so aFastTest() is invoked while a SlowTest() is skipped.

Things to Note:

  1. No need to implement an interface or extend a class.
  2. Even though the above example uses the JUnit conventions, it is the annotations that tell TestNG what our methods can be called any name you like.
  3. A test method could belong to one or multiple groups.

We can either invoke our test using the command line, the ant task (as shown below), or the XML file: once you have compiled your test class into the build directory.

Use ant to invoke it:

c:> ant
Buildfile: build.xml

Output

Output

The test results can be browsed using:

start test-output\index.html (on Windows)

If you don’t like this process of getting the test results, there’s an easier way to do it. It is also possible to generate the TestNG reports in Jenkins. There is a plugin Jenkins offers to accomplish this task. The plugin is the TestNG Results plugin. This next section will find out how to generate TestNG report in Jenkins with this plugin.

You might also like- Generate PHPUnit Coverage Report In HTML and XML

With TestNG certification, you can challenge your skills in performing automated testing with TestNG and take your career to the next level.

Here’s a short glimpse of the TestNG certification from LambdaTest:

TestNG Results Plugin

TestNG Results plugin enables you to publish the results of TestNG created using org.testng.reporters.XMLReporter.TestNG The resulting XML file contains more details than the XML file of the JUnit framework. In graph and table reports, this plugin reveals such additional information. The plugin also makes it possible to import the XML TestNG reports in Jenkins from each build.

Now getting to the ‘how’ part of ‘how to generate TestNG report in Jenkins’- data is parsed using the output that org.testng.reporters.XMLReporter generates. The results are shown with a trend graph, and all the specifics of the failed tests are also presented. And that’s how TestNG reports in Jenkins look like!

Let’s find out more about a Jenkins Selenium report.

Features of TestNG Results Plugin

A Jenkins Selenium report generated using TestNG Results plugin offers the following features-

  1. The page layouts are intentionally designed so that they look very similar to Jenkins’s JUnit plugin. Also known for the fact JUnit inspires them.
  2. You will see a summary of passed/fail checks and passed/fail configuration methods on the project/build tab. To see the test failure, it also enlists the failed test cases with a direct connection.
  3. test-summary

  4. The test methods in the class result summary page are separated from the configuration method.
  5. In the Package result summary, we can see the order in which test methods were run during the regression run.
  6. execution-order

  7. The pass/failed pattern of previous job results will be seen in different graphs.
  8. test-graph

  9. Able to hide or expand tables that display package and class results.

Watch this video to learn how TestNG has become one of the most robust test automation frameworks and all you need to know to get started with TestNG in Selenium.

Utilities of TestNG Results Plugin

Using this plugin is very straightforward once you have configured your build to run TestNG-based test cases and figured out how to generate TestNG report in Jenkins. Simply trigger Publish TestNG Results in Post-Build Behavior.

This option enables the following properties to be configured:

  1. TestNG XML report pattern- This is a pattern for the file name that can be used to find the TestNG XML report files (for example, */target/testng-results.xml*). The path is either an Ant-style pattern (e.g., fileset) or a list of files and folders separated by the characters’;:,’. It is also vital that TestNG be configured to generate an XML Jenkins Selenium report using _org.testng.reporters. This is to ensure XMLReporter for this plugin functions properly.
  2. The Escape Test Description string- If checked, while displaying test method results, the plugin escapes the description string that is associated with the test method. Unchecking allows us to use HTML tags to format the description, which is enabled by default.
  3. The Escape exception messages- If checked, the plugin escapes the test method’s exception messages. Unchecking allows you to use HTML tags to format the exception message, e.g., embed links in the text. This is enabled by default.
  4. Show Failed Builds- If selected, the plugin includes results from the failed builds in the trend graph. This is disabled by default.
    • In case this is a maven build, it is always better to configure the build step with -Dmaven.test.failure.ignore=true option. This results in the build with test failures being marked as Unstable, thus distinguishing it from the build that failed because of issues not related to the tests.
    • Also, even when this option is selected, failed builds with no results and aborted builds will not be displayed in graphs.
  5. Marking the build as a failure on a failed configuration- This marks the build as failed if there are any configuration method failures.
  6. Configuring thresholds for marking the build as Unstable/Failed- A Threshold Mode can be configured by the user as a number of tests or a percentage of tests and then configured separately for these values. Even if there are several test failures or label the build as failed, even if there is a single test method failure, this allows for situations where we want the builds to be marked successful.

9 Of The Best Reporting Tools For Selenium

Free Testing

How to Generate TestNG Reports in Jenkins?

Now let us see how to generate TestNG Report in Jenkins. To be able to do this, you need to have all the required tools in your system.

Prerequisites-

  1. TestNG Project
  2. Jenkins – You can check Jenkins installation here.
  3. TestNG Results Plugin in Jenkins

Once you have all this installed, we can get started with a TestNG Project.

How To Create a TestNG Project?

In order to generate TestNG reports in Jenkins, we need to start from scratch. First, we will create a TestNG project. To create a TestNG project, you will need to follow the steps mentioned below-

Step 1: Open Eclipse and create a Java class.

Step 2: Keep all the library files in a folder.

Step 3: Write a Java program.

The below snippet highlights all the three steps mentioned above.

Step 4: Now convert the Java Program into TestNG.

Alternative Method- Another way you can create a TestNG Project is by creating a new TestNG project all from the beginning.

Step 1: Click File –> New –> Java Project

testng

Step 2: Give it an arbitrary Project Name, say “FirstTestNGProject” and then click Next.

FirstTestNGProject

Step 3: Next step is to start importing the TestNG Libraries into the project. Click on the “Libraries” tab –> “Add Library”

Add Library

Step 4: Choose “TestNG” on the Add Library dialog, and click Next.

testng

Step 5: Click on Finish.

It should be noticed that TestNG is included on the Libraries list.

Step 6: The JAR files containing the Selenium API will now be added. These files are located in the Java client driver we downloaded from http://docs.seleniumhq.org/download/.

Selenium API

Then, navigate to where the Selenium JAR files are placed.

After adding the external JARs, the screen should appear like this.

Step 7: Click on Finish and confirm that our FirstTestNGProject is visible on Eclipse’s Package Explorer window.

FirstTestNGProject

After we have the TestNG project, the next step is to install Jenkins. You can refer to the guided steps for Jenkins installation in our Jenkins Tutorial.

Now that we have TestNG Project and Jenkins platform ready, the next step is to install TestNG Reports Plugin in Jenkins.

Installing TestNG Reports Plugin In Jenkins

The next and the most crucial step to know how to generate TestNG report in Jenkins is to install the TestNG Reports plugin. To install the plugin in Jenkins, follow these steps-

  1. Open Jenkins (localhost:8080).
  2. Click on ‘Manage Jenkins‘ → Click on ‘Manage Plugins
  3. TestNG-Reports-Using-Jenkins-1

  4. Click on ‘Available‘ tab
  5. Select ‘TestNG Results Plugin
  6. Click on ‘Install Without Restart
  7. TestNG-Reports-Using-Jenkins

  8. Open the ‘TestNGProject’ Job created earlier.
  9. Click on ‘Configure’ and scroll down to select ‘Post Build Actions
  10. Click on ‘Add Post Build Actions’ dropdown list.
  11. TestNG-Reports-Using-Jenkins-3

  12. And select ‘Publish TestNG Results‘.
  13. TestNG-Reports-Using-Jenkins-4

  14. In the TestNG XML Report Pattern edit box enter ‘**/testng-results.xml‘ and click ‘Save
  15. TestNG-Reports-Using-Jenkins-

  16. We have already created a new project ‘TestNGProject ‘to run TestNG Tests and generate TestNG Reports after execution using Jenkins.
  17. Click on the ‘Build Now‘ button to execute it. This will invoke testng.xml from the batch file.
  18. Right-click on Build Number and click on Console Output to see the result.
  19. Once the execution is completed, there will be a link to view ‘TestNG Results’
  20. TestNG-Reports-Using-Jenkins-7

There you go- that’s how to generate TestNG Report in Jenkins!

Alternative Method To Generate TestNG Reports in Jenkins

There’s another way to set up the TestNG reports in Jenkins Dashboard.

  1. Login into Jenkins.
  2. Manage Jenkins and Install TestNG Result Plugin.
  3. Please make sure that you restart Jenkins after the plugin installation.
  4. TestngResultPlugin

  5. Next, go to Jenkins Home Page → Create New Jenkins Job and in Post-Build Action select → Publish TestNg Result.
  6. TestNg+PostBuild

  7. Now either give the complete path of your testng-result.xml or type **/testng-results.xml in TestNG XML report pattern to search inside your project workspace.
  8. PublishTestNG

  9. Execute your Jenkins Job, and Now your Jenkins Selenium report or ‘TestNG Results’ would be visible in Your Jenkins Dashboard.

TestNg+Result

TestNG+ResultCapture

How to Share TestNG Reports in Jenkins?

In the sections above, we saw how to generate TestNG report in Jenkins or Jenkins Selenium report, as some like to call them. Once you have generated those TestNG reports in Jenkins, what would you like to do with them? The ultimate goal of generating a Jenkins Selenium report is to share them!

There is a possibility to send those email notifications directly from Jenkins along with the generated TestNG reports. In this section, we will learn how to email the generated Jenkins Selenium report to team members, leads or even managers.

Let’s see how we can accomplish this-

  1. Start ‘jenkins.war’ file to get started with Jenkins ( Assuming it is already installed ).
  2. Open Jenkins (http://localhost:8080).
  3. We would need a required set of Plugins before we start:
    • Email Extension
    • Email Extension Template Plugin
    • TestNG Results
    • Selenium HTML Report
  4. Configuration for Sending Email
    • Go to Jenkins –> Manage Jenkins –> Configure System –> Email Notification
    • Enter the email id in the “SMTP Server” edit field. For e.g. “smtp.gmail.com”
    • Click on “Advanced” and configure as shown in the below image.
    • Username and password need to be provided for the account from where we want to send the email notification( You can either use your corporate Email or personal based on situations).
    • It is always better for testing purposes to check the “Test configuration by sending test email” checkbox to validate whether Email was sent successfully or not.
    • Enter the recipient’s email address and click on “Test Configuration” button.
    • If we can see the “Email was successfully sent” message, then it can be confirmed that the configuration is correct.
    • If we see any error message, it could be either because the configuration is incorrect or security access issues. Follow the below steps to enable “Less secure app access.”
    • Navigate to https://myaccount.google.com/
    • Click on “Security
    • Go to the “Less secure app access” section and switch it on.

Yay! You are all set to send the email notification for TestNG reports in Jenkins!!

Now it’s time to test this.

  1. Go to Jenkins Dashboard
  2. Create a new Job.
  3. email

  4. Let us create a new project named “VerifyEmailNotification”.

    Give the Project Description and select ‘Execute Batch Command’ from the Build drop-down and enter any invalid commands as shown in the image below, as each unstable build will receive an email. (Then only email alerts will be sent if the build fails).

  5. Select “Email Notification” from “Post-build Actions” and add the email address of the recipient and check “Send email for every unstable build” as shown in the image below.
  6. Click on “Save.”
  7. Now click on “Build Now” to start the execution.
  8. Go to “Console Output” to verify the result.
  9. Validate the recipient’s mail to confirm if the email was sent from Jenkins.

Now that we are confident our email notifications are working, let us configure Jenkins to attach a TestNG Report in the Email.

  1. Go to Jenkins –> Manage Jenkins –> Configure System –> Extended Email Notification
  2. Enter “smtp.gmail.com” in the “SMTP Server” edit field NOTE: Here, Gmail is taken just as an example.
  3. Next, click on “Advanced” and fill in the details as shown below.
  4. Click on “Save.”
  5. Go to Jenkins dashboard and create either a new project, or you can choose an existing project. Here we have an existing project [Flight Reservation (POM)]
  6. Click on Configure on the left-hand side.
  7. Select “Editable Email Notification” from “Post-build Actions”
  8. Click on “Advanced Settings”
  9. Select “Always” from the “Add Trigger” dropdown.
  10. Click on “Advanced” and fill in the details as shown below.

    • Mention the path of emailable-report.html of TestNG in the Content edit field.
    • Click on “Save.”
    • Next, click on “Build Now” to start the execution of the job.
    • Navigate to “Console Output” to check the result.
    • Validate your recipient mail to see the Test Execution report.

And here you go. Now you are ready to generate TestNG reports in Jenkins & share those Jenkins Selenium reports!

Wrapping Up

I hope this blog has helped you take report generation and sharing one step ahead. Now you not only know how to generate TestNG report in Jenkins, but you have also learned how to share those TestNG reports in Jenkins via Email. It’s time to elevate Jenkins & TestNG’s functioning by sending out the email notification with your very next Jenkins Selenium report. If you’re looking to enhance your TestNG skills further, check out our comprehensive TestNG interview questions answers guide here. Let me know how it goes in the comments section below.

Happy testing!

Author Profile Author Profile Author Profile

Author’s Profile

Piyusha

After being an avid Software developer by profession, I am an Inside content writer who enjoys writing. I enjoy conducting the necessary research to make my articles relevant, current and interesting.

Blogs: 7



linkedintwitter

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free