Annotation in TestNG
The Annotations in TestNG is the key driver. The main annotations are taken from the original link.
The general rule about annotations in TestNG
These above annotations are inherited when placed them on a superclass of TestNG class. The TestNg attributes that go along with the TestNG annotations are given as:
The annotations in TestNg table
|@BeforeSuite||The annotated method runs before all tests present in the suite. It runs only once.|
|@AfterSuite||The annotated method runs after all tests present in the suite. It runs only once.|
|@BeforeTest||The annotated method runs before any test method that belongs to a class and annotated by @Test Annotation or <Test> tag.|
|@AfterTest||The annotated method runs after all test method(s) that belongs to a class and annotated by @Test Annotation or <Test> tag.|
|@BeforeGroups||The annotated method(mainly a configuration method) runs before the first test method that belongs to any of these groups.|
|@AfterGroups||The annotated method(mainly a configuration method) runs after the last test method that belongs to any of these groups.|
|@BeforeClass||The annotated method runs before the first test method in the current class. It runs only once.|
|@AfterClass||The annotated method runs after the last test method in the current class. It runs only once.|
|@BeforeMethod||The annotated method runs before each test method in the current class.|
|@AfterMethod||The annotated method runs after each test method in the current class.|
|alwaysRun||For all before methods like BeforeSuite, BeforeTest, BeforeClass, BeforeMethod except BeforeGroups, if the flag is true|
this annotated configuration method runs irrespective of what groups it belongs to.
For all before methods like AfterSuite, AfterTest, AfterClass, AfterMethod except AfterGroups, if the flag is true
this annotated configuration method runs irrespective of
-What groups, it belongs to.-If there is any failures or warning in the previous methods
-If any of the methods are skipped in the previous section.
|dependsOnGroups||This annotated method depends on the list of groups specified by the users.|
|dependsOnMethods||This annotated method depends on the list of methods specified by the users.|
|enabled||This annotated method checks if the methods of this class/method are enabled.|
|groups||This annotated method belongs to the list of groups specified by the users.|
|inheritGroups||This annotated method will belong to groups specified in the @Test annotation at the class level if set as true.|
|onlyForGroups||This annotated method applicable only for @BeforeMethod and @AfterMethod.|
If specified, then this setup/teardown method will only be invoked if the corresponding test method belongs to one of the listed groups.
|@DataProvider||This annotated method supplies data for a test. The method returns an Object. Each of the object is the parameter list of the test method. The @Test method that wants to use data from this DataProvider needs to use a dataProvider name equals to the name of this annotation.|
|name||It is the data provider’s name. If we omit this, the name of the method will be treated as the data provider.|
|parallel||This attribute controls the concurrency. If set to true, the data provider runs in parallel and supplies data to a test.|
The default value of this attribute is false.
|@Factory||This annotated method instructs TestNG to treat this method as a factory that generates and returns objects(Object). These objects|
are consumed by the TestNG classes.
|@Listeners||This annotated method defines listeners in the test class.|
|value||It is an array of classes that extends org.testng.ITestNGListener.|
|@Parameters||This annotated method tells TestNg on how to pass parameters to a @Test method.|
|value||The list of variables that fill the parameters of this method.|
|@Test||This annotation makes a class or a method a part of the test. It is the main or root of the test cases.|
|alwaysRun||This attribute enables the method to run always even if the dependant method fails.|
|dataProvider||It is the data provider attribute for this test method.|
|dataProviderClass||This attribute tells the class for the lookup for the data provider. If we do not provide this information the data provider will be|
looked at the class of the current test method or one of its base classes. If this attribute is specified, the data provider method
needs to be static in the specified class.
|dependsOnGroups||This attribute tells TestNG that this method is depending on the specified groups.|
|dependsOnMethods||This attribute tells TestNG that this method is depending on the specified methods.|
|description||This attribute talks about the description of this method.|
|enabled||This attribute checks if methods on this class/method are enabled.|
|expectedExceptions||This attribute tells TestNG about the list of exceptions that a test method is expected to throw. If no exception or a|
different than one on this list is thrown, this test will be marked a failure.
|groups||This attribute tells TestNG about the list of groups this class/method belongs to|
|invocationCount||This attribute tells TestNG about the number of times this method should be invoked|
|invocationTimeOut||This attribute tells TestNG about the maximum number of milliseconds this test should take for the cumulated time of|
all the invocationcounts. This attribute will be ignored if the invocationCount is not specified.
|priority||This attribute tells TestNG about the priority for this test method. Lower priorities will be scheduled first.|
|successPercentage||This attribute tells TestNG about the percentage of success expected from this method|
|singleThreaded||This attribute tells TestNG to run a test on a single thread if this attribute set to true. This attribute ignores parallel=”methods”.|
This attribute can only be used at the class level and it will be ignored if used at the method level.
|timeOut||This attribute specifies the maximum number of milliseconds this test should take to finish its execution.|
|threadPoolSize||This attribute tells TestNG the specific size of the thread pool for this method. The method will be invoked from multiple threads|
as specified by invocationCount
What id the sequence of Annotations in TestNG? What is the test suite flow in TestNG?
The sequence of the annotations are as follows:
Before Suite – Before Test – Before Class -DataProvider- Before Method – Test – After Method – After Class – After Test – After Suite
Depending on the TestData we provide the execution count will be as follows
[Before Method – Test – After Method]*N where N is the number of inputs coming from test data.
All @BeforeXXX() methods are preconditions or initialization of resources. And all @AfterXXX() methods are postcondition or reclaim of resources.
We can run the sample script written
NewTest class to see the sequence.
The Before and After Annotations in TestNg
Before and After Annotations are generally used to execute a certain set of test code that needs to be executed during to set up and tear down process. They are used basically to set up some variables or configurations before a test method initiates. Then omit them after the test execution finishes.
The DataProvider Annotation in TestNG
The DataProvider in TestNG is a special method that returns an array to provide the actual values. On these values the method is dependent. It allows us to write data-driven tests. The same test can run multiple times with a different set of data. It helps to provide complex data into our test methods.
Benefits of using Annotations in TestNG
- TestNG is driven by annotations so it can identify the method just by looking at them. So we do not need to provide names in any specific format or we do not need to follow any specific pattern.
- Annotations accept additional parameters which make the life of the test engineer easy.
- The strongly types annotations are compiler checked hence no scope of mistakes.
- Test classes look clean with no extensions to other classes for TestNG. This feature makes a functional tester write his own test case.
What are the parameters in TestNG?
TestNG parameterization is one of the important features that allow test engineers to pass the parameter value to test methods as an argument. It is supported by @Parameters annotation.
What are the dependencies in TestNG?
The dependency feature in TestNG allows a test method to depend on a single or a group of test methods. This feature is similar to the precondition of the test but divided into several methods. Method dependency in general works if the “depend-on-method” is part of the same class or any of the inherited base class(in case the class extends).
What is Test Suite in TestNG?
A test suite is a set of test cases that will test the functional aspect of the software. While the Test Suite is an abstract concept in terms of TestNG code, the concept, however, is implemented in testng.xml.
<suite> is the root or parent tag of the testng.xml and a <suite> tag can hold one or many tests.
The <suite> tag has the below-written attributes:
|name||It is the mandatory attribute and represents the name of the suite.|
|verbose||The level of the run.|
|parallel||The mode that tells if the tests will run on multiple threads.|
|thread-count||Applicable if the above parallel flag is true or enabled.|
This attribute tells how many threads will be carrying out the test.
|annotations||The type of annotations present in our test.|
|time-out||The default time out value for the test.|
What are the Factories in TestNG?
This is one of the excellent features provided by TestNG. With Factory annotation, we can create a test during runtime. This on the fly test creation depends on data sets or some other condition.
How to handle timeout issues in TestNG?
There are certain test cases that may fail due to sync issues or time out issues. In those cases, we can mark those test methods. Once marked, even if the marked test case fails, the execution continues as normal.
Expected Exception and Expected Message information in TestNG
With TestNG, we can cross verify if the application is throwing correctly Exceptions during the test run. TestNG allows the test engineers to specify the type of expected exception during test method execution. It also allows multiple values for verification. In case, the list of the exception does not contain the expected one, it will fail the test case.
How to execute parallel tests in TestNG?
TestNG provides an ability to execute our test cases parallelly or in a multi-threaded way. This configuration is provided in the test suite configuration. Instead of the normal sequential test execution, this mode provides speed and the number of test cases can be executed in the same time frame.