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.
- Run TestNG.
▪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.
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 a 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.
- Create a testng.xml and add listeners tag in XML.