CosmoCode (Formerly TeachMeSelenium)

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.

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:

How to setup TestNG?

Preconditions

Please refer This Tutorial for installing preconditions.

Installation steps

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:

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.

Exit mobile version