Java – conditionally skip TestNG tests
I don't have much experience with TestNG annotations, but I'm trying to build a test suite using the TestNG framework and the POM design pattern of retail websites I intend to use a data - driven approach My plan is to drive my test scenario through Excel instead of using TestNG xml.
For example, I will have multiple test suites, which are just various class files under the package name TestSuite The TestSuite name will be listed in Excel. Users can set the running mode of the test suite by changing the running mode to true / false Here, I plan to implement a condition check to see if the run mode is false and skip TestSuite, the TestSuite class, accordingly
Do we have any direct way to use TestNG to achieve the same purpose, or please use a small example to suggest any solution
Solution
You can use TestNG's annotation transformer to set the disabled property of the @ test annotation to true or false
Example:
public class MyTransformer implements IAnnotationTransformer { public void transform(ITest annotation,Class testClass,Constructor testConstructor,Method testMethod){ if (isTestDisabled(testMethod.getName()))) { annotation.setEnabled(false); } } public boolean isTestDisabled(String testName){ // Do whatever you like here to determine if test is disabled or not } }