TestNG Introduction
TestNG is a testing framework that is undeniably the same as JUnit and NUnit with more functionality added to make execution more efficient and powerful. Cedric Beust created the popular test automation framework TestNG in 2003 using the Java programming language. It is an open-source automated testing framework.
TestNG provided solutions for some of the issues faced in Junit and NUnit and gives the developer/testers the ability to write more flexible and powerful tests with help of easy annotations, parameterizing, Data listeners, DataProviders, HTML reports, logs, test case grouping, prioritizing and parallel testing abilities, With multiple excellent features TestNG has quickly become popular among testers.
While TestNG is mostly popular for its annotations, TestNG DataProviders is another handy feature that makes data-driven testing easy and efficient.
With the testing setup, we don’t need any main function in our code for execution.
TestNG annotations are placed over the method with sign @.
For example
Benefits of TestNG
Major advantages of testing are:
· Logs can be generated
· Annotations make code efficient and easy to manage
· Ability to produce HTML Reports of execution
· Test cases can be Grouped & Prioritized
· Parallel testing is possible
· Data Parameterization is possible
Execution Sequence of Annotation
Annotations in TestNG
@BeforeSuite: Method with this annotation will run before all tests in the test suite
@AfterSuite: Method with this annotation will run after all tests in the test suite
@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.
@BeforeClass: Method with this annotation will run before the first test method in the current class
@AfterClass: Method with this annotation will run after the last test method in the current class
@BeforeMethod: Method with this annotation will run before each and every test method
@AfterMethod: Method with this annotation will run after each test method
@Test: The annotated method is a part of a test case.
Few other annotations are bit advanced and are out of scope for this article (we can discuss more details in a future article)
@Parameters: This annotation is used to pass parameters to test methods.
@DataProvider: If we use @DataProvider annotation for any method that means you are using that method as a data supplier. The configuration of @DataProvider annotated method must be as it always returns Object[][] which we can use in @Test annotated method. The @Test method that wants to receive data from this DataProvider needs to use a dataProvider name equals to the name of this annotation.
@Factory: Marks a method as a factory that returns objects that will be used by TestNG as Test classes. The method must return Object[ ].
@Listeners: This annotation is used with the test class. It helps in writing logs and results.
Tests in TestNG are structured at 4 levels.
Suite->Tests->TestNG Class->Test Method
Benefits of using annotations
1. Easy To Learn — The annotations are very easy to learn and execute. There is no predefined rule or format, and the tester just needs to annotate methods using their judgment.
2. We can pass additional parameters to annotations.
3. Annotations are strongly typed, so the compiler will flag any mistakes right away.
4. Test classes no longer need to extend anything (such as Testcase, for JUnit 3).
Author:
QA Detective Conf 2021: Nov 20 & 21' 2021
Get register now!