<p>I have a base class that represents a database test in TestNG, and I want to specify that all classes extending from this class are of a group "db-test", however I have found that this doesn't seem possible.  I have tried the @Test annotation:</p>

<pre><code>@Test(groups = { "db-test" })
public class DBTestBase {
}
</code></pre>

<p>However, this doesn't work because the @Test annotation will try to make a bunch of methods into tests, and warnings/errors pop up in eclipse when the tests are run.</p>

<p>So I tried disabling the test, so at least the groups are assigned:</p>

<pre><code>@Test(enabled = false, groups = { "db-test" })
public class DBTestBase {
}
</code></pre>

<p>but then any @BeforeTest (and other similar annotations) ALSO get disabled... which is of course not what I want.</p>

<p>I would like some way to annotate a class as being of a particular type of group, but it doesn't quite seem possible in TestNG.  Does anyone have any other ideas?</p>

[{'answer_id': 9137, 'author': 'Sam Merrell', 'author_id': 782, 'author_profile': 'https://Stackoverflow.com/users/782', 'pm_score': 1, 'selected': False, 'text': '<p>I\'m not sure how the annotation inheritance works for TestNG but this <a href="http://web.archive.org/web/20100104232432/http://beust.com/weblog/archives/000170.html" rel="nofollow noreferrer">article</a> may be of some use.</p>\n<p>Actually, this may <a href="http://testng.org/doc/documentation-main.html#annotations" rel="nofollow noreferrer">help better</a>, look at inheritGroups.</p>\n'}, {'answer_id': 75209, 'author': 'Alejandro Bologna', 'author_id': 9263, 'author_profile': 'https://Stackoverflow.com/users/9263', 'pm_score': 2, 'selected': False, 'text': "<p>TestNG will run all the public methods from a class with a @Test annotation. Maybe you could change the methods you don't want TestNG to run to be non-public</p>\n"}, {'answer_id': 154080, 'author': 'Jorge Ferreira', 'author_id': 6508, 'author_profile': 'https://Stackoverflow.com/users/6508', 'pm_score': 0, 'selected': False, 'text': '<p>You can specify the @Test annotation at method level that allows for maximum flexibility.</p>\n\n<pre><code>public class DBTestBase {\n\n    @BeforeTest(groups = "db-test")\n    public void beforeTest() {\n        System.out.println("Running before test");\n    }\n\n    public void method1() {\n        Assert.fail(); // this does not run. It does not belong to \'db-test\' group.\n    }\n\n    @Test(groups = "db-test")\n    public void testMethod1() {\n        Assert.assertTrue(true);\n    }\n}\n</code></pre>\n\n<p>Does this works for you or I am missing something from your question.</p>\n'}, {'answer_id': 275342, 'author': 'VonC', 'author_id': 6309, 'author_profile': 'https://Stackoverflow.com/users/6309', 'pm_score': 1, 'selected': False, 'text': '<p>It would seem to me as the following <strong>code-challenge</strong> (community wiki post):</p>\n\n<p>How to be able to execute all test methods of Extended class from the group \'aGlobalGroup\' without:</p>\n\n<ul>\n<li>specifying the \'aGlobalGroup\' group on the Extended class itself ?</li>\n<li>testing non-annotated public methods of Extended class ?</li>\n</ul>\n\n<p>The first answer is easy:<br>\n<strong>add a class TestNG(groups = { "aGlobalGroup" }) on the Base class level</strong></p>\n\n<p>That group will apply to all public methods of both Base class and Extended class.</p>\n\n<p>BUT: even non-testng public methods (with no TestNG annotation) will be included in that group.</p>\n\n<p>CHALLENGE: avoid including those non-TestNG methods.</p>\n\n<pre><code>@Test(groups = { "aGlobalGroup" })\npublic class Base {\n\n    /**\n     * \n     */\n    @BeforeClass\n     public final void setUp() {\n           System.out.println("Base class: @BeforeClass");\n     }\n\n\n    /**\n     * Test not part a \'aGlobalGroup\', but still included in that group due to the class annotation. &lt;br /&gt;\n     * Will be executed even if the TestNG class tested is a sub-class.\n     */\n    @Test(groups = { "aLocalGroup" })\n     public final void aFastTest() {\n       System.out.println("Base class: Fast test");\n     }\n\n    /**\n     * Test not part a \'aGlobalGroup\', but still included in that group due to the class annotation. &lt;br /&gt;\n     * Will be executed even if the TestNG class tested is a sub-class.\n     */\n    @Test(groups = { "aLocalGroup" })\n     public final void aSlowTest() {\n        System.out.println("Base class: Slow test");\n        //throw new IllegalArgumentException("oups");\n     }\n\n     /**\n      * Should not be executed. &lt;br /&gt;\n      * Yet the global annotation Test on the class would include it in the TestNG methods...\n     */\n    public final void notATest() {\n       System.out.println("Base class: NOT a test");\n     }\n\n    /**\n     * SubClass of a TestNG class. Some of its methods are TestNG methods, other are not. &lt;br /&gt;\n     * The goal is to check if a group specify in the super-class will include methods of this class. &lt;br /&gt;\n     * And to avoid including too much methods, such as public methods not intended to be TestNG methods.\n     * @author &lt;a href="http://stackoverflow.com/users/6309/vonc"&gt;VonC&lt;/a&gt;\n     */\n    public static class Extended extends Base\n    {\n        /**\n         * Test not part a \'aGlobalGroup\', but still included in that group due to the super-class annotation. &lt;br /&gt;\n         * Will be executed even if the TestNG class tested is a sub-class.\n         */\n        @Test\n        public final void anExtendedTest() {\n           System.out.println("Extended class: An Extended test");\n        }\n\n        /**\n         * Should not be executed. &lt;br /&gt;\n         * Yet the global annotation Test on the class would include it in the TestNG methods...\n         */ \n        public final void notAnExtendedTest() {\n            System.out.println("Extended class: NOT an Extended test");\n        }\n    }\n</code></pre>\n'}, {'answer_id': 275353, 'author': 'VonC', 'author_id': 6309, 'author_profile': 'https://Stackoverflow.com/users/6309', 'pm_score': 3, 'selected': True, 'text': '<p>The answer is through a custom <strong>org.testng.IMethodSelector</strong>:</p>\n\n<p>Its <strong>includeMethod()</strong> can exclude any method we want, like a public not-annotated method.</p>\n\n<p>However, to register a custom <em>Java</em> MethodSelector, you must add it to the <strong>XMLTest</strong> instance managed by any TestRunner, which means you need your own <strong>custom TestRunner</strong>.</p>\n\n<p>But, to build a custom TestRunner, you need to register a <strong>TestRunnerFactory</strong>, through the <strong>-testrunfactory</strong> option.</p>\n\n<p>BUT that -testrunfactory is NEVER taken into account by <strong>TestNG</strong> class... so you need also to define a custom TestNG class :</p>\n\n<ul>\n<li>in order to override the configure(Map) method, </li>\n<li>so you can actually set the TestRunnerFactory</li>\n<li>TestRunnerFactory which will build you a custom TestRunner,</li>\n<li>TestRunner which will set to the XMLTest instance a custom XMLMethodSelector</li>\n<li>XMLMethodSelector which will build a custom IMethodSelector</li>\n<li>IMethodSelector which will exclude any TestNG methods of your choosing!</li>\n</ul>\n\n<p>Ok... it\'s a nightmare. But it is also a code-challenge, so it must be a little challenging ;)</p>\n\n<p>All the code is available at <a href="http://snippets.dzone.com/posts/show/6446" rel="nofollow noreferrer"><strong>DZone snippets</strong></a>.</p>\n\n<p>As usual for a code challenge:</p>\n\n<ul>\n<li>one java class (and quite a few inner classes)</li>\n<li>copy-paste the class in a \'source/test\' directory (since the package is \'test\')</li>\n<li>run it (no arguments needed)</li>\n</ul>\n\n<hr>\n\n<p><strong>Update from Mike Stone:</strong></p>\n\n<p>I\'m going to accept this because it sounds pretty close to what I ended up doing, but I figured I would add what I did as well.</p>\n\n<p>Basically, I created a Groups annotation that behaves like the groups property of the Test (and other) annotations.</p>\n\n<p>Then, I created a GroupsAnnotationTransformer, which uses IAnnotationTransformer to look at all tests and test classes being defined, then modifies the test to add the groups, which works perfectly with group exclusion and inclusion.</p>\n\n<p>Modify the build to use the new annotation transformer, and it all works perfectly!</p>\n\n<p>Well... the one caveat is that it doesn\'t add the groups to non-test methods... because at the time I did this, there was another annotation transformer that lets you transform ANYTHING, but it somehow wasn\'t included in the TestNG I was using for some reason... so it is a good idea to make your before/after annotated methods to alwaysRun=true... which is sufficient for me.</p>\n\n<p>The end result is I can do:</p>\n\n<pre><code>@Groups({ "myGroup1", "myGroup2"})\npublic class MyTestCase {\n    @Test\n    @Groups("aMethodLevelGroup")\n    public void myTest() {\n    }\n}\n</code></pre>\n\n<p>And I made the transformer work with subclassing and everything.</p>\n'}]
['https://Stackoverflow.com/questions/9044', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/122/']