In today’s newsletter we are going to discuss
Introduction
TestNG Annotations
TestNG Priority
TestNG Groups
TestNG Parameters
TestNG Data Providers
TestNG Dependent Tests
GitHub Repo
Introduction
TestNG (Test Next Generation) is a popular testing framework for Java designed to make it easier to write, organize, and execute test cases and suites. It is widely used in the software testing community and provides a rich set of features for automated testing.
Adding Dependency of TestNG
Add this dependency in the pom.xml file for TestNG execution. You can get this dependency from the maven repository.
TestNG Annotation
TestNG Annotations is a logic which written in source test code to control the flow of the execution of the tests.
TestNG contains a hierarchy among the annotations. Here are the types of annotations with hierarchy.
1. BeforeSuite - This method in TestNG runs before the execution of all other test methods.2. BeforeTest - This method in TestNG runs before the execution of all the test methods that are inside that folder.
3. BeforeClass - This method in TestNG will run before the first method invokes of the current class.
4. BeforeMethod - This method in TestNG will execute before each test method.
5. Test - This annotation is the important part, where we write actual Test Code.
6. AfterMethod - This method in TestNG will run after each test method is executed.
7. AfterClass - This method in TestNG will execute after all the test methods of the current class execute.
8. AfterTest - This method in TestNG executes after the execution of all the test methods that are inside that folder.
9. AfterSuite - This method in TestNG runs after the execution of all other test methods.
TestNG Priority
In TestNG, you can set test method priorities to control the order in which tests are executed.
Priority check happens after the annotation check by TestNG.
TestNG annotation hierarchy has higher precedence then test priority.
Here are the different use cases can occurred with Test Priority.
1. Test cases with assign priorities
- The larger the priority number, the lower is its priority. So a method with priority 1 will run after the test with priority 0. In the below example order of execution will be testMethodA > testMethodB > testMethodC
2. Test cases with same priorities.
- In this case, TestNG runs the test cases in the alphabetical order. In the below example order of execution will be testMethodA > testMethodB.
3. Test Case with and without priority
- Test cases without the priority attribute are given the "priority" and executed before the methods with priority. In the below example order of execution will be testMethodC > testMethodA > testMethodB
TestNG Groups
TestNG Groups are great way to categorize and group your test methods allowing you to execute specific subsets of tests based on these groupings.
In the below example, smoke test is part of smoke group and regression test is part of regression group.
Here is the how we can execute the group from the XML suite
If we want to exclude some group from execution,
TestNG Parameters
Parameters in TestNG let us run a function many times with different values.
In TestNG file, we can specify the parameters values using the <parameter> element.
Annotate the test method with “@Parameters” & specify parameters names matching with the ones defined in XML file.
TestNG Data Provider
TestNG Data Provider lets you to provide multiple sets of data to your test methods.
Using data provider, we can connect with external provider such as xls, csv and sql.
Create a method that returns a 2D array. Each element in the array represents a set of parameters for a test case.
Annotate test method with “@Test” and specify data provider attribute to link it with the data provider.
TestNG Dependent Tests
The dependent tests in TestNG determine the dependency of a test on a single or group of tests.
GitHub Repository
You can find code of this newsletter issue at this GitHub Repository
“If you are not willing to learn, no one can help you. If you are determined to learn, no one can stop you” - Zig Ziglar
Let’s Connect on Social Media
Connect with me on LinkedIn (4.2K+ Followers) and Twitter.