How to Include @BeforeClass method with groups

2,051 views
Skip to first unread message

Lidia

unread,
Jul 14, 2010, 3:22:32 PM7/14/10
to testng-users
Hello

I'm trying to run different groups of tests. All tests in a given
class depend on a method that's annotated with @BeforeClass. However,
this method is excluded in the group run. Here are the details:

@BeforeClass(alwaysRun = true)
public void openBrowser() {
...
}

@Test(groups = {"Monitoring", "Acceptance", "Configuration" })
public void testLoadDefaultMonitoringService() throws Exception {
...
}

testng.xml contains:

<test name="ConfigurationTest">
<groups>
<run>
<include name="Monitoring"/>
</run>
</groups>
<packages>
<package name="com.sun.glassfish.tests.configuration"/>
</packages>
</test>

During execution I see the following:

[testng] [XmlMethodSelector] Excluding method
com.sun.glassfish.tests.configuration.openBrowser()

What is the way to include all @BeforeClass, @AfterClass methods when
running a specific group of tests?

Thanks
Lidia

Cédric Beust ♔

unread,
Jul 14, 2010, 3:36:36 PM7/14/10
to testng...@googlegroups.com
Hi Lidia,

I can't reproduce this problem.  I'm using code and testng.xml similar to the ones below but I do see the AfterClass run when alwaysRun=true and not being run when it's false.

Could you email me a small test case reproducing this problem?

Thanks.

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




--
Cédric


Lidia

unread,
Jul 14, 2010, 8:11:27 PM7/14/10
to testng-users
Hello Cedric

My bad. Although the excluding line is printed:

[testng] [XmlMethodSelector] Including group : Monitoring
[testng] [XmlMethodSelector] Excluding method
com.sun.glassfish.tests.configuration.openBrowser()

the openBrowser method is in fact executed as seen in later output:

[testng] ======
[testng] TESTCLASS:
com.sun.glassfish.tests.configuration.AdminMonitoringServiceTests
[testng] [TestClass] BeforeClass :
com.sun.glassfish.tests.configuration.AdminMonitoringServiceTests.openBrowser()
[testng] [TestClass] Test :
com.sun.glassfish.tests.configuration.AdminMonitoringServiceTests.testLoadDefaultMonitoringService()
[testng] [TestClass] Test :
com.sun.glassfish.tests.configuration.AdminMonitoringServiceTests.testSimpleModifyMonitoringService()
[testng] [TestClass] AfterClass :
com.sun.glassfish.tests.configuration.AdminMonitoringServiceTests.stopTest()
[testng] [TestClass]
[testng] ======

All is when selecting included groups in testng.xml file, as noted in
my previous post! However, I cannot get the right group executed when
specifying group in ant. In this case group definition (annotation)
is only in java test file and my testng.xml has the following:

<test name="JMSTest">
<classes>
<class
name="com.sun.glassfish.tests.jms.AdminJMSDestinationResourceTests"/>
</classes>
</test>

<test name="ConfigurationTest">
<packages>
<package name="com.sun.glassfish.tests.configuration"/>
</packages>
</test>

I'm still trying to execute "Monitoring" group, as annotated in one
class in com.sun.glassfish.tests.configuration package. Here is an
example from build.xml file:

<target name="run-monitoring" depends="start-selenium-server,compile"
description="Run Selenium Admin GUI Monitoring Tests">
<testng suitename="Admin GUI Monitoring Tests"
classpathref="runtime.classpath"
listeners="org.uncommons.reportng.HTMLReporter"
groups="Monitoring"

The above does not work as expected, as it executes all the tests in
all the packages/classes. Do I need to define all the annotated
groups in testng.xml file, as below, to get it working from ant?

<test name="ConfigurationTest">
<groups>
<define name="monitoring">
<include name="Monitoring"/>
</define>
</groups>
<packages>
<package name="com.sun.glassfish.tests.configuration"/>
</packages>
</test>

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

Cédric Beust ♔

unread,
Jul 14, 2010, 10:27:06 PM7/14/10
to testng...@googlegroups.com
Hi Lidia,

Yes, the "groups" attribute is supported by the ant task, and you can see this for yourself by setting the flag `dumpcommand="true"`, which will output something like this:

   [testng] [TestNGAntTask] TESTNG PASSED @/var/folders/4n/4nPcrk3KGkW43XZ8-HNYM++++TM/-Tmp-/testng6496886501012527924 WHICH CONTAINS:
   [testng] [TestNGAntTask] -d
   [testng] [TestNGAntTask] /Users/cbeust/java/testng/target/test-output
   [testng] [TestNGAntTask] -groups
   [testng] [TestNGAntTask] Injection
 
  [testng] [TestNGAntTask] -suitename
   [testng] [TestNGAntTask] Ant suite
   [testng] [TestNGAntTask] -testname
   [testng] [TestNGAntTask] Ant test
   [testng] [TestNGAntTask] /Users/cbeust/java/testng/src/test/resources/testng.xml
   [testng] [TestNG] Running:
   [testng]   /Users/cbeust/java/testng/src/test/resources/testng.xml
   [testng]

However, this is not going to work because passing groups this way is mutually exclusive with passing a testng.xml file, and if you pass both, testng.xml wins (because it has much more detailed information about the test run, information that might conflict with -groups).

If you want to include certain groups, just specify them in your testng.xml file.

Hope this helps.

--
Cédric



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


Lidia

unread,
Jul 16, 2010, 7:41:44 PM7/16/10
to testng-users
Thanks for the explanation, makes sense. Since we have all our
execution done from ant, I "pick" the right testng.xml file through
ant for now, depending on what groups we want to run. When I have
some time, I'll look into switching to ant completely. If there are
any reasons not to do so, I would be glad to hear.

Thanks
Lidia


On Jul 14, 7:27 pm, Cédric Beust ♔ <ced...@beust.com> wrote:
> Hi Lidia,
>
> Yes, the "groups" attribute is supported by the ant task, and you can see
> this for yourself by setting the flag `dumpcommand="true"`, which will
> output something like this:
>
>    [testng] [TestNGAntTask] TESTNG PASSED
> @/var/folders/4n/4nPcrk3KGkW43XZ8-HNYM++++TM/-Tmp-/testng6496886501012527924
> WHICH CONTAINS:
>    [testng] [TestNGAntTask] -d
>    [testng] [TestNGAntTask] /Users/cbeust/java/testng/target/test-output
>  *  [testng] [TestNGAntTask] -groups
>    [testng] [TestNGAntTask] Injection
>  *  [testng] [TestNGAntTask] -suitename
> > <testng-users%2Bunsu...@googlegroups.com<testng-users%252Buns...@googlegroups.com>

Cédric Beust ♔

unread,
Jul 17, 2010, 12:44:46 AM7/17/10
to testng...@googlegroups.com
Hi Linda,

I recommend moving away from configuring TestNG with the command line and ant as soon as you can.

The command line and ant are meant to be used only to get started quickly, but once your test suite starts growing, using testng.xml is the best way to specify it extensively...

--
Cédric


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


Reply all
Reply to author
Forward
0 new messages