Table of Contents
Overview to TestNG
TestNG (Test Next Generation) is a testing framework for the Java programming language that was developed to make a variety of testing tasks easier. TestNG is designed to cover all categories of tests: unit, functional, end-to-end, and integration. Although it has several new features that increase its power and ease of use, it nevertheless takes influence from JUnit and NUnit.
Because of its extensive feature set, adaptability, and user-friendliness, TestNG is a valuable instrument in the field of automated testing. It offers a strong foundation for effectively creating and carrying out tests, empowering teams to confidently maintain high-quality software.
TestNG environment setup:
- Download and install Java as per your system operating system if java is not available in your system. We will use recommended jdk17 version. Here is the link Java Archive Downloads – Java SE 17 (oracle.com) Please follow the steps which are coming during installation of Java.
- Set Environment variable JAVA_HOME e.g. C:\Program Files\Java\jdk17. If you need a detailed guide, please follow How to Set the JAVA_HOME Variable on Windows, Linux, & Mac (wikihow.com)
- Download and install Maven using link Maven – Download Apache Maven
- Set MAVEN_HOME environment variable e.g., C:\apache-maven-3.8.8
- There are multiple Integrated Development Environment (IDEs) supported for java development like IntelliJ Idea, Eclipse, and Visual Studio Code. Please use any one for your development.
- To install TestNG for Java projects, you typically use build management tools such as Maven Add the TestNG dependency to your project’s pom.xml file. Maven will automatically download TestNG and its dependencies during the next build.
Manual Download of TestNG
- Without the Maven build tool, download TestNG jar file from mvn repository. Let’s say the version is 7.9.0. https://mvnrepository.com/artifact/org.testng/testng/7.9.0.
- Copy the jar file into a folder. Create a sample project using your IDE and go to the project structure.
- Go to the module->dependency section and add the downloaded jar file. TestNG testing framework is now added to your project.
10 Wonderful TestNG Features:
Annotations: TestNG has an extensive collection of annotations, including @Test, @BeforeMethod, @AfterMethod, @BeforeClass, @AfterClass, @BeforeTest, and @AfterTest, making it simple for developers to specify how test methods should behave as well as setup and takedown procedures.
TestNG supports automatic test discovery using the @Test annotation.
Centralized Test Configuration: TestNG offers the option to define test configurations using XML files. These XML configuration files provide a centralized way to specify test suites, test methods, listeners, parameters, and more. TestNG automatically parses these XML files to discover and execute tests according to the defined configurations.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Regression Suite-R2.2.2" parallel="none" thread-count="1">
<test name="Sanity-Test" group-by-instances="true" time-out="12000000">
<classes>
<class name="SearchTest"/>
<class name="HomePageTest"/>
<class name="LoginPageTest"/>
</classes>
</test>
Parameterization: TestNG allows developers to run the same test method with different sets of data using the feature parameterization. This reduces duplication of the code, increases test coverage, and reduces maintenance efforts. @Parameters annotation is used to achieve this. Parameterization in TestNG empowers developers to write more versatile and reusable test cases.
Prioritize test: Using the priority element of the @Test annotation, TestNG enables developers to set a priority for the order in which test methods inside a test class are executed. Regardless of the test methods’ default alphabetical order, this feature guarantees that they are executed in the prescribed order.
TestNG executes test methods in alphabetical order according to their method names. By giving test methods a priority using the priority attribute, developers can override this behavior.
import org.testng.annotations.Test;
public class FirstTest {
@Test(priority = 1 )
void test1()
{
System.out.println("Case1");
}
@Test(priority = 2 )
void test2()
{
System.out.println("Case2");
}
}
Dependency Management: With TestNG, dependencies between test methods specified by developers by utilizing the dependsOnMethods element of the @Test annotation. This property guarantees that the dependent test method or methods are run ahead of the current test method.
@Test
public void loginTest() {
// Test logic for login
}
@Test(dependsOnMethods = "loginTest")
public void homepageTest() {
// Test logic for Homepage
}
Groups: With TestNG, developers can use the @Test(groups = {…}) annotation to group test methods. Test management becomes more structured and adaptable with this feature, which makes it easier to execute test sets based on how they are categorized into groups.
Parallel Execution: With TestNG’s strong support for parallel test method execution, developers may take use of several threads to speed up test execution and minimize the total duration of the test suite. By running tests concurrently, parallel execution improves testing efficiency, particularly for big test suites.
TestNG enables developers to set up parameters for parallel execution declaratively using the testng.xml configuration file or programmatically using annotations.
<suite name="Regression Suite-R2.2.2" parallel="none" thread-count="4">
Listeners: With TestNG, developers may create custom listeners to track test execution and trigger actions before or following test start, test success, test failure, and other events. For reporting, logging, and connecting TestNG with other frameworks or tools, this capability is helpful. An overview of some commonly used listeners:
- ITestListener: You can listen to test execution events through this interface, including test start, test success, test failure, test skipped, etc. You can carry out operations prior to or following the execution of each test method by implementing this interface.
- ISuiteListener: You can use this interface to listen for events, like suite start and suite finish, that occur throughout the execution of a test suite. You can execute operations before or after the whole suite is executed by putting this interface into practice.
- IReporter: Using this interface, you may create personalized HTML reports according to the test findings. You can alter the structure and content of the test reports that TestNG generates by putting this interface into practice.
Reporting: After a test is run, TestNG creates comprehensive HTML reports that include information on the tests that were passed, failed, and skipped. To help with debugging and analysis, these reports frequently include stack traces, screenshots, and other pertinent data.
Build Tool Integration: Popular build technologies like Maven and Gradle may be easily integrated with TestNG to enable automatic testing during the build process. As a result, the CI/CD pipeline’s tests are executed continuously, enhancing the reliability and quality of the program.
The pytest blog entry on the Python test framework could be interesting to you. https://dasfascination.com/10-fantastic-pytest-features/