TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionalities that make it more powerful and easier to use. TestNG is designed to cover all categories of tests: unit, functional, end-to-end, integration, etc…
This page list down all available tutorials on TestNG in this blog. This page will be updated as soon as a new tutorial is published on TestNG.
Setup and Installation
This tutorial discusses following topics in detail:
- Introduction of TestNG
- Advantages of TestNG
- Installing TestNG onto Eclipse
- Creating Java Project with TestNG Dependencies
- Creating your first TestNG class
- Running TestNG test
Begin with TestNG
TestNG uses annotations to help developers to write the tests. Learn about all the annotations provided by TestNG and a brief description of them.
Before and After annotations are mainly used to execute a certain set of code before and after the execution of test methods. These are used to basically set up some variables or configuration before the start of a test execution and then to cleanup any of these things after the test execution ends.
While writing unit tests there can be certain scenarios where we need to verify that an exception is being thrown by the program during execution. TestNG provides a feature to test such scenarios by allowing the user to specify the type of exceptions that are expected to be thrown by a test method during execution. It supports multiple values being provided for verification. If the exception thrown by the test is not part of the user entered list, the test method will be marked as failed.
When executing TestNG tests, there may be some scenarios where you may have to disable a particular test or a set of tests from getting executed. For example, consider a scenario where a serious bug exists in a feature due to certain tests belonging to certain scenarios that cannot be executed. As the issue has already been identified we may need to disable the said test scenarios from being executed.
While running tests there can be cases where certain tests get stuck or may take much more time than expected. In such a case you may need to mark the said test case as fail and then continue. In this tutorial, we will learn to configure TestNG tests to timeout after some configured time duration.
One of the important features of TestNG is parameterization. This feature allows user to pass parameter values to test methods as arguments. This is supported by using the @Parameters annotation.
An important features provided by TestNG is the DataProvider feature. It helps you to write data-driven tests, which essentially means that same test method can be run multiple times with different data-sets. It helps in providing complex parameters to the test methods as it is not possible to do this from XML.
Grouping test methods is one of the most important features of TestNG. In TestNG users can group multiple test methods into a named group. You can also execute a particular set of test methods belonging to a group or multiple groups. This feature allows the test methods to be segregated into different sections or modules.
Learn about following topics in this tutorial:
- Grouping tests example
- Running a TestNG group through Eclipse
- Running a TestNG group through testng.xml
- Writing tests which belong to multiple groups
- Including and excluding groups
- Using regular expressions
- Default group
- Group of groups
Dependency is a feature in TestNG that allows a test method to depend on a single or a group of test methods. This will help in executing a set of tests to be executed before a test method. Method dependency only works if the “depend-on-method” is part of the same class or any of the inherited base class (i.e. while extending a class).
Learn about the @Factory annotation provided by TestNG. @Factory allows tests to be created at runtime depending on certain data-sets or conditions.
While learning TestNG, many people get confused when they read about the @DataProvider and @Factory annotations – what to use when? and what is better? Let’s take a look at both of their functionalities.
Parallelism or multi-threading in software terms is defined as the ability of the software, operating system, or program to execute multiple parts or sub-components of another program simultaneously. TestNG allows the tests to run in parallel or multi-threaded mode. This means that based on the test suite configuration, different threads are started simultaneously and the test methods are executed in them. This gives a user a lot of advantages over normal execution, mainly reduction in execution time and ability to verify a multi-threaded code.
Learn about following topics:
- Advantages of Parallel Tests Execution
- Running test methods in parallel
- Running test classes in parallel
- Running tests inside a suite in parallel
- Configuring a test method to run in multiple threads
Learn how to execute testng tests using maven build file i.e. pom.xml file. This knowledge is important if you are planning to automate your tests and making them part of project build process itself.
Happy Learning !!