Here I am going to explore the TestNG framework so that you can find the framework that fits your testing needs better. TestNG is widely used for testing along with the cucumber framework.
TestNG-Overview
TestNG is an automation testing framework where NG stands for the “Next Generation”. TestNG is inspired by JUnit and NUnit which uses the annotations (@). TestNG overcomes the disadvantages of JUnit and is designed to make end-to-end testing easy. Using TestNG, a proper report can be generated and failed test cases can be executed separately.
Features of TestNG:
It is easy to understand and use.
TestNG has multiple classes, interfaces and methods that will make testers’ tasks easy.
It has Annotations, and is easier to understand and provides parallel testing.
It produces the HTML reports for implementation.
By using this, test cases can be grouped more easily.
It supports Data-driven testing( @ DataProvider)
It is supported by a variety of tools and plugins.
It allows defining dependency of the test method over other methods.
It allows prioritization of test methods.
We can do test groups by using the grouping of test methods.
It has support for parameterizing test cases using @ ParameterAnnotations.
It acts as a compiler for our java code. So we don’t need to write a public static void main(string[]args). Internally it uses Java only.
Dependency for Maven
Include the below snippet in your pom.xml file
Annotations in TestNG
@BeforeSuite: The annotated method will be run before all tests in this suite have run.
@AfterSuite: The annotated method will be run after all tests in this suite have run.
@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.
@BeforeGroups: The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked.
@AfterGroups: The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked.
@BeforeClass: The annotated method will be run before the first test method in the current class is invoked.
@AfterClass: The annotated method will be run after all the test methods in the current class have been run.
@BeforeMethod: The annotated method will be run before each test method.
@AfterMethod: The annotated method will be run after each test method.
@DataProvider:Marks a method as supplying data for a test method. The annotated method must return an Object[][] where each Object[] can be assigned the parameter list of the test 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:Defines listeners on a test class.
@Parameters:Describes how to pass parameters to a @Test method.
@Test:Marks a class or a method as part of the test.
Assertions in TestNG
A test is considered successful if it completed without throwing any exception or if it threw an exception that was expected. Test methods will typically be made of calls that can throw an exception, or of various assertions (using the Java assert keyword). An assert failing will trigger an AssertionError, which in turn will mark the method as failed.
Below is an example test method:
Below we would be discussing most of the commonly used assertions in testNG framework:
Assert.assertEqual(String actual, String expected): This assertion method accepts two parameters i.e. the actual value and expected value to validate if the actual string is equal to the expected string or not. The assertion exception is thrown if both the strings are not equal.
Assert.assertEqual(String actual, String expected, String message): This assertion method is similar to the assertion method discussed above, the only difference is that this method can accept one more string parameter as a message. In case the assertion condition is not met, the assertion error is thrown along with a message passed here.
Assert.assertEquals(boolean actual, boolean expected): This assertion method accepts two boolean values and validates if both are equal or not.
Assert.assertTrue(condition): This assertion method is used to assert whether the condition passed in a parameter returns true or not. If the condition returns false, the assertion error is thrown.
Assert.assertTrue(condition, message): This assertion method is similar to the assertion method discussed in previous one, the only difference is that this method can accept one more string parameter as a message. In case the assertion condition is passed as false, the assertion error is thrown along with a message passed here.
Assert.assertFalse(condition): This assertion method is used to assert whether the condition passed in a parameter returns false or not. If the condition returns true, the assertion error is thrown.
Assert.assertFalse(condition, message): This assertion method is similar to the assertion method discussed in the previous one, the only difference is that this method can accept one more string parameter as a message. In case the assertion condition is passed as true, the assertion error is thrown along with a message passed here.
Assert.assertNull(condition): This assertion method is used to assert whether the condition passed in a parameter returns null or not. If the condition doesn’t return null, the assertion error is thrown.
Assert.assertNotNull(condition): This assertion method is used to assert whether the condition passed in a parameter returns value except null or not. If the condition returns null, the assertion error is thrown.
Best practices for using TestNG in Selenium automation:
Structure Test Suites: Organize your test suites based on logical groupings, such as functional areas or test priorities. Divide tests into smaller, focused suites rather than having one large suite. This improves maintainability and allows for easier test management.
Use Descriptive Test Names: Give meaningful names to your test methods using descriptive language. This makes it easier to understand the purpose of each test and quickly identify failures or issues.
Leverage TestNG Annotations: Utilize TestNG annotations to control the test execution flow and define preconditions and postconditions. Annotations like @BeforeMethod, @AfterMethod, @BeforeClass, and @AfterClass help in setting up and tearing down test environments effectively.
Group and Prioritize Tests: Use TestNG’s @Test annotation’s groups and priority attributes to categorize and prioritize your tests. This allows you to run specific groups of tests based on requirements or execute critical tests first.
Data-Driven Testing: Employ TestNG’s data provider feature for data-driven testing. Separate test data from the test logic by fetching data from external sources like Excel, CSV files, or databases. This enhances test coverage and allows for easy maintenance of test data.
Test Dependencies: Utilize TestNG’s dependsOnMethods or dependsOnGroups attributes to manage dependencies between test methods or groups. This ensures that tests run in a specific order, facilitating smoother test execution.
Parameterized Tests: Use TestNG’s @Parameters annotation to create parameterized tests. This allows you to execute the same test method with different input values, reducing code duplication and making tests more versatile.
TestNG Assertions: Leverage TestNG’s assertion methods (assertEquals(), assertTrue(), etc.) to validate expected outcomes in your tests. Use descriptive messages in assertions to provide clear failure information.
Implement TestNG: listeners to enhance reporting and add custom functionalities. TestNG provides various listeners, such as ITestListener and ISuiteListener, which allow you to capture test results, take screenshots, log additional information, or perform custom actions during test execution.Parallel Test Execution: Utilize TestNG’s parallel execution feature to run tests in parallel, leveraging the available resources and reducing overall test execution time. Configure parallelism at the test method, class, or suite level based on the system capabilities and test requirements.
TestNG Reporters: Extend TestNG reports by implementing custom reporters. TestNG provides options like IReporter and ITestReporter that allow you to generate customized test reports with additional information or integrate with other reporting frameworks.
Continuous Integration: Integrate TestNG with your preferred CI/CD tools like Jenkins or Bamboo for seamless automation and continuous testing. Configure your CI/CD pipeline to trigger TestNG test executions automatically whenever new code changes are pushed.
Maintain Test Environment Independence: Design your tests to be independent of specific test environments. Avoid hard-coding environment-specific details, such as URLs or credentials, by utilizing configuration files, environment variables, or test data providers.
Regular Test Maintenance: Maintain your test suites by reviewing and updating them periodically. Remove redundant or obsolete tests, update assertions as per application changes, and ensure tests are kept up to date with the evolving requirements.
Conclusion
TestNG makes automated tests more structured, readable, maintainable, and user-friendly. It provides powerful features and reporting. We explored in detail all important aspects of TestNG test script using annotations and assert statements.
Thanks for reading!!
Comentários