How to design better tests using TestNG
Table of contents
No headings in the article.
TestNg is a widely used tool for writing test cases for API, Web, and App automation. TestNg comes with lots of features that can help us to write better automation test cases.
The Main features of TestNG are below:
Different Types of Annotations Support
Support Multi-Threaded Testing
Different Types of Listeners to support reporting
Support of Parallel Test Execution
Annotations Support: TestNg Supports multiple annotations which help us in writing test cases and execution of testNg. Which are below
@ BeforeSuite - method runs before the suites start for execution. And all the methods get executed under this.
@ AfterSuite - method runs after the execution of all test methods present in the suite
@ BeforeTest - method runs before the execution of all test methods that are available in that folder.
@ AfterTest - method runs after the execution of all test methods that are available in that folder.
@ BeforeClass - method runs before the execution of the first method of the current class gets executed
@ AfterClass - method runs after the execution of the first method of the current class gets executed
@ BeforeMethod - method will run before each test method of the class
@ AfterMethod - method will run after each test method of the class.
@BeforeGroups - method will run only once before the executions of all test methods that belong to a particular group.
@AfterGroups - method will run only once after the executions of all test methods that belong to a particular group.
Support Multi-Threaded Testing Support
TestNg Support execution of test cases in a multi-threaded way. This helps us to execute test cases in a faster way and helps us in utilizing the resources in a better way. To Support multi-threaded test execution we can define thread pool size, invocation time, and timeout on the test.
Different Types of Listeners
TestNg Support different types of listeners which help us in generation test reports post-execution of test cases. Some of the common listeners are below.
- ITestListeners
- ISuiteListeners
- IReporter
- IExecutionListener
- IAnnotationTransformer
- IInvokedMethodListener etc.
Support of Parallel Test Execution
TestNg also supports the execution of test cases in parallel. We can execute test cases parallel via methods or by classes.