Welcome to the TestNG Annotations Tutorials with Java repository! This repository serves as a comprehensive guide to working with TestNG annotations in Java. Whether you're new to TestNG or an experienced developer looking to enhance your testing skills, this repository covers various TestNG annotations and their usage in test automation.
Follow these steps to set up your development environment and start using TestNG with Java.
- Java JDK
- Choose one of the following IDE:
- Apache Maven
- Java JDK
- Download the installer from the Oracle Java SE Downloads page.
- Run the installer and set up the JDK.
- IDE
- Download and install your preferred IDE.
- Apache Maven
- Download Apache Maven from the official website.
- Follow installation instructions.
- Download the JDK installer from Oracle.
- Run the installer and select your installation folder.
- Set up JAVA_HOME and PATH environment variables: - Open System Properties. - Navigate to Environment Variables. - Add JAVA_HOME with the JDK installation path. - Update the PATH variable to include %JAVA_HOME%\bin.
- Verify Java installation by running java --version in a terminal.
- Download Spring Tools from the official website.
- Run the installer and select the package to install.
- Choose your installation folder and complete the setup.
- Launch Spring Tools to start using it.
- Check if you have Java installed by running java --version.
- Download Apache Maven from the official website.
- Extract the archive to your desired location.
- Set up M2_HOME and MAVEN_HOME environment variables:
- Create M2_HOME and point it to the Maven installation directory.
- Update the PATH variable to include %M2_HOME%\bin.
- Verify Maven installation by running mvn --version in a terminal.
- Java JDK - Java Development Kit
- Spring Tools - IDE for Spring applications
- Apache Maven - Dependency Management
To start using the framework:
- Fork the repository.
- Clone, i.e, download your copy of the repository to your local machine using
git clone https://github.com/ebrahimhossaincse/TestNG-Annotations-Tutorials-Java.git
- Import the project into your preferred IDE (Eclipse, IntelliJ IDEA, or Spring Tools).
- Make any desired changes or additions to the project.
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.13.0</version>
</dependency>
<!--
https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.5.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.8.0</version>
<scope>test</scope>
</dependency>
</dependencies>
The @BeforeSuite annotation in TestNG is used to indicate that a specific method will be executed before the execution of all tests in the suite. This annotation is typically used to perform setup activities that are required for the entire test suite.
- Initializing global resources required for the entire test suite.
- Configuring environment settings such as database connections, web server setup, etc.
- Preparing test data that will be used across multiple test cases.
- There can only be one method annotated with @BeforeSuite in a test suite. If multiple methods are annotated with @BeforeSuite, TestNG will throw an exception.
- The @BeforeSuite method is executed only once before the entire test suite runs.
The @AfterSuite annotation in TestNG is used to indicate that a specific method will be executed after the execution of all tests in the suite has completed. This annotation is typically used to perform cleanup activities or actions that need to be done once after the entire suite has finished running.
- Closing resources or connections that were opened for the entire suite.
- Releasing memory or performing finalization tasks.
- Generating test reports or sending notifications after all tests have finished.
- There can only be one method annotated with @AfterSuite in a test suite. If multiple methods are annotated with @AfterSuite, TestNG will throw an exception.
- The @AfterSuite method is executed only once after the entire test suite has run.
The @BeforeTest annotation in TestNG is used to indicate that a specific method will be executed before each test method in a test class. This annotation is typically used to perform setup activities that are required before each individual test method runs.
- Initializing test-specific resources or data.
- Setting up preconditions required for individual test methods.
- Preparing the environment for each test method.
- Each test class can have multiple methods annotated with @BeforeTest. All these methods will be executed before each test method in the class.
- The @BeforeTest methods are executed in the order they are defined in the test class.
The @AfterTest annotation in TestNG is used to indicate that a specific method will be executed after each test method in a test class has completed its execution. This annotation is typically used to perform cleanup activities or actions that need to be done after each individual test method finishes.
- Closing resources or connections that were opened for a specific test.
- Releasing memory or performing finalization tasks after each test.
- Logging or reporting after each test is completed.
- Each test class can have multiple methods annotated with @AfterTest. All these methods will be executed after each test method in the class.
- The @AfterTest methods are executed in the reverse order of how they are defined in the test class.
The @BeforeClass annotation in TestNG is used to indicate that a specific method will be executed once before the first test method in the current class runs. This annotation is typically used to perform setup activities that are shared by all test methods in the class.
- Initializing class-level resources or data.
- Setting up preconditions shared by all test methods in the class.
- Performing setup tasks that are common to all test methods in the class.
- Each test class can have only one method annotated with @BeforeClass. If multiple methods are annotated with @BeforeClass, TestNG will throw an exception.
- The @BeforeClass method is executed only once before any test method in the class runs.
The @AfterClass annotation in TestNG is used to indicate that a specific method will be executed once after all the test methods in the current class have completed their execution. This annotation is typically used to perform cleanup activities or actions that need to be done once after all test methods in the class have run.
- Closing resources or connections that were opened for the entire class.
- Releasing memory or performing finalization tasks for the entire class.
- Generating class-level reports or performing class-level cleanup.
- Each test class can have only one method annotated with @AfterClass. If multiple methods are annotated with @AfterClass, TestNG will throw an exception.
- The @AfterClass method is executed only once after all test methods in the class have run.
The @BeforeMethod annotation in TestNG is used to indicate that a specific method will be executed before each test method in a test class. This annotation is typically used to perform setup activities that are required before each individual test method runs.
- Initializing method-specific resources or data.
- Setting up preconditions required for individual test methods.
- Preparing the environment for each test method.
- Each test class can have multiple methods annotated with @BeforeMethod. All these methods will be executed before each test method in the class.
- The @BeforeMethod methods are executed in the order they are defined in the test class.
The @AfterMethod annotation in TestNG is used to indicate that a specific method will be executed after each test method in a test class has completed its execution. This annotation is typically used to perform cleanup activities or actions that need to be done after each individual test method finishes.
- Closing resources or connections opened for a specific test method.
- Releasing memory or performing finalization tasks after each test method.
- Logging or reporting after each test is completed.
- Each test class can have multiple methods annotated with @AfterMethod. All these methods will be executed after each test method in the class.
- The @AfterMethod methods are executed in the reverse order of how they are defined in the test class.
The @Test annotation in TestNG is used to indicate that a specific method is a test method. This annotation is typically used to define the actual test cases that should be executed.
- Defining the test logic that should be executed.
- Running specific test cases within a test class.
- Each method annotated with @Test is considered a separate test case.
- TestNG executes the test methods in the order they are defined in the test class.
- TestNG provides various assertions (assert, assertEquals, assertTrue, etc.) to validate the test results.
The @DataProvider annotation in TestNG is used to supply test methods with data. It allows you to separate the data from the test methods, making it easier to maintain and reuse test data across multiple test cases
- Supplying test methods with different sets of test data.
- Executing the same test method with multiple input data.
- Parameterizing tests to cover various scenarios.
- The test method that uses the @DataProvider annotation should have parameters that match the data provided.
- The test method will be executed multiple times, once for each set of data provided by the data provider.
The @Factory annotation in TestNG is used to create a test class instance multiple times with different sets of data or parameters. This annotation is particularly useful when you need to create instances of a test class dynamically, each with its own set of input data.
- Creating multiple instances of a test class with different inputs.
- Running the same test method with various parameters or data sets.
- Parameterizing tests dynamically.
- The @Factory method must return an array of objects or Object[].
- Each object in the array will be considered an instance of the test class and will execute the test method(s) defined in the test class.
- TestNG creates a new instance of the test class for each object returned by the @Factory method.
The @Ignore annotation in TestNG is used to temporarily disable a test method or a test class. This annotation is helpful when you want to skip certain tests without deleting them, such as when a test is failing due to an issue that needs to be fixed later.
- Temporarily skipping a test method or class.
- Marking tests that are currently failing or not ready for execution.
- Preventing certain tests from running in specific scenarios.
- The @Ignore annotation can be applied to both test methods and test classes.
- When a test method is ignored, TestNG will not execute it, and the method will be marked as "skipped" in the test report.
- Ignored tests will not contribute to the pass or fail status of the overall test suite.
Special thanks to the contributors and the TestNG community for their valuable resources and tutorials.
For questions or feedback, please feel free to reach out:
- Maintainer: Md. Ebrahim Hossain
- Project Link:TestNG-Annotations-Tutorials-Java