Session 42 Introduction to TestNG
TestNG
๐งช TestNG Notes (Corrected + Clear Version)
-
In TestNG, we do not use the
mainmethod. -
You need to create test methods in TestNG depending on how many steps are required to execute the test case.
-
To execute a test method, you must use the
@Testannotation on top of it. -
By default, TestNG executes test methods in alphabetical order.
-
To override the alphabetical execution, use the
priorityparameter in the@Testannotation.
Example:@Test(priority = 1) -
If you do not set any priority, TestNG assigns a default priority of
0. -
If multiple test methods have the same priority, then TestNG will again execute them in alphabetical order.
-
You can also provide negative numbers as priority values. (e.g.,
priority = -1). -
TestNG will execute only those methods which have the
@Testannotation. -
If all
@Testannotations are removed or commented, and you try to run the script, it will show:
"No tests found. Nothing was run."
๐ข Execution Order Summary
-
TestNG executes test methods in alphabetical order by default.
-
@Test(priority = <number>)controls the execution order. -
If you provide priority, then alphabetical order is ignored.
-
Priorities can be random numbers (they need not be sequential).
-
Default priority is 0 if not specified.
-
If priorities are the same, TestNG follows alphabetical order.
-
โ Negative values are allowed in priorities.
-
Only methods annotated with
@Testwill be executed.
๐ Creating a Test Suite in TestNG
-
To generate a TestNG XML suite,
Right-click on the package where all test cases are present โTestNGโConvert to TestNGโFinish. -
It generates an XML file (test suite) used to:
-
Run tests in cross-browser environments.
-
Run multiple tests simultaneously (parallel execution).
-
๐ Viewing Reports
-
To view the TestNG report for the first time, refresh the Selenium WebDriver project.
-
After each test execution, refresh the
test-outputfolder to see the updated report.
โ Bonus Suggestions:
-
Try to mention that
@BeforeMethodand@AfterMethodcan be used for setup and cleanup before/after each test method. -
You can also use
dependsOnMethodsfor controlling test dependencies.