Software Development

An Introduction to TestNG Framework

TestNG is a testing framework for Java that is widely used in test automation. It provides a wide range of features for writing and running tests, including annotations, test configuration, data-driven testing, parallel execution, and reporting.

Annotations are a key feature of TestNG, allowing developers to specify the behavior of test methods using simple, descriptive keywords such as @Test, @BeforeMethod, @AfterMethod, @DataProvider, and @Parameters. These annotations enable TestNG to automatically execute tests in a specified order and with a predefined set of data inputs.

Test configuration is another important feature of TestNG, allowing developers to set up test environments, initialize test data, and configure test settings. TestNG also supports data-driven testing, which allows developers to run the same test with different sets of data inputs. This feature is particularly useful for testing complex systems with multiple input parameters.

Parallel execution is another key feature of TestNG, enabling developers to execute tests simultaneously across multiple threads, processors, or machines. This helps to reduce the time required to run large test suites and to improve test efficiency.

TestNG also provides comprehensive reporting features, allowing developers to generate detailed test reports, track test results, and identify areas for improvement.

Overall, TestNG is a powerful and flexible testing framework that is well-suited for automated testing in Java-based applications.

1. JUnit 5 vs. TestNG

JUnit and TestNG are the most popular Java frameworks for automated Selenium testing. Below we shall see their main differences.

Here are some key differences between the two:

  1. Annotations: TestNG provides a wider range of annotations than JUnit 5, making it easier to set up test configurations and execute tests in a specific order.
  2. Reporting: TestNG provides better reporting capabilities out of the box, including HTML reports that show the status of each test and their associated data.
  3. Parallel execution: Both frameworks support parallel execution of tests, but TestNG’s parallel execution capabilities are more advanced and flexible.
  4. Extension model: JUnit 5 has a more robust and flexible extension model, which allows developers to customize and extend the framework’s behavior more easily.
  5. Assertions: JUnit 5 has a more comprehensive set of built-in assertions, making it easier to write concise and readable tests.
  6. Platform support: JUnit 5 has better support for running tests on different platforms, including the ability to run tests on the Java Virtual Machine (JVM), in the browser, and on mobile devices.

Ultimately, the choice between JUnit 5 and TestNG will depend on the specific needs of the project and the preferences of the development team. Both frameworks are powerful and flexible, and can be used to create effective and efficient test suites for Java applications.

2. Getting Started With TestNG

Here are the steps to get started with TestNG:

  1. Install TestNG: TestNG can be easily installed using a build tool such as Maven or Gradle. To install TestNG using Maven, add the following dependency to your project’s pom.xml file:
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>7.4.0</version>
    <scope>test</scope>
</dependency>
  • Create a TestNG test class: A TestNG test class is a Java class that contains one or more test methods, annotated with the @Test annotation. Here’s an example:
import org.testng.annotations.Test;

public class MyTestNGTest {

    @Test
    public void testMethod() {
        // Your test code here
    }
}
  • Configure TestNG: TestNG provides a wide range of configuration options to customize the behavior of your tests. Configuration can be done using XML files or programmatically using Java code. Here’s an example of a TestNG XML configuration file:
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="MyTestSuite">
  <test name="MyTest">
    <classes>
      <class name="com.example.MyTestNGTest"/>
    </classes>
  </test>
</suite>
  • Run your tests: Once you have created your test class and configured TestNG, you can run your tests using your build tool or from the command line. To run tests from the command line, use the following command:
java -cp <path to your test classes and dependencies> org.testng.TestNG <path to your testng.xml file>

For example, if your test classes and dependencies are located in the “target” directory, and your TestNG XML configuration file is located in the “src/test/resources” directory, you can run your tests with the following command:

