TestNG Introduction
In this post, we will be studying – What is TestNG? It is a widely used testing framework. It supports all levels of testing from unit testing to automated functional testing. We will see its usage in Selenium tests and also have a look at its advantages.
Content
What is TestNG?
TestNG is a JUnit-inspired testing framework. It provides several features like data-driven testing, parameterization support, running test cases in parallel, and test case grouping, etc. All these features help in creating robust and powerful testing projects.
We can simply convert a method or function to a test by adding an annotation – @Test on top of it. A simple test in Java looks like this-
@Test
public void sampleTest() {
//Any test logic
System.out.println("Hi! ArtOfTesting here!");
}
Now, either in case of exception or in case of assertion failure, the test case will be marked as Failed. Else, the test will be marked as Passed.
Features of TestNG
- Different Types of Assertions
It has an Assert class that provides multiple methods supporting different types of assertions. For instance, these assertions help in checking equality of expected and actual result; asserting a condition as True or False; asserting if a value is Null or not null, etc. - Run tests in Parallel
Using its testNG.xml file, we can run test cases in parallel. This helps in reducing the overall test execution time. The parallel execution can be done in multiple levels – method level, class level, suite level. For details check running Selenium Tests in Parallel. - Make tests dependent on one another
We can make the test dependent on other tests. This can be done with the help of the ‘dependsOnMethods’ and ‘dependsOnGroups’ attributes with @Test annotation. - Prioritizing tests
With this, we can assign a numerical priority value to the Tests. The default priority is 0. - Grouping of tests
It supports the logical grouping of test cases. Thus, providing the ability to run the test groups in parallel, performing certain operations before or after the execution of the tests belonging to a group. - Data-Driven Testing
Using @DataProvider, we can create data-driven tests. With this dataProvider annotation, we can pass the test data to the test method. - Reporting
After test execution, an HTML report gets created providing tabular reporting of test results. The reporting format is also configurable by implementing listeners. - Parameterization
It provides inherent support for parameterization using its @Parameters annotation. Using @Parameters annotation, we can pass the value of a parameter to the test logic from the testng.xml file.
Advantages of TestNG
- It helps in reducing overall test execution time by providing the capability to run tests in parallel.
- It can be easily integrated with CICD tools like Jenkins. This can be done with the help of the testNG.xml file.
- No need to hardcode the data. This can be done with different annotations like – @DataProvider, @Parameter, etc.
- We can make one test dependent on another. Thus, saving time by skipping the dependent test in case the parent tests fail.
- With the grouping of tests, we can create different test suites like – sanity, smoke, regression, etc. Moreover, we can integrate and trigger those test suites with CICD tools like – Jenkins.
TestNG Installation
There is no special installation of TestNG. You can simply download the testNG jar file and add it as a library or include it through maven.
org.testng
testng
6.10
test
TestNG with Eclipse Setup
In order to integrate TestNG with Eclipse IDE, we can follow the below steps mentioned in – TestNG.org. For this, we can either use Eclipse Marketplace or the update site.
Via Eclipse Marketplace
Go to the TestNG page on the Eclipse Market Place and drag the icon called “Install” onto your workspace.
Install from update site
- Select “Help / Install New Software…”
- Enter the update site URL in the “Work with:” field-
Update site for release – https://dl.bintray.com/testng-team/testng-eclipse-release/. - Check the check box next to the URL.
- Click on ‘Next’.
- After that follow the steps mentioned on Eclipse.
Once complete, create a sample test like this.
@Test
public void sampleTest() {
System.out.println("Hello World!");
}
Now, we can run the test by right-clicking the class and clicking on the “Run as” option. After that click on “TestNG Test”. With this, we can execute the test cases.
If you see, the test results in the form of Passed, Failed, and Skipped. Then, it means we have set up properly.
This completes our tutorial on the topic – “What is TestNG”. For a complete step-by-step tutorial series check – TestNG tutorial. Thank you.