[testng-users] Skip TestCase at Runtime

693 views
Skip to first unread message

Benjamin87

unread,
May 23, 2012, 7:50:50 AM5/23/12
to testng...@googlegroups.com

Hi,

I want to achieve the following szenario:

In the @BeforeSuite I want to define an Array with Testcases that should be
skipped:

skipit = new ArrayList<String>();
skipit.add("Test1");

So if the TestSuite starts, all Testcases that should be skipped and
therefore are defined in the skip array, should be set to "Enabled=false" in
the Annotation. Also all Testcases, which depend on the skipped testcases
should depend on the testcase before the skiped one.

Test0 <-- Test1 <-- Test2
Test0<--Test2 (Test1:enabled=false)

So I tried to write my own Annotation Transformer.

My Problem:

The Annotation Transformer is called BEFORE the @BeforeSuite-Lifecycle, so
my skipped array is empty. Is there any way to call it after the
@BeforeSuite?

In general can i modify Annotation after the @BeforeSuite?

If this it not possible(which would be very bad for me). How can i pass
parameters from the .xml Testconfiguration-file, to achieve a skip-list,
which will be noticed from the Annation Transformer?

Thanks for your reply!
--
View this message in context: http://old.nabble.com/Skip-TestCase-at-Runtime-tp33895104p33895104.html
Sent from the testng-users mailing list archive at Nabble.com.

Cédric Beust ♔

unread,
May 23, 2012, 2:55:52 PM5/23/12
to testng...@googlegroups.com
Annotation transformers are run before the annotations are read, by definition (it wouldn't make much sense otherwise).

Another approach would be to define an IMethodInterceptor and remove from that list the methods you don't want to run.

One last thing: "skipped" has a specific meaning in TestNG, you seem to mean "disabled" or "ignored" here.

-- 
Cédric




--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.


Benjamin87

unread,
May 24, 2012, 4:18:37 AM5/24/12
to testng...@googlegroups.com

Yeah, disabled is the right definition.

What would happen to the depend Methods, if i remove an Method A with the
IMethodInterceptor and Method B depends on Method A. Method B would be
skipped or throw an error, right?

In that Case the parameter variant would be the best way. But how can i pass
Parameters from the .xml Configuration File to an Annotation Transformer
Class to modify Annotation before the Suite starts?

The @parameters is a Annotation as well, so it would be read after the
Annotation Transformer?
View this message in context: http://old.nabble.com/Skip-TestCase-at-Runtime-tp33895104p33900460.html

Benjamin

unread,
Jul 6, 2012, 9:21:36 AM7/6/12
to testng-users
So i was little short on time lately, but know i have tried the
MethodInterceptor approach:

public class TestNG_MethodInterceptor extends StartTests implements
IMethodInterceptor {

private HashMap<String,String[]> removedmeths = new
HashMap<String,String[]>();

@Override
public List<IMethodInstance> intercept(List<IMethodInstance> arg0,
ITestContext arg1) {
List<IMethodInstance> result = new ArrayList<IMethodInstance>();
if(skipit != null && skipit.size()>=1)
{
for(IMethodInstance mi:arg0)
{
String methodname = mi.getMethod().getMethodName();
String[] dependmeth =
build_clear_methodsname(mi.getMethod().getMethodsDependedUpon());
Boolean add = true;
for(String s:skipit)
{
if(s.equals(methodname))
{
removedmeths.put(methodname, dependmeth);
add = false;
}
}
for(String s :dependmeth)
{
if(removedmeths.containsKey(s))
{
/*
* Something like "mi.set_depend_on_Method(removedmeths.get(s));
* Setting the depend_on_Method of the actual test to the
depend_on_Methods of the removed test
*/
}
}
if(add)result.add(mi);
}

}
return result;
}

private String[] build_clear_methodsname(String[] input)
{
String[] methnames = new String[input.length];

for(int i=0;i<input.length;i++)
{
methnames[i] = input[i].split("\\.")[input[i].split("\
\.").length-1];
}
return methnames;
}

}

My Problem is, that i dont know how to change the Annoation of an
Method inside of the MethodInterceptor. I want to change the
dependencies for a Method. How can i do this?

Thanks for your replies!


On 24 Mai, 10:18, Benjamin87 <b.soeltenf...@googlemail.com> wrote:
> Yeah, disabled is the right definition.
>
> What would happen to the depend Methods, if i remove an Method A with the
> IMethodInterceptor and Method B depends on Method A. Method B would be
> skipped or throw an error, right?
>
> In that Case the parameter variant would be the best way. But how can i pass
> Parameters from the .xml Configuration File to an Annotation Transformer
> Class to modify Annotation before the Suite starts?
>
> The @parameters is a Annotation as well, so it would be read after the
> Annotation Transformer?
>
>
>
>
>
>
>
>
>
> Cedric Beust wrote:
>
> > Annotation transformers are run before the annotations are read, by
> > definition (it wouldn't make much sense otherwise).
>
> > Another approach would be to define an IMethodInterceptor and remove from
> > that list the methods you don't want to run.
>
> > One last thing: "skipped" has a specific meaning in TestNG, you seem to
> > mean "disabled" or "ignored" here.
>
> > --
> > Cédric
>
> > On Wed, May 23, 2012 at 4:50 AM, Benjamin87
> > <b.soeltenf...@googlemail.com>wrote:
>
> >> Hi,
>
> >> I want to achieve the following szenario:
>
> >> In the @BeforeSuite I want to define an Array with Testcases that should
> >> be
> >> skipped:
>
> >> skipit = new ArrayList<String>();
> >> skipit.add("Test1");
>
> >> So if the TestSuite starts, all Testcases that should be skipped and
> >> therefore are defined in theskiparray, should be set to "Enabled=false"
> >> in
> >> the Annotation. Also all Testcases, which depend on the skipped testcases
> >> should depend on thetestcasebefore the skiped one.

Cédric Beust ♔

unread,
Jul 6, 2012, 11:26:23 AM7/6/12
to testng...@googlegroups.com
You need to use an IAnnotationTransformer if you want to modify an annotation.

-- 
Cédric

Sachin Francis

unread,
Oct 20, 2014, 1:08:27 AM10/20/14
to testng...@googlegroups.com, ced...@beust.com
Hi Cédric,

I got a have a doubt here.

Is it possible to disable / skip tests based some analysis of data fed by Data Provider.

Say my Dataprovider injects data of type A, i want a subset test cases to be run. Say its type B i want another set of tests to be run.

I see that IAnnotationTransformer comes before the BeforeTest, hence i cant do any analysis of my data here.
IMethodInterceptor executes after BeforeTest but before BeforeClass, But BeforeTest executes only once though I run as factory. Hence even this is not helping me.

Is there anything else which i can try?

sheetal singh

unread,
Mar 1, 2017, 12:21:02 PM3/1/17
to testng-users, ced...@beust.com

@Sachin Francis

Did you get a solution for your query, I am also trying something similar.

Based of data provider values I want to disable test rather skip it.


⇜Krishnan Mahadevan⇝

unread,
Mar 1, 2017, 1:12:26 PM3/1/17
to testng...@googlegroups.com
Sheetal,


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

To unsubscribe from this group and stop receiving emails from it, send an email to testng-users+unsubscribe@googlegroups.com.

To post to this group, send email to testng...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages