Simplify a broad range of testing needs, from unit testing to functional testing to integration testing to regression testing.
Tests in TestNG
Write the business logic of your test and insert TestNG annotations in your code.
Add the information about your test (e.g. the class name, the groups you wish to run, etc…) in a testng.xml file or in build.xml.
▪Suite – Represented by one XML file. It can contain one or more tests and is defined by the <suite> tag.
▪Tests – Represented by <test> and can contain one or more TestNG classes.
▪TestNG class – Java class that contains at least one TestNG annotation. It is represented by the <class> tag and can contain one or more test methods.
▪Test method – Java method annotated by @Test in your source.
How to install TestNG in Eclipse?
▪Goto Help Menu, Click on “Install new Software”
▪Enter the URL (http://beust.com/eclipse/) at Work with field and click on “Add” button.
▪Once you click on “Add”, it will display the screen, enter the Name as “TestNG“. ▪Follow the onscreen Instructions
Different TestNG Annotations
@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 <test> tag is run.
@AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the <test> tag have run.
@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.
@AfterMethod: The annotated method will be run after each test method
TestNG provides this annotation to use the tests run multiple times according to the test data needs.
Use the @DataProvider annotation to define the test data, further use the @Test to parameterize.
The annotated method must return an Object where each Object can be assigned the parameter list of the test method.
In the Test annotation, receives two arguments like name and parallel. ▫name -> provide the name of the dataprovider to be used ▫Parallel -> provide Boolean value to run the test in parallel for different parameters or not.
Example – Same and Different TestNG Classes
TestNG – disable/ignore Test
How to enable or disable a Test in TestNG?
Used to enable or disable a particular test based on the needs of the execution.
Disable/enable a test in TestNG is achieved by setting the enable attribute of the @Test annotation to false/true.
@Test( enabled=false ) è to disable a test ▪@Test( enabled=true ) à to enable a test
Parallel Execution (Multi-Thread mode)
How to perform parallel test execution using TestNG?
TestNG provides a mechanism to execute Tests, Suites and classes in parallel or single Test in multiple threads.
Based on the test suite configuration, different threads are started simultaneously and the test methods are executed.
Create TestNG XML file for parallel execution, at the Suite tag specify attribute “parallel” as methods or classes or suites as needed by the test execution.
Parallel Execution – Example
Create the TestNG XML file as below and execute using the TestNG runner
TestNG class file to demonstrate Tests in parallel execution and showing Thread id and Thread names in parallel
How to add dependency on tests using TestNG?
Many times during the test execution we encounter situation where few other tests should be executed and those tests may be a suite or test or classes.
TestNG provides an opportunity to resolve.
Single/Multiple Test method dependency using “dependsOnMethods”
Test Group dependency using “dependsOnGroups”
Dependency – Single/Multiple Methods
TestNG – Groups
How to group tests in TestNG?
TestNG offers flexibility to group tests into category, and then execute tests based on groups.
Use the groups attribute under @Test annotation to create the group of tests.
Run the TestNG classes using any one of the following ▫Run Configuration XML configuration
Groups – Example Code
Groups – Example XML file
Sample TestNG XML file
Test NG – Listeners
An Interface that modifies the default TestNG’s behavior.
Listeners “listen” to the event defined in the selenium script and behave accordingly.
Selenium by implementing Listeners Interface.
Allows customizing TestNG reports or logs. Many types of TestNG listeners available
OnStart – Method is called when any Test starts.
onTestSuccess – Method is called on the success of any Test.
onTestFailure – Method is called on the failure of any Test.
onTestSkipped – Method is called on skipped of any Test.
onTestFailedButWithinSuccessPercentage – Method is called each time Test fails but is within success percentage.
onFinish – Method is called after all Tests are executed.
Steps to create a TestNG Listener
How to create TestNG Listeners?
Create class “ListenerImplement” and implement “ITestListener” methods. ▪Create another class for “TestCases”
Using @Listener Annotation – Implement listener in our regular project class i.e. “TestCases”
Execute the “TestCases ” class. Methods in TestCases are called automatically according to the behavior of methods annotated as @Test Verify the Output that logs displays at the console
Listener across multiple Classes
If project has multiple classes adding Listeners to each one of them could be cumbersome and error prone.