EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Software Testing Tutorial TestNG Annotations
Secondary Sidebar
Software Testing Tutorial
  • Basics
    • What is Software Testing
    • Careers in Software Testing
    • Defect Life Cycle in Software Testing
    • Bug Life Cycle
    • Levels of Software Testing
    • Software Testing Life Cycle
    • Software Tester Work
    • Software Testing Principles
    • Software Testing Services
    • Testing Methodologies
    • Test Approaches
    • Types of Software Testing
    • What is a Bug in Software Testing
    • Benefits of Automation Testing
    • What is Automation Testing?
    • Types of Automation
    • Typical Journey of a Software Tester
    • Automation Testing Process
    • Mobile Automation Testing
    • Automation Testing Life Cycle
    • Software Quality Assurance
    • Software Quality Assurance
    • What is Test Environment?
    • Verification and Validation Testing
  • Types of Testing
    • Adhoc Testing
    • Types of System Testing
    • Manual Testing Types
    • Unit Testing Types
    • Unit Testing Benefits
    • Agile Testing
    • What is Agile Testing
    • Acceptance Testing
    • Stress Testing Types
    • Alpha and Beta Testing
    • Application Testing
    • Automation Testing
    • Automation Testing Advantages
    • Benchmark Testing
    • Black Box Testing
    • Domain Testing
    • Dynamic Testing
    • Ecommerce Testing
    • Fuzz Testing
    • Gray Box Testing
    • GUI Testing
    • Installation Testing
    • Interface Testing
    • Interoperability Testing
    • Mainframe Testing
    • Manual Testing
    • Mutation Testing
    • Monkey Testing
    • Negative Testing
    • Penetration Testing
    • Penetration testing phases
    • Penetration testing framework
    • Protocol Testing
    • Recovery Testing
    • Regression Testing
    • Mobile Penetration Testing
    • Accessibility Testing
    • Sanity Testing
    • Scalability Testing
    • Security Testing
    • Spike Testing
    • Stability Testing
    • State Transition Testing
    • Static Testing
    • Gatling Load Testing
    • System Integration Testing
    • Structural Testing
    • Locust Load Testing
    • System Testing
    • Control Flow Testing
    • Unit Testing
    • Cypress testing
    • Volume Testing
    • Web Testing Application
    • What is Exploratory Testing
    • What is Stress Testing
    • What is Usability Testing
    • White Box Testing
    • Types of White Box Testing
    • Compatibility Testing?
    • Use Case Testing
    • Beta Testing
    • Integration Testing
    • Non Functional Testing
    • Non Functional Testing Types
    • What is Functional Testing
    • Functional testing types
    • Cookie Testing
    • Alpha Testing
    • Boundary Value Testing
    • Equivalence Class Testing
    • Glass Box Testing
    • SOA Testing
    • Smoke Testing
    • Visual Testing
    • Visual Paradigm
    • Model-Based Testing
  • Testing techniques
    • Software Testing Methodologies
    • Black Box Testing Techniques
    • Static Testing Techniques
    • Test Case Design Techniques
    • What is Static Analysis
  • Testing tools
    • Manual Testing Tools
    • Visual Testing Tools
    • Automation Testing Tools
    • Functional Testing Tools
    • GUI Testing Tools
    • Penetration Testing Tools
    • Performance Testing Tools
    • SOA Testing Tools
    • Accessibility Testing Tools
    • What is QTP
    • Regression Testing Tools
    • Security Testing Tools
    • Test Management Tools
    • Defect Management Tools
    • Code Coverage Tools
    • Test Coverage Tools
    • Defect Tracking Tools
    • Continuous Integration Tools
    • Install Bugzilla
    • Test data generation tool
    • Unit Testing Tools
    • Web Testing Tools
    • Stress Testing Tools
    • Performance Monitoring Tools
    • Mobile Testing Tools
    • Responsive Testing Tool
    • Cross Browser Testing Tools
    • Risk Based Testing
    • Database Testing Tools
    • WinRunner
    • What is Squish?
    • CubicTest
    • What is WinRM?
    • Bugzilla Tool
    • Code review tools
    • Penetration Testing Open Source Tools
  • Advance
    • Cyclomatic Complexity
    • Decision Table Testing
    • Decision Tree Algorithm
    • What is Continuous Integration
    • Mantis Bug Tracker
    • Equivalence Partitioning
    • Gantt Chart Software
    • Acceptance Testing Types
    • Load testing tools
    • Install TestNG
    • Install Unity
    • Defect Management Process
    • Test Plan Template
    • Testing Interview Questions
    • Testing of Mobile application
    • What is Test Automation Frameworks
    • Test Automation Framework
    • Application of Automation
    • Test Automation Process
    • Automation Testing Roles and Responsibilities
    • What is Instruction Cycle?
    • What is Cucumber?
    • 15 Best Popular Bug Reporting Tools
    • What is Automated Testing?
    • Software Maintenance Types
    • Types of Penetration Testing
    • Software Reliability
    • Best Gantt Chart Software
    • Code Coverage
    • Branch Coverage
    • Decision Coverage
    • Statement Coverage
    • What is Test Case
    • Types of Test Case
    • What is Test Scenario
    • Formal Review
    • Alpha Beta Pruning
    • What is Cyclomatic Complexity?
    • Test Coverage
    • How to Write Test Case
    • Testing Documentation
    • Performance Testing Life Cycle
    • Test Harness
    • Test Strategy
    • Software Incident Management
    • What is Debugging
    • What is Defect?
    • Listeners in TestNG
  • Inteview Questions
    • Automation Testing Interview Questions
    • Manual Testing Interview Questions
    • ISTQB Interview Questions
    • Cucumber Interview Questions
    • Software Testing Interview Questions
    • Penetration Testing Interview Questions

