TestNG (Part 1)
TestNG is a testing framework for the Java programming language. It is designed to cover all categories of tests: unit, functional, end-to-end, integration, etc. TestNG is similar to JUnit, but it is more powerful and easier to use. It provides annotations, support for data-driven testing, test configuration through XML files, and more.
TestNG also allows for flexible test execution, including the ability to run tests in parallel, and generate HTML reports. Additionally, TestNG integrates well with build tools like Maven and Gradle and can be easily used with IDEs like Eclipse and IntelliJ IDEA. It is widely used in enterprise applications to automate unit testing and integration testing.
What are TestNG Annotations?
@BeforeMethod: This will be executed before every @test annotated method.
@AfterMethod: This will be executed after every @test annotated method.
@BeforeClass: This will be executed before first @Test method execution. It will be executed one only time throughout the test case.
@AfterClass: This will be executed after all test methods in the current class have been run
@BeforeTest: This will be executed before the first @Test annotated method. It can be executed multiple times before the test case.
@AfterTest: A method with this annotation will be executed when all @Test annotated methods complete the execution of those classes inside the <test> tag in the TestNG.xml file.
@BeforeSuite: It will run only once, before all tests in the suite are executed.
@AfterSuite: A method with this annotation will run once after the execution of all tests in the suite is complete.
@BeforeGroups: This method will run before the first test run of that specific group.
@AfterGroups: This method will run after all test methods of that group complete their execution.
Write TestNG Annotations in the format it will get executed?
Before Suite
Before Test
Before Class
Before Method
Test Case 1
After Method
Before Method
Test Case 2
After Method
After Class
After Test
After SuiteWhy are you using TestNG?
TestNG is a framework used for testing using which we can have control on executing test cases, can generate different reports for executed test cases, can sequence test cases, can tag them, can provide prerequisite,s and also can execute only failed test cases in the next run
How to run only failed test cases in TestNG?
After the first run of an automated test run. Right-click on Project – Click on RefreshA folder will be generated named the “test-output” folder. Inside “test-output” folder, you could find “testng-failed.xml”Run “testng-failed.xml” to execute the failed test cases again.
What is listners in Testng?
Listener is defined as an interface that modifies the default TestNG's behavior. As the name suggests Listeners "listen" to the event defined in the selenium script and behave accordingly. It is used in selenium by implementing Listeners Interface. It allows customizing TestNG reports or logs. There are many types of TestNG listeners available.
There are many types of listeners that allow you to change the TestNG's behavior.
Add a new listener class implementing ITestListener, add unimplemented methods, give your code in particular method, then add the same in xml file - suite levelBelow are the few TestNG listeners:
- IAnnotationTransformer ,
- IAnnotationTransformer2 ,
- IConfigurable ,
- IConfigurationListener ,
- IExecutionListener,
- IHookable ,
- IInvokedMethodListener ,
- IInvokedMethodListener2 ,
- IMethodInterceptor ,
- IReporter,
- ISuiteListener,
- ITestListener .
Above interfaces are called TestNG Listeners. These interfaces are used in selenium to generate logs or customize the Testing reports.