Learn Annotations in TestNG in an Easy Effective and Quick Manner

4
36
Annotations in TestNG
Annotations in TestNG
Spread the love

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

AnnotationAttributesMeaning
@BeforeSuiteThe annotated method runs before all tests present in the suite. It runs only once.
@AfterSuiteThe annotated method runs after all tests present in the suite. It runs only once.
@BeforeTestThe annotated method runs before any test method that belongs to a class and annotated by @Test Annotation or <Test> tag.
@AfterTestThe annotated method runs after all test method(s) that belongs to a class and annotated by @Test Annotation or <Test> tag.
@BeforeGroupsThe annotated method(mainly a configuration method) runs before the first test method that belongs to any of these groups.
@AfterGroupsThe annotated method(mainly a configuration method) runs after the last test method that belongs to any of these groups.
@BeforeClassThe annotated method runs before the first test method in the current class. It runs only once.
@AfterClassThe annotated method runs after the last test method in the current class. It runs only once.
@BeforeMethodThe annotated method runs before each test method in the current class.
@AfterMethodThe annotated method runs after each test method in the current class.
alwaysRunFor 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.
dependsOnGroupsThis annotated method depends on the list of groups specified by the users.
dependsOnMethodsThis annotated method depends on the list of methods specified by the users.
enabledThis annotated method checks if the methods of this class/method are enabled.
groupsThis annotated method belongs to the list of groups specified by the users.
inheritGroupsThis annotated method will belong to groups specified in the @Test annotation at the class level if set as true.
onlyForGroupsThis 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.

@DataProviderThis 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.
nameIt is the data provider’s name. If we omit this, the name of the method will be treated as the data provider.
parallelThis 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.

@FactoryThis 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.
@ListenersThis annotated method defines listeners in the test class.
valueIt is an array of classes that extends org.testng.ITestNGListener.
@ParametersThis annotated method tells TestNg on how to pass parameters to a @Test method.
valueThe list of variables that fill the parameters of this method.
@TestThis annotation makes a class or a method a part of the test. It is the main or root of the test cases.

syntax:

@Test
alwaysRunThis attribute enables the method to run always even if the dependant method fails.

@Test(alwaysRun=true)
dataProviderIt is the data provider attribute for this test method.

@Test(dataProvider="testData")
dataProviderClassThis 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.
dependsOnGroupsThis attribute tells TestNG that this method is depending on the specified groups.

@Test(dependsOnMethods="groupName")
dependsOnMethodsThis attribute tells TestNG that this method is depending on the specified methods.

@Test(dependsOnMethods="methodName")
descriptionThis attribute talks about the description of this method.

@Test(description="Actual Test")
enabledThis attribute checks if methods on this class/method are enabled.

@Test(enabled=true/false)
expectedExceptionsThis 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.
groupsThis attribute tells TestNG about the list of groups this class/method belongs to
invocationCountThis attribute tells TestNG about the number of times this method should be invoked
invocationTimeOutThis 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.
priorityThis attribute tells TestNG about the priority for this test method. Lower priorities will be scheduled first.

@Test(priority=2)
successPercentageThis attribute tells TestNG about the percentage of success expected from this method
singleThreadedThis 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.
timeOutThis attribute specifies the maximum number of milliseconds this test should take to finish its execution.
threadPoolSizeThis 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

  1. 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.
  2.  Annotations accept additional parameters which make the life of the test engineer easy.
  3. The strongly types annotations are compiler checked hence no scope of mistakes.
  4. 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:

AttrubuteDescription
nameIt is the mandatory attribute and represents the name of the suite.
verboseThe level of the run.
parallelThe mode that tells if the tests will run on multiple threads.
thread-countApplicable if the above parallel flag is true or enabled.
This attribute tells how many threads will be carrying out the test.
annotationsThe type of annotations present in our test.
time-outThe 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.


Spread the love
READ  Learn How to Work With DataProvider in TestNG Effectively
I am the founder and owner of the blog - TechTravelHub.com, always love to share knowledge on test automation,tools,techniques and tips.I am a passionate coder of Java and VBScript.I also publish articles on Travel ideas and great honeymoon destinations.Apart from these, I am a gear-head,love to drive across India. I have shared lots of articles here on How to travel several parts of India.Customization of cars aka car modification is my another hobby.Get in touch with me on ani01104@gamil.com

4 COMMENTS

  1. […] 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 […] Source: https://www.techtravelhub.com/annotations-in-testng/ […]

  2. […] fromhttps://www.techtravelhub.com/annotations-in-testng/ […]

LEAVE A REPLY

Please enter your comment!
Please enter your name here