TestNG | Introduction & Setup

TestNG – List of tutorials

  1. Introduction & Setup (You are reading this)
  2. Annotations (coming soon)
  3. Running a simple TestNG test with Selenium (coming soon)
  4. Assertions (coming soon)
  5. Introduction to TestNG test suite, i.e. testng.xml (coming soon)
  6. Managing test execution through test suite (coming soon)
  7. Parameters (coming soon)
  8. Data Providers (coming soon)
  9. 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

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.
testng-features

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
  • 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
  • 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

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.

Leave a Reply