views:

47

answers:

1

Hey Guys,

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.

A: 

You have two options:

Your annotation transformer would test the condition and then override the @Test annotation to add the attribute "enabled=false" if the condition is not satisfied.

Cedric Beust
Thanks cedric. I think i will like to explore the 'annotation transformer' option. that sounds more like what am looking for.
Afamee
Thanks again. It didnt take me long to get a working example of this transformer. One thing though is not behaving as i expected. I want to dynamically transform the name of the test that i run (...at least the way it will be displayed on the test result) by calling annot.setTestName(concatString) where annot represents the method annotation but result comes back with original name unchanged. Is there another way to do this?? Hopefully didnt confuse u.
Afamee
You won't be able to override the behavior of a class at runtime, that's just how Java is designed. Instead, I suggest you put the logic that decides what that name is directly into the test so you can return it in getTestName().
Cedric Beust
that is exactly what I did. I thought setTestName("newName") was meant to change the name of the test. I understand calling getTestName() to get name based on logic in test code but then I want to set this newly retrieved name for the test by saying annot.setTestName(newName) or annot.setTestName(getTestName()+"_Modified"). When the test completes, it still has the original name and not the modified name.
Afamee