How to disable all tests in a class

2,683 views
Skip to first unread message

José González Gómez

unread,
Mar 11, 2010, 11:32:33 AM3/11/10
to testng-users
Hi there,

I'm using method level annotations, and I would like to disable all
the tests in a class, how can I do it? I've tried several things
without success, and I finally made a small test case to show my
problem:

1. Failing tests, either of the following options make my tests fail:

@Test
public class DisabledTest
{
public void fail( )
{
Assert.fail( );
}
}

public class DisabledTest
{
@Test
public void fail( )
{
Assert.fail( );
}
}

2. If i try to disable all the tests in the class at the class level,
they are still runned and fail as long as I maintain the method
annotations:

@Test(enabled = false)
public class DisabledTest
{
@Test
public void fail( )
{
Assert.fail( );
}
}

3. If I don't have method annotations, the tests are correctly
disabled:

@Test(enabled = false)
public class DisabledTest
{
public void fail( )
{
Assert.fail( );
}
}

So, is it possible to disable all the tests in a class without
modifying or deleting all the method annotations?

Thanks in advance, best regards
José González

Cédric Beust ♔

unread,
Mar 11, 2010, 12:01:55 PM3/11/10
to testng...@googlegroups.com
Correct:  annotations defined on the class will be overridden by annotation defined on methods, this is intentional.

The easiest way to solve your problem would be to implement an IAnnotationTransformer that would look up the @Test annotation, look up the class name and if it's the class you want to disable, add "enabled=false" to the @Test annotation.

-- 
Cedric


2010/3/11 José González Gómez <jose.gonz...@gmail.com>

--
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.




--
Cédric


José González Gómez

unread,
Mar 11, 2010, 12:28:02 PM3/11/10
to testng-users
Hi Cedric,

What I find puzzling is that none of the method annotations explicitly
define "enabled = true", so I thought in this case the class level
annotation would win, as it's the only one explicitly defining
enabled. So I must understand that a Test annotation implicitly
defines "enabled = true" even if you don't specify it??

Thanks, best regards
José

On 11 mar, 18:01, Cédric Beust ♔ <cbe...@google.com> wrote:
> Correct:  annotations defined on the class will be overridden by annotation
> defined on methods, this is intentional.
>
> The easiest way to solve your problem would be to implement an
> IAnnotationTransformer that would look up the @Test annotation, look up the
> class name and if it's the class you want to disable, add "enabled=false" to
> the @Test annotation.
>
> --
> Cedric
>

> 2010/3/11 José González Gómez <jose.gonzalez.go...@gmail.com>

> > testng-users...@googlegroups.com<testng-users%2Bunsu...@googlegroups.com>


> > .
> > For more options, visit this group at
> >http://groups.google.com/group/testng-users?hl=en.
>
> --

> ***Cédric
> *

Cédric Beust ♔

unread,
Mar 11, 2010, 12:33:54 PM3/11/10
to testng...@googlegroups.com
Hi José,

Correct, take a look at the definition of the @Test annotation:

  /**
   * Whether methods on this class/method are enabled.
   */
  public boolean enabled() default true;

If I didn't specify a default, this attribute would no longer be optional, which is not acceptable.

Because of this, when TestNG parses the value of this attribute, it has no way of knowing whether that value is the default or if it was specified by the user, so in both cases, it will override the value specified on the class annotation.

I can't think of an easy way to work around this limitation right now...

-- 
Cédric



2010/3/11 José González Gómez <jose.gonz...@gmail.com>
Hi Cedric,
To unsubscribe from this group, send email to testng-users...@googlegroups.com.

José González Gómez

unread,
Mar 11, 2010, 3:13:31 PM3/11/10
to testng-users
Hi Cedric,

I've never programmed an annotation, so I didn't know about this...
After RTFM I've noticed you can't tell the difference between a
default value and an explicitly assigned value...

The only thing that comes to mind is to create a specific annotation
to enable / disable tests... that way you could disable a whole class
applying a @Disabled or @Enabled(false) annotation to the class. There
would be no problem about default values because you could check for
the presence of the annotation: a missing annotation would mean an
enabled test method, unless a class annotation is present.

