Advanced Techniques for Exclude and Include
Parameterizing test methods inclusion/exclusion
TestNG allows you to pass data to functions at runtime. To know more about test parameterization please visit this page.
Utilizing regular expressions for inclusion/exclusion
To learn more about how you can include and exclude tests using regular expressions, please visit this page.
Dynamic inclusion/exclusion based on runtime conditions
This means that you want to include or exclude certain conditions during runtime based upon some conditions at runtime. For dynamic inclusion and exclusion based on runtime conditions, you could consider using Behavior Driven Development(BDD) style testing frameworks like Spock. This is an advanced topic. This type of technique is mainly used in Robustness testing. You can read more about about Robustness testing here.
Real-world scenarios and use cases for advanced inclusion/exclusion techniques
- Debugging: During test development, you could be running into unwanted issues. To pinpoint the exact cause of the problem, you could try running one test suite at a time, followed by one test class and then method. In such a case, you would not want to delete or comment your code at all, which isn’t recommended. So, disabling and enabling tests as per need becomes a crucial feature. This can be easily achieved by setting @Test(enabled=false).
- Avoiding Incomplete Tests: There may test methods that you haven’t provided the body for, just left them by defining their method signatures. It would not be wise to run these description less test cases during tests. It would be a better option to disable these tests and enable them after you are satisfied with them. The syntax to use would be @Test(enabled=false).
- Skipping poorly written Tests: When deciding to run entire test suites, it is a good option to disable tests that aren’t well written and produce ambiguous results that could make the execution of entire test suite fail. These include those test cases that don’t always produce the outcome we expect. Use @Ignore annotation or @Test(enabled=false) to skip the test.
- Testing Specific Features: Suppose you have to perform tests on a particular feature or functionality. And because your application is big, you have made these tests in separate groups. In such cases, you can easily disable other tests groups and test your desired feature. The syntax to add a test in a group is @Test(groups={“login”, “cart”}). To exclude the group of feature cart, you simply need to use the <exclude name=”cart” /> tag in testng.xml file. This would allow tests in the login group to execute and the ones in the cart group to be ignored.
What are Exclude and Include Test Methods in TestNG?
TestNG is a popular Java testing framework that stands for Test-Next Generation. It is an advanced form of JUnit, and offers comprehensive features, especially when it comes to integration tests. In this article, we will learn how to include and exclude test cases from executing at the Method, Class, and Suite levels.
Table of Content
- Include Test Methods
- Exclude Test Methods
- Advanced Techniques for Exclude and Include
- Conclusion
- FAQ’s on Exclude Include Test Cases in TestNG
Include and exclude features in TestNG allow us to stop and allow tests from executing on the go. This means that if we have written a test that we do not want to execute in some situation, we can simply ignore that test case, test method, or test suite without having to delete it entirely. This way, if we ever need to run these tests again, all we need to do is simply enable them.