💡TestNG
💡 What is TestNG?
TestNG is an open source automated testing framework for Java. It is designed to cover a wide range of test categories like unit, functional, end-to-end, integration, etc. TestNG is more powerful and flexible than the older JUnit framework.
Types of Testing Used with TestNG
🧪 Unit Testing
TestNG is well-suited for writing and managing unit tests to test individual classes, methods, and functions.
It's annotations like @Test, @BeforeMethod, @AfterMethod etc. make it easy to organize unit tests.
👪 Integration Testing
TestNG allows you to create test suites that cover interaction between modules/services. This helps in integration testing.
Dependencies between test methods can be defined for testing workflows.
💻 Functional Testing
TestNG can be used for functional testing of applications to validate that features and end-to-end workflows work as expected.
Data providers help parameterize tests with different test data.
☁️ Acceptance Testing
TestNG combined with Selenium can be used for customer acceptance testing to validate software against customer requirements.
📱 Cross Browser Testing
TestNG integrates well with Selenium to enable running tests across browsers and platforms for cross browser testing.
🎯 Regression Testing
Test suites with a stable set of tests can be rerun using TestNG to catch any regressions during software changes.
👥 Parallel Testing in TestNG
Parallel Attribute
TestNG allows parallel execution of tests by using the 'parallel' attribute in the testng.xml file.
For example:
This runs all the test tags defined in the suite in parallel.
Thread Count
The thread-count attribute can specify how many threads to use for concurrent test execution.
For example:
This will use 5 threads to run the tests in parallel.
Grouping Tests
Tests can be grouped logically and each group can run in parallel.
For example:
Here, TestClass1 and TestClass2 will run in parallel as part of Group1. Similarly, TestClass3 and TestClass4 will run parallel in Group2.
Parallel Modes
TestNG has different parallel modes like tests, classes, instances etc. We can choose the parallelism at suite, test or class level.
So in summary, TestNG provides annotations, thread pools, grouping and parallel modes to easily enable parallel test execution and reduce test time.
TestNG Annotations
@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 tag is run.
@AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the 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.
@Test: The annotated method is a part of a test case
🚦 Test Method Priority in TestNG
Default Priority
❗️If test priority is not defined while running multiple test cases, TestNG assigns all @Test a priority as zero (0).
🏃♂️Now, while running; lower priorities will be scheduled first.
💡If we don’t mention any priority, testng will execute the @Test methods based on alphabetical order of their method names irrespective of their place of implementation in the code.
🥇 Methods with Same Priority
😅There may be a chance that methods may contain same priority.
🔀In those cases, testng considers the alphabetical order of the method names whose priority is same.
So in summary:
Default priority is 0 if not specified
Lower priority tests run first
Alphabetical order for same priority tests
This helps organize test execution order in TestNG based on priority and naming conventions.
What are TestNG Assertions? 🧪
In TestNG, assertions are validations or checkpoints used to verify the expected behavior of an application during automated testing. Assertions help testers determine whether the test cases have passed or failed confidently.
Types of TestNG Assertions 📚
Hard Assertions 💪
Hard assertions in TestNG are used to validate the expected result of a test case. If a hard assertion fails, the test execution is aborted, and the test case is marked as failed.
Soft Assertions (Verify Method) 🌟
Soft assertions in TestNG allow the test execution to continue even if an assertion fails. The test case execution continues till the end, and the results of all assertions are captured. To view the assertions result at the end of the test, the assertAll()
method needs to be invoked.
Here's an example of using TestNG assertions in Java, including both hard and soft assertions:
In this example, we have two test methods that use both hard and soft assertions:
testHardAssertion()
uses two hard assertions to validate the behavior of the Calculator class.testSoftAssertion()
uses two soft assertions to validate the behavior of the Calculator class. TheassertAll()
method is used to capture all the assertions and report them at the end of the test.
Please note that the Calculator
class and its methods are not shown in this example. You can replace them with your own class and methods.
Key differences between TestNG and JUnit
In summary, TestNG has more extensive features and flexibility related to test organization, execution, config and reporting.
Last updated