I think this is far more flexible than the current approach: you could
have a completely disabled class and enable individual methods as the
method annotation would override the class annotation. What do you
think? Something worth creating a new feature ticket?

Thanks a lot, best regards
José

On 11 mar, 18:33, Cédric Beust ♔ <cbe...@google.com> wrote:
> Hi José,
>
> Correct, take a look at the definition of the @Test annotation:
>
>   /**
>    * Whether methods on this class/method are enabled.
>    */
>   public boolean enabled() default true;
>
> If I didn't specify a default, this attribute would no longer be optional,
> which is not acceptable.
>
> Because of this, when TestNG parses the value of this attribute, it has no
> way of knowing whether that value is the default or if it was specified by
> the user, so in both cases, it will override the value specified on the
> class annotation.
>
> I can't think of an easy way to work around this limitation right now...
>
> --

> ***Cédric
> *

> > <testng-users%2Bunsu...@googlegroups.com<testng-users%252Buns...@googlegroups.com>

Cédric Beust ♔

unread,
Mar 11, 2010, 5:08:45 PM3/11/10
to testng...@googlegroups.com
No need for a feature ticket, you can implement this yourself :-)

You could write an IMethodInterceptor which would check for the presence of a certain annotation on the class, such as @DisableTests, and if found, would return a list of all the methods that don't belong to that class.


Let me know if you need help on how to implement this.

-- 
Cedric


2010/3/11 José González Gómez <jose.gonz...@gmail.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.




--
Cédric


José González Gómez

unread,
Mar 12, 2010, 4:38:03 AM3/12/10
to testng-users
Ups, let me rephrase the question... do you think this is something
worth having in TestNG out of the box? I think it's a very useful
feature...

Best regards
José

On 11 mar, 23:08, Cédric Beust ♔ <cbe...@google.com> wrote:
> No need for a feature ticket, you can implement this yourself :-)
>
> You could write an IMethodInterceptor which would check for the presence of
> a certain annotation on the class, such as @DisableTests, and if found,
> would return a list of all the methods that don't belong to that class.
>
> Doc:  http://testng.org/javadocs/org/testng/IMethodInterceptor.html
>
> Let me know if you need help on how to implement this.
>
> --
> Cedric
>

> 2010/3/11 José González Gómez <jose.gonzalez.go...@gmail.com>

> > > > <testng-users%2Bunsu...@googlegroups.com<testng-users%252Buns...@googlegroups.com>
> > <testng-users%252Buns...@googlegroups.com<testng-users%25252Bun...@googlegroups.com>

> ***Cédric
> *

Cédric Beust ♔

unread,
Mar 12, 2010, 11:41:01 AM3/12/10
to testng...@googlegroups.com
Hi José,

2010/3/12 José González Gómez <jose.gonz...@gmail.com>

Ups, let me rephrase the question... do you think this is something
worth having in TestNG out of the box? I think it's a very useful
feature...

What's the advantage of doing this with an annotation compared to simply commenting out the line in your testng.xml file?

--
Cédric


José González Gómez

unread,
Mar 15, 2010, 6:36:18 AM3/15/10
to testng-users
On 12 mar, 17:41, Cédric Beust ♔ <cbe...@google.com> wrote:
> Hi José,
>
> 2010/3/12 José González Gómez <jose.gonzalez.go...@gmail.com>

>
> > Ups, let me rephrase the question... do you think this is something
> > worth having in TestNG out of the box? I think it's a very useful
> > feature...
>
> What's the advantage of doing this with an annotation compared to simply
> commenting out the line in your testng.xml file?
>

Well, I'm using the Eclipse Plugin, and I'm currently running all my
tests per project indicating the parent package where the tests are
located, so I don't use a testng.xml file... maybe I should use one?

Sebastien Tardif

unread,
Oct 3, 2013, 4:15:02 PM10/3/13
to testng...@googlegroups.com
When using Eclipse TestNG integration, I never use a testng.xml and it also seems to not use one by default. I know a annotation like @Test(enableAll=false) would be useful.

I can tell you that what I see people are doing in many projects and many organization is to comment out all the @Test of a class.
Reply all
Reply to author
Forward
0 new messages