Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable TestNG test based on a condition

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.

like image 934
Afamee Avatar asked Oct 15 '10 20:10

Afamee


People also ask

How do I disable a specific test script using TestNG feature?

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.

Can we disable a test in TestNG?

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.

How do you disable a test method?

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.

How do I disable test cases?

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.


1 Answers

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.");   } } 
like image 185
Bruce Avatar answered Sep 28 '22 16:09

Bruce