I don't have much experience with TestNG annotations, however I am trying to build a test suite using TestNG framework and POM design pattern for a retail website. I am planning to use a data driven approach. My plan is to drive my test scenarios through excel and not using testng.xml.
For example I will be having number of testsuites, which will be nothing but various class files under a package name TestSuite. TestSuite names will be listed in an excel and user will be allowed to set the run mode of the testsuite by changing the run mode to TRUE/FALSE. Here I am planning to implement a condition check to see if the Run mode is FALSE and accordingly skip the testsuite, i.e. the testsuite class.
Do we have any direct method to achieve the same using TestNG or please suggest any solution for the same with a small example.
Please add the below line of code in the beginning of the test which will skip your test case
throw new SkipException("Skipping the test case");
You can use TestNG's annotation transformer to set the disabled
property of a @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
}
}
Since 6.13 throw new SkipException("Skipping the test case");
won't work. I have to set status before throwing exception, otherwise test status will be failure instead of skipped:
iTestResult.setStatus(TestResult.SKIP);
throw new SkipException("Skipping the test case");
Probably it will be fixed/changed in next releases, please see https://github.com/cbeust/testng/issues/1632 for details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With