TestNG – List of tutorials
- Introduction & Setup (You are reading this)
- Annotations (coming soon)
- Running a simple TestNG test with Selenium (coming soon)
- Assertions (coming soon)
- Introduction to TestNG test suite, i.e. testng.xml (coming soon)
- Managing test execution through test suite (coming soon)
- Parameters (coming soon)
- Data Providers (coming soon)
- Test execution reports (coming soon)
What is a testing framework?
Although we can write the test script in the code blocks provided by the programming language, like for Java in the main()
method. In the real world test automation projects we use a test framework to better structure the test code.
Let us say you own a bakery shop. You get a bulk order to produce cookies of dimension 10cm x 10cm. One approach would be to bake cookies by assumptions and then measure each cookie one by one and cut it to 10cm x 10cm. The other approach would be to create a structure of dimension 10cm x 10cm and then bake as many cookies over it. This structure is the framework.
![framework](https://i1.wp.com/cosmocode.io/wp-content/uploads/2016/04/framework.jpg?resize=248%2C203&ssl=1)
A testing framework provides functionalities that can be directly used by individual tests without the need to write code in every test to achieve those functionalities. These functionalities include but not limited to test report generation, test execution, etc.
What is TestNG?
TestNG is the most popular testing framework for Java inspired by JUnit and NUnit. You can also compare it with pytest for the Python world. It is based on Java annotations. It can be used for a broad range of testing activities from unit testing to integration testing. It is widely used along with the Selenium testing tool to build a Java testing framework.
Why do we need TestNG in Selenium?
TestNG has several features that help us organize tests in more efficient way. Some of the key features are as follows:
- As a test runner, it can run all or selected tests among the list of tests called a test-suite.
- It provides a mechanism to prioritize tests or run tests in a particular order.
- We can also run the same test with a different set of data for data-driven testing. For instance, verifying a form accepts different combinations of inputs.
- It can automatically generate the status of test execution i.e a Test execution report.
- It provides ways to run multiple tests parallelly for faster execution. We can also run the same test parallelly but on different browsers to achieve cross-browser execution.
How to setup TestNG?
Preconditions
- Java Development Kit (JDK) 7 or higher
- Eclipse IDE (You can use any editor but for this tutorial we’ll use Eclipse IDE)
Please refer This Tutorial for installing preconditions.
Installation steps
- Launch Eclipse IDE
- From menu-bar select Help -> Eclipse Marketplace
![eclise](https://i1.wp.com/cosmocode.io/wp-content/uploads/2016/04/eclise.png?resize=378%2C329&ssl=1)
- Under the ‘Find’ field, enter the text as ‘TestNG’ and click on the search icon. You will now see TestNG with ‘Install’ button. Click on the ‘Install’ button. If TestNG is already installed, you will have ‘Update’ and ‘Uninstall’.
![eclise2](https://i0.wp.com/cosmocode.io/wp-content/uploads/2016/04/eclise2.png?resize=566%2C691&ssl=1)
- Follow onscreen instructions and complete installation.
Creating and running a simple TestNG test
Once TestNG is installed, reopen eclipse. Create a new Java project MyTestNGProject
and a class MyTests
. You can refer to This Tutorial if you need help. Once the Java project and class has been created, right-click on the project name. You should see the TestNG option like below:
![gg](https://i0.wp.com/cosmocode.io/wp-content/uploads/2016/04/gg.png?resize=523%2C47&ssl=1)
Click the option Convert to TestNG.
Next, we’ll write a simple test method and give @Test
annotation to this method. This annotation will imply that the method is a TestNG test method. You would need to import the following package – org.testng.annotations.Test
@Test
public void helloTestNG() {
assert true;
}
Right-click over the method name and select Run as TestNG. You should see your very first test running. If you are curious why the test passed it is because we have used an assert
statement and provided the first argument to be hardcoded true
. This statement verifies that the argument passed is true
. In the real world project, this value will be derived from a variable or WebDriver
function calls.
The above paragraph probably did not make sense to you. You should Continue Reading other parts of TestNG tutorial for better understanding.
In the Next Tutorial, we will learn about TestNG annotations.