Is there currently a way to disable TestNG test based on a condition
I know you can currently disable test as so in TestNG:
@Test(enabled=false, group={"blah"}) public void testCurrency(){ ... }
I will like to disable the same test based on a condition but dont know how. something Like this:
@Test(enabled={isUk() ? false : true), group={"blah"}) public void testCurrency(){ ... }
Anyone has a clue on whether this is possible or not.
In such cases, annotation @Test(enabled = false) helps to disable this test case. If a test method is annotated with @Test(enabled = false), then the test case that is not ready to test is bypassed. Now, let's see @Test(enabled = false) in action.
TestNG provides the feature of enabling and disabling the test cases. We can disable a set of test cases from getting executed. For example, consider a scenario where a serious bug occurs in a feature due to certain tests, so we need to disable the test cases from being executed.
If you want to ignore a test method, use @Ignore along with @Test annotation. If you want to ignore all the tests of class, use @Ignore annotation at the class level.
Disable Test Cases If you need one or more test cases to be skipped during a test run, you can disable them. To do this: Right-click the desired test cases and select Disable Test Case.
An easier option is to use the @BeforeMethod annotation on a method which checks your condition. If you want to skip the tests, then just throw a SkipException. Like this:
@BeforeMethod protected void checkEnvironment() { if (!resourceAvailable) { throw new SkipException("Skipping tests because resource was not available."); } }
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