This tutorial covers TestNG Installation. We will also run a basic TestNG program and see what reports can be generated post-test run in TestNG:
This tutorial will educate you on how to install TestNG through Marketplace and Install New Software options in the Eclipse.
We will also run a basic TestNG program and see what are the reports that one can generate post-test run in TestNG.
=> Watch Out The Simple TestNG Training Series Here.
Table of Contents:
TestNG Installation And Configuration
TestNG For Eclipse: The plug-in helps us in running our TestNG scripts/tests from Eclipse. It helps in running suites, groups or individual test cases. Whenever there is an error, it will be reported in a separate tab and we can view them without any hassles. This plug-in also has several templates that make our testing easy.
Global Environment Variables set up/configuration required for TestNG in Eclipse:
- The latest version of Java should be installed for running the TestNG for Eclipse plugin.
- The environment variable for Java should be configured.
- Eclipse version 4.2 and above should be downloaded in the machine. Versions below that are not supported, we will have to make sure to update the correct version.
- Eclipse Versions required include Oxygen (4.7), Neon (4.6), Mars (4.5), Luna (4.4), Kepler (4.3), Juno (4.2, 3.8), Previous to Juno (<=4.1), Photon (4.8), 2018-09 (4.9).
- Platform Support: Windows, Mac, Linux/GTK.
TestNG can be installed in two different ways:
- We can use the Eclipse Marketplace.
- We can install it from Install New Software.
Let’s discover both the ways of installation of TestNG in Eclipse.
#1) Installation Through Marketplace
Given below are the steps to install TestNG from Marketplace:
Step 1: Start Eclipse.
Step 2: Go to the Help Section.
Step 3: Click Eclipse Marketplace in the Help Section.
Step 4: The Eclipse Marketplace window opens. Enter TestNG in the Find option and click on the search button.
Step 5: Click on the Install button as shown below.
Step 6: Another new window will open, do not change anything. Just click on the Confirm button.
Step 7: Click on the Next button and the License Agreement dialog box will get opened. Click on “I accept the terms of the license agreement” and then click on the Finish button.
Step 8: When a Security warning is received, click on the OK button.
Step 9: Please wait for the installation to be completed.
Step 10: Eclipse will prompt for a restart, click the Yes button. If not, we should restart Eclipse for the changes that we have done.
Step 11: Once the restart is completed, we can verify if TestNG was successfully installed or not. To verify, click on Windows, then on Preferences and see if TestNG is included in the Preferences list or not.
#2) Through Install New Software
Step 1: Click on the Help Button on the Top Menu bar in Eclipse. Then click on “Install New Software” as shown below.
Step 2: Enter http://beust.com/eclipse in Work with the Field as shown below and wait for some time for TestNG to display.
Step 3: Select the checkbox for TestNG and click on the Next Button.
Step 4: It checks for the requirements or any dependencies before starting the installation. If there are any issues with the requirements/dependencies, it will ask to install them before continuing with TestNG. Most of the time it gets installed successfully.
Step 5: Once the above steps are completed, it will ask to review the installation details. Click OK and then on the Next button to continue.
Step 6: Accept the terms of the license agreement and click on the Finish button.
It takes a few minutes to get installed.
Note: Once the installation is completed, we can check if TestNG is installed properly or not. To do that, go to the Windows Menu bar, click on Show View and then click on Other as shown below.
Expand the Java Folder and see if TestNG is available as shown below.
TestNG Automation Test Case
Let’s create our first automation test case with TestNG.
Step 1: Create a new Project and Package. Click on File -> New -> Java Project.
Then the New Java Project window will open up. Enter a Project Name as shown below.
Here it has been given as TestNGPractice. Don’t select anything, just keep it as the default unless it shows any messages. Click on the Next button.
Now the Java Settings screen is displayed. Please keep it as default and click on the Finish button.
Step 2: The TestNGPractice project is created and we can also see the JRE System Library to be included automatically.
Step 3: Right-click on the TestNGPractice Project and then click on Properties. Properties for TestNGPractice window will be displayed.
Step 4: Select Java Build Path and click on the libraries tab as shown below.
Now click on Add External JARs. It will take you to the folder where you have kept all your executable JAR files. Select all the files and click on the Open button as shown below.
Now click on add two more jar files as shown below.
You will be able to see that all the External Jar Files have been included/added for this Project. Click on Apply and Close button.
Now all the Referenced Libraries have also been added.
Step 5: Now Right-click on the src file under TestNGPractice Project. Select New-> Other-> Create a new Java Package.
Enter the name as shown below “practiceTests” and click on Finish Button. Don’t change anything unless and until you get any message.
Step 6: Now create a TestNG class under the package practiceTests. Right-click on the Test Case folder, go to TestNG and select “TestNG Class”.
Enter the Class name as BasicTest in the New TestNG class screen as shown below and click on the Finish button. You can also check @BeforeMethod and @AfterMethod Annotations for our first basic program.
Annotations are used by the developers to describe codes in a program. There are different types of annotations used in TestNG as per the requirements. @Test is the base annotation in the TestNG workflow. Here, @BeforeMethod and @AfterMethod annotations are executed before and after the execution of the @Test methods respectively.
Note: In TestNG class, we don’t select public static void main as we do in Java Programs. It should always be unchecked. We don’t need the main method to execute the class. TestNG automatically executes the class.
Your first TestNG class is prepared. Now you can write a basic test for your first program in the TestNG class.
Let’s write the basic TestNG program. We will use @BeforeMethod, @Test and @AfterMethod TestNG Annotations for writing our program. We will be learning more about Annotations in our upcoming tutorials.
Here you can see that we have checked the annotations while creating the first TestNG class. We can see the annotations that we have selected (@BeforeMethod and @AfterMethod), imported from the TestNG library.
Program:
package practiceTests; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import org.testng.annotations.AfterMethod; public class BasicTest { @BeforeMethod public void beforeMethod() { System.out.println("Launch the Browser"); } @Test public void gmailLogin() { System.out.println("Gmail Logged in successfully"); } @AfterMethod public void afterMethod() { System.out.println("Log out of Gmail and Close the Browser"); } }
Step 7: Let’s run the test. It should be done by right-clicking on the test case script. Select Run As -> TestNG Test. Click on the OK Button.
Step 8: Once the execution is completed, the results would look as in the below screens. The results screen shows PASSED: gmailLogin. This means that the test has passed successfully without any errors.
Similarly, you can check the other two tabs i.e. Failed Tests and Summary. If you have any Failed Tests, you can check the details and the Test Summary tab shows details like Test name, Time (in seconds) taken to execute the test, Class count (here it was only one) and method (here only one).
TestNG also produces HTML Reports. To access this report, you can go to the Project folder and click on the test output folder. You can check if there are different types of reports like ‘emailable-report.html’ and ‘index.html’ as shown below. Click on both the reports and you will be able to see the details of your test execution.
TestNG’s ‘index.html’ gives a link to different components like Groups, Reporter Output, Times, etc. By clicking on each of these, you can see different detailed descriptions of execution. We will be discussing these in the upcoming tutorials.
Conclusion
TestNG has useful features that make testing easier. We have seen how TestNG can be installed and how to run a basic test program using TestNG in this tutorial. We have also seen how reports can be generated post test run.
Our upcoming tutorial will explain the TestNG Annotations and Listeners in detail.
=> Take A Look At The TestNG Beginners Guide Here.