java -cp target/*:target/classes org.testng.TestNG src/test/resources/testng.xml
  • Analyze test results: TestNG provides detailed reports of test results, including information on passed, failed, and skipped tests, as well as the time taken to run each test. The default test report is an HTML file located in the “test-output” directory.

That’s it! With these steps, you should be able to get started with TestNG and create effective and efficient test suites for your Java applications.

3. Methods To Run Tests with TestNG

3.1 Automation TestNG With Selenium

Here’s how you can use TestNG with Selenium to create automated tests:

  • Set up your Selenium environment: Before you can use TestNG with Selenium, you need to set up your Selenium environment. This involves downloading the necessary WebDriver executable for your browser and adding it to your system PATH. You will also need to add the Selenium Java bindings to your project’s build path. Here’s an example of how to do this with Maven:
<dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.1.0</version>
    </dependency>
</dependencies>
  • Create a TestNG test class: A TestNG test class is a Java class that contains one or more test methods, annotated with the @Test annotation. Here’s an example:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class MySeleniumTest {

    private WebDriver driver;

    @BeforeMethod
    public void setUp() {
        // Set up the WebDriver instance
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver.exe");
        driver = new ChromeDriver();
    }

    @AfterMethod
    public void tearDown() {
        // Quit the WebDriver instance
        driver.quit();
    }

    @Test
    public void testSearch() {
        // Navigate to the Google homepage
        driver.get("https://www.google.com/");

        // Search for TestNG
        driver.findElement(By.name("q")).sendKeys("TestNG");
        driver.findElement(By.name("btnK")).click();

        // Verify that the search results page title contains "TestNG"
        String title = driver.getTitle();
        Assert.assertTrue(title.contains("TestNG"));
    }
}
  • Run your tests: Once you have created your test class, you can run your tests using TestNG as described in the previous answer.
  • Analyze test results: TestNG provides detailed reports of test results, including information on passed, failed, and skipped tests, as well as the time taken to run each test. The default test report is an HTML file located in the “test-output” directory.

That’s it! With these steps, you should be able to use TestNG with Selenium to create automated tests for your web applications. You can use Selenium’s various APIs to interact with web elements and perform actions, and TestNG’s annotations to control the flow of your tests.

3.2 Parallel Test Execution in TestNG

TestNG provides a powerful feature to execute tests in parallel, which can greatly reduce the time required to run a large suite of tests. Here’s how you can execute tests in parallel with TestNG:

  1. Configure parallel execution: TestNG provides several options for parallel execution, which can be configured in the TestNG XML configuration file. The options include:
    • parallel: This attribute specifies the type of parallel execution. The possible values are “methods”, “tests”, “classes”, “instances”, or “none”. For example, to execute all methods in parallel, set parallel="methods" in the test tag.
    • thread-count: This attribute specifies the number of threads to use for parallel execution. For example, to use 5 threads, set thread-count="5" in the test tag.

Here’s an example of a TestNG XML configuration file that runs tests in parallel:

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="MyTestSuite" parallel="methods" thread-count="5">
  <test name="MyTest">
    <classes>
      <class name="com.example.MyTestNGTest"/>
    </classes>
  </test>
</suite>
  • Annotate test methods: In order to run test methods in parallel, annotate them with the @Test annotation and set the threadPoolSize attribute to the number of threads you want to use. For example:
import org.testng.annotations.Test;

public class MyTestNGTest {

    @Test(threadPoolSize = 5, invocationCount = 10)
    public void testMethod() {
        // Your test code here
    }
}

This will run the testMethod() 10 times, with 5 threads running in parallel.

  • Run your tests: Once you have configured your parallel execution and annotated your test methods, you can run your tests as usual using TestNG.

TestNG also provides other features to customize the parallel execution of your tests, such as thread safety, thread timeouts, and more. With these features, you can create efficient and effective test suites that can run in parallel to save time and increase productivity.

3.3 Creating TestNG XML File

To create a TestNG XML file, you will need to specify the configuration for your test suite, including the classes, methods, and parameters to be executed. Here’s a step-by-step guide to creating a TestNG XML file:

  • Create a new XML file: In your project, create a new file with the extension .xml. This file will contain the TestNG configuration for your test suite.
  • Define the XML structure: The TestNG XML file should start with an XML declaration, followed by a <!DOCTYPE> declaration that points to the TestNG DTD, and a root <!DOCTYPE> element named suite. Here’s an example:
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="MyTestSuite">
  <!-- Configuration goes here -->
</suite>
  • Define test configuration: Within the suite element, you can define one or more test elements, each representing a set of tests to be executed. The test element can have a name attribute and may contain one or more class elements, each representing a Java class to be tested. Here’s an example:
<suite name="MyTestSuite">
  <test name="MyTest">
    <classes>
      <class name="com.example.MyTestClass"/>
    </classes>
  </test>
</suite>
  • Define test method configuration: Within the class element, you can define one or more test-method elements, each representing a test method to be executed. The test-method element can have a name attribute, as well as optional attributes such as enabled, description, groups, and depends-on-method. Here’s an example:
<suite name="MyTestSuite">
  <test name="MyTest">
    <classes>
      <class name="com.example.MyTestClass">
        <methods>
          <include name="testMethod1" />
          <include name="testMethod2" />
          <exclude name="testMethod3" />
        </methods>
      </class>
    </classes>
  </test>
</suite>

In this example, we have included testMethod1 and testMethod2, and excluded testMethod3.

  • Define parameters: You can also define parameters in your TestNG XML file, which can be used to configure your tests. Parameters can be defined at the suite, test, class, or method level, and can be referenced in your test code using the @Parameters annotation. Here’s an example:
<suite name="MyTestSuite">
  <parameter name="baseUrl" value="https://www.google.com" />
  <test name="MyTest">
    <parameter name="searchTerm" value="TestNG" />
    <classes>
      <class name="com.example.MyTestClass" />
    </classes>
  </test>
</suite>

In this example, we have defined a parameter named baseUrl at the suite level, and a parameter named searchTerm at the test level.

  • Save the XML file: Once you have defined your TestNG XML configuration, save the file with a descriptive name, such as testng.xml.

That’s it! You can now use this TestNG XML file to run your tests. To execute the tests defined in the XML file, you can use the TestNG command-line tool or your preferred build tool, such as Maven or Gradle.

3.4 Automation With Selenium, Cucumber, and TestNG

Selenium, Cucumber, and TestNG can be used together for automated testing of web applications. Here’s how you can set up and use these tools together:

  • Install Selenium WebDriver: Start by installing Selenium WebDriver for your preferred programming language. WebDriver is the core component of Selenium that provides a library for interacting with web browsers. You can install it using a package manager or by downloading it from the Selenium website.
  • Install Cucumber: Next, install Cucumber for your programming language. Cucumber is a behavior-driven development (BDD) framework that allows you to write executable specifications in plain text. You can install it using a package manager or by downloading it from the Cucumber website.
  • Install TestNG: Finally, install TestNG for your programming language. TestNG is a testing framework that provides advanced features such as parallel testing, data-driven testing, and test configuration through XML files. You can install it using a package manager or by downloading it from the TestNG website.
  • Create a Cucumber feature file: Start by creating a feature file in the features directory of your project. This file will contain the scenarios for your test suite, written in Gherkin syntax. Here’s an example:
Feature: Search on Google
  As a user
  I want to search for a term on Google
  So that I can find information

  Scenario: Searching for TestNG on Google
    Given I am on the Google search page
    When I enter "TestNG" in the search box
    And I click the search button
    Then I should see search results for "TestNG"
  • Create a step definition file: Next, create a step definition file in the step_definitions directory of your project. This file will contain the code that maps the steps in the feature file to actual Selenium WebDriver commands. Here’s an example:
public class SearchSteps {
  private WebDriver driver;
  
  @Given("^I am on the Google search page$")
  public void i_am_on_the_google_search_page() {
    driver = new ChromeDriver();
    driver.get("https://www.google.com/");
  }
  
  @When("^I enter \"(.*)\" in the search box$")
  public void i_enter_search_term(String searchTerm) {
    WebElement searchBox = driver.findElement(By.name("q"));
    searchBox.sendKeys(searchTerm);
  }
  
  @When("^I click the search button$")
  public void i_click_the_search_button() {
    WebElement searchButton = driver.findElement(By.name("btnK"));
    searchButton.click();
  }
  
  @Then("^I should see search results for \"(.*)\"$")
  public void i_should_see_search_results(String searchTerm) {
    WebElement resultsStats = driver.findElement(By.id("result-stats"));
    assertThat(resultsStats.getText()).contains(searchTerm);
    driver.quit();
  }
}
  • Create a TestNG XML file: Finally, create a TestNG XML file in the root directory of your project. This file will specify the configuration for your test suite, including the Cucumber feature file and the TestNG test class. Here’s an example:
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="MyTestSuite">
  <test name="MyTest">
    <classes>
      <class name="cucumber.api.testng.CucumberTestNGRunner">
        <methods>
          <include name=".*" />
        </methods>
        <params>
          <param name="cucumber.features" value="src/test/resources/features" />
          <param name="cucumber.glue" value="step_definitions" />
        </params>
      </class
    </classes>
  </test>
</suite>

3.5 Run JUnit Selenium Tests Using TestNG

JUnit and TestNG are both popular testing frameworks for Java. While JUnit is focused on unit testing, TestNG provides features for integration and functional testing as well. If you have existing Selenium tests written with JUnit and you want to run them using TestNG, you can follow these steps:

  • Install TestNG: Install TestNG in your development environment. You can install it using a package manager or by downloading it from the TestNG website.
  • Create a TestNG XML file: Create a TestNG XML file that specifies the configuration for your test suite. Here’s an example:
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="MyTestSuite">
  <test name="MyTest">
    <classes>
      <class name="com.example.tests.LoginTest"/>
    </classes>
  </test>
</suite>

This file defines a test suite named MyTestSuite with a single test named MyTest. The test includes a single class named com.example.tests.LoginTest.

  • Convert JUnit tests to TestNG: To convert JUnit tests to TestNG, you can simply add @Test annotations to your existing test methods. Here’s an example:
public class LoginTest {
  private WebDriver driver;

  @Before
  public void setUp() {
    driver = new ChromeDriver();
  }

  @After
  public void tearDown() {
    driver.quit();
  }

  @Test
  public void testLogin() {
    driver.get("http://www.example.com/login");
    WebElement usernameField = driver.findElement(By.name("username"));
    WebElement passwordField = driver.findElement(By.name("password"));
    WebElement loginButton = driver.findElement(By.name("login"));

    usernameField.sendKeys("myusername");
    passwordField.sendKeys("mypassword");
    loginButton.click();

    assertTrue(driver.getTitle().contains("Dashboard"));
  }
}

In this example, the @Test annotation has been added to the testLogin() method.

  • Run the tests: Run the tests using the TestNG XML file. You can do this using the TestNG command-line tool, or by configuring your build system to run the tests using TestNG. Here’s an example of running the tests using the TestNG command-line tool:
$ java -cp testng.jar:mytests.jar org.testng.TestNG testng.xml

This command runs the tests defined in mytests.jar using the configuration specified in testng.xml.

4. Use Cases for TestNG

TestNG is a popular testing framework for Java that provides a wide range of features to help developers and testers write robust and efficient tests. Here are some use cases for TestNG:

  1. Unit testing: TestNG is often used for unit testing because it allows developers to write tests that are easy to maintain, execute quickly, and report on the results. TestNG provides annotations, such as @Test, @BeforeMethod, and @AfterMethod, that make it easy to write and organize unit tests.
  2. Integration testing: TestNG can also be used for integration testing, where multiple components of an application are tested together to ensure that they work correctly. TestNG’s support for groups, dependencies, and data providers make it easy to write and execute integration tests.
  3. Parameterized testing: TestNG provides support for parameterized testing, which allows developers to write a single test method that can be executed with multiple sets of test data. This makes it easy to test a wide range of scenarios with minimal code duplication.
  4. Data-driven testing: TestNG also provides support for data-driven testing, where test data is stored separately from the test code. This makes it easy to modify and reuse test data without changing the test code.
  5. Parallel testing: TestNG allows tests to be run in parallel, which can greatly reduce the time it takes to execute a large suite of tests. TestNG can run tests in parallel at the method, class, test, and suite levels.
  6. Test configuration and management: TestNG allows developers to configure and manage tests easily. TestNG provides a comprehensive XML configuration file that can be used to specify the test suite, test methods, test data, test groups, and more. Additionally, TestNG supports various listeners and reporters that can be used to customize the test execution and generate detailed reports.

In summary, TestNG is a versatile and powerful testing framework that can be used for a wide range of testing scenarios, from unit testing to integration testing, parameterized testing, data-driven testing, and more.

5. Conclusion

In conclusion, TestNG is a highly useful and popular testing framework for Java that provides a wide range of features to help developers and testers write robust and efficient tests. It offers support for unit testing, integration testing, parameterized testing, data-driven testing, and parallel testing, as well as comprehensive test configuration and management options. With TestNG, developers can easily write, organize, and execute tests, while generating detailed reports to help identify and fix issues quickly. Overall, TestNG is an essential tool for any Java developer looking to build high-quality software with reliable and effective tests.

Java Code Geeks

JCGs (Java Code Geeks) is an independent online community focused on creating the ultimate Java to Java developers resource center; targeted at the technical architect, technical team lead (senior developer), project manager and junior developers alike. JCGs serve the Java, SOA, Agile and Telecom communities with daily news written by domain experts, articles, tutorials, reviews, announcements, code snippets and open source projects.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button