TestNG Annotations

By Yashi GoyalYashi Goyal

testng annotations

Introduction to TestNG Annotations

TestNG Annotations are the comments or notes included in the TestNG coding for providing instructions to the compiler for performing the test execution in a certain planned guided flow? Some of the annotations commonly used in the TestNG codes are @BeforeTest, @AfterTest, @BeforeSuite, @AfterSuite, @Test, @BeforeMethod, @AfterMethod, @BeforeClass, @AfterClass, @Parameters, etc. It is an efficient test automation framework capable of test script execution in a compilation of test cases and parallel execution.

TestNG Annotations

There are various TestNG annotations, and each annotation is used for a different purpose. Annotations with their explanations are mentioned below:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

1. @BeforeSuite: This annotated method is run before all the tests in a test suite is run.

2. @AfterSuite: This annotated method is run after all the tests in a particular test suite is run.

3. @BeforeTest: This annotated method is run before any test method related to the class, which is defined inside the <test> tag.

4. @AfterTest: This annotated method is run after every test method related to the class, which is defined inside the <test> tag, is executed.

5. @BeforeClass: This annotated method is run only once and is executed before the first test method of the mentioned class.

6. @AfterClass: This annotated method is run only once and is executed after all the mentioned/ current class test methods are executed.

7. @Test: This annotated method marks any class or method as a part of a test that needs to be tested. It has various parameters, which are mentioned below:

  • alwaysRun: The test method always run if this is set to true no matter the test case on which it depends gets failed.
  • dataProvider: This defines the name of the data provider for the test case.
  • depends on methods: This defines the list of methods on which that particular method depends on.
  • dependsOnGroups: This defines the list of groups on which that particular method depends on.
  • Description: It defines the description of the method
  • Enabled: It defines whether the method is enabled or not. If it is set to false, then that method is not executed or is considered as not present.
  • Priority: It defines the priority of the particular test method following which it should be executed. Lower Priority values are scheduled first, and if the test cases are not defined as any priority values, then the TestNG assigns a default priority value (0).
  • successPercentage: It defines the percentage of success that is expected from that method.
  • invocationCount: As the name suggests, it specifies the number of times that the method is called.
  • timeOut: It defines the maximum number of times that a particular test case should take to execute. It is defined in milliseconds.
  • InvocationTimeOut: Like timeOut, it specifies the number of milliseconds that a method should take to complete all the invocation counts. Its values are always provided in milliseconds.

8. @BeforeMethod: This annotated method runs before each and every test method.

9. @AfterMethod: This annotated method will run after each and every test method.

10. @BeforeGroups: This annotated method will run before the first test method of the respective group, which is a part of the execution.

11. @AfterGroups: This annotated method will run after all the test methods or after the last method of the respective group is invoked, which is a part of the execution.

12. @DataProvider: This annotated method is used to supply the data to the test method. It can be used with any @test method in order to supply the input. It is used to supply the complex parameters to the @test methods, which cannot be provided in the XML file. Every @DataProvider must return Object[ ] [ ] where each Object[ ] is assigned the list of parameters of test method. It has an attribute as ‘name’, which needs to be provided; if the programmer does not mention it, its value is set to the method’s name. It has one attribute with the name ‘parallel’, which is by default ‘false’. If set to true, then all test cases

13. @Listeners: This annotation is used to define the listeners in a test method.

14. @Parameters: This annotation is used to define the parameters in a test method. ‘Value’ attribute is used containing all the variable names that are used as parameters.

15. @Factory: This annotation is used to mark any test method as a factory and returns the object (Object[ ]) that is used as TestNG classes by the TestNG file.

Advantages of TestNG Annotations

Below given are some of the advantages of TestNG annotations:

  • TestNG is a framework used with the Selenium having annotations that provide an easy and simple to use way to control the flow of execution of automated test cases.
  • TestNG helps to run/ execute the test cases in a parallel manner.
  • TestNG provides the facility of test case prioritization according to the requirements of the programmer.
  • TestNG helps in the grouping of test cases and executing them accordingly.
  • TestNG provides an excellent way of report generation according to the test results.
  • TestNG is strongly typed, which defines the capability of strong error checking.
  • It provides an easy way of parameter passing and providing inputs to the test cases.
  • TestNG can also connect to external data sources.
  • There is no need for a pattern or format that needs to be followed for the execution of test cases. Annotations provide the facility to call the methods according to the specific requirements.
  • TestNG generates HTML-based reports representing the data in a graphical format that is easy and convenient to understand.

Conclusion

The above description clearly explains the various annotations of TestNG and the purpose of each annotation in controlling the flow. Though the annotations of TestNG are very easy to use, and we can use them according to the requirements, but a deep understanding of each annotation should be gathered before using them in the test automation.

Recommended Articles

This is a guide to TestNG Annotations. Here we discuss various TestNG annotations with advantages. You can also go through our other related articles to learn more-

  1. Java Annotations
  2. Install TestNG
  3. Compatibility Testing 
  4. Hibernate Annotations
Popular Course in this category
Selenium Automation Testing Training (11 Courses, 4+ Projects, 4 Quizzes)
  11 Online Courses |  4 Hands-on Projects |  45+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Software Testing Training (11 Courses, 2 Projects)4.9
Penetration Testing Training Program (2 Courses)4.8
TestNG Training (4 Courses, 2 Project)4.7
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more