testNG is a unit testing framework for Java, helps to run our tests(methods) in a guided manner.
testNG stands for ‘test Next Generation’ – as this offers more flexibility over JUnit (another testing framework to run the methods).
What testNG offers:
- Need not to have test names in sorting order
- User friendly annotations.
- Support for data-driven testing(@DataProvider)
- Support for parameters
- Group wise test run
- Parallel test run
- Allow dependency of one test on another
- Support to enable or disable the test run
We will discuss the above one by one.
NOTE:
- For testNG installation guide, follow http://testng.org/doc/download.html
- Please read this blog before moving to testng.xml
testNG starts with assigning the annotations for the methods in a class, which determines the sequence of running the tests.
Annotations | Description |
@BeforeSuite | The annotated method will be run before all tests in this suite have run. |
@AfterSuite | The annotated method will be run after all tests in this suite have run. |
@BeforeTest | The annotated method will be run before any test method belonging to the classes inside the tag is run (from testng.xml) |
@AfterTest | The annotated method will be run after all the test methods belonging to the classes inside the tag is run (from testng.xml) |
@BeforeGroups | The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked. |
@AfterGroups | The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked. |
@BeforeClass | The annotated method will be run before the first test method in the current class is invoked. |
@AfterClass | The annotated method will be run after all the test methods in the current class have been run. |
@BeforeMethod | The annotated method will be run before each test method. before each @Test |
@AfterMethod | The annotated method will be run after each test method. after each @Test |
@Test | A method as part of the test. |
2 more annotations we use with the above mentioned annotations:
@Parameters and @dataProvider.
Each annotation as mentioned above have some common attributes:
dependsOnGroups
: The list of groups this method depends on.
dependsOnMethods
: The list of methods this method depends on.
enabled
: Whether methods on this class/method are enabled.
groups
: The list of groups this class/method belongs to.
Each test method (as annotated @Test) have some attributes:
description
: The description for this method.
priority
: The priority for this test method. Lower priorities will be scheduled first.
alwaysRun
: If set to true, this test method will always be run even if it depends on a method that failed.
dataProvider
: name of the dataprovider for this test method.
To run the above mentioned annoted tests, we need to create testng.xml file.
Ways to create testng.xml:
- In Eclipse, Right click on the project, Select “TestNG” and click on the “Convert to TestNG” which will create an xml file (can give any name, mostly we give testng.xml).
- Create new xml file and enter the details (see below, and place the file under current project folder)
Sample testng.xml file:
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" > <suite name="MySuite" parallel="none"> <test name="MyTest"> <classes> <class name="tcPkg.test1"/> <class name="tcPkg.test2"/> <class name="tcPkg.test3"/> </classes> </test>
In the above xml file, we can provide any name for the suite and test name, but the class name should be exactly same as the class having the test methods.
Note: TestNG classes do not have main() method.
Ways to run the testng.xml file: [Make sure testng.xml is under current project folder]
Refer ways to run testng.xml
Also refer testNG features.