@BeforeGroups method not being called

1,100 views
Skip to first unread message

Chris Borrill

unread,
Sep 3, 2006, 5:06:47 PM9/3/06
to testng...@googlegroups.com
I have the following test class which I am using to test how @BeforeGroups works, but it does not work as I expected, the init() method is never executed. Can anyone explain what I have missed?

What is the scope of the @BeforeGroups annotation, is it "class" (runs before all test methods in a class which belong to the group) or "suite" (runs before all test methods in the suite which belong to the group) ?

public class FactoryExample {

@BeforeGroups(groups = { "tabletests" })
public void init() {
Reporter.log("init()");
}

@Test(groups = { "tabletests" })
public void testMe() {
Reporter.log("testMe()");
}
}

thankyou,
Chris
---------------------------------------------------------------------
Posted via Jive Forums
http://forums.opensymphony.com/thread.jspa?threadID=42150&messageID=84327#84327

Alexandru Popescu

unread,
Sep 4, 2006, 11:04:12 AM9/4/06
to testng...@googlegroups.com
#: Chris Borrill changed the world a bit at a time by saying (astral date: 9/4/2006 12:06 AM) :#

> I have the following test class which I am using to test how @BeforeGroups works, but it does not work as I expected, the init() method is never executed. Can anyone explain what I have missed?
>
> What is the scope of the @BeforeGroups annotation, is it "class" (runs before all test methods in a class which belong to the group) or "suite" (runs before all test methods in the suite which belong to the group) ?
>
> public class FactoryExample {
>
> @BeforeGroups(groups = { "tabletests" })
> public void init() {
> Reporter.log("init()");
> }
>
> @Test(groups = { "tabletests" })
> public void testMe() {
> Reporter.log("testMe()");
> }
> }
>
> thankyou,
> Chris

I think the answer is: test level. As you know a suite is composed up from tests. A before/after
group method is invoked around the tests methods making up a group (from the classes included in a
test). Does this answer your question?

./alex
--
.w( the_mindstorm )p.
TestNG co-founder

Ashwin Jayaprakash

unread,
Sep 10, 2006, 3:55:37 AM9/10/06
to testng...@googlegroups.com
I had the same problem with Before and AfterGroups. I've created a group called "oracle", with one class containing the following (below), and another with the actual tests and belonging to the same group. But when I choose and run that "oracle" group from Eclipse, the Before and After methods are never invoked. I even tried moving the Before/AfterGroups methods into the class containing the Test methods, but still no luck.

------------------------
@Test
public class BaseTest {
@BeforeGroups(groups = { "oracle" }, alwaysRun = true)
public void start() throws Exception {
....
something
....
// Test startup.
Connection connection = cruncher.createConnection();
connection.close();
}

@AfterGroups(groups = { "oracle" }, alwaysRun = true)
public void end() throws Exception {
Main.stop();
}
}

------------------------

public class UndoTest {
public void do1() {
System.out.println("do1");
}

@Test(groups = { "oracle" })
public void test() {
Helper helper = new Helper();

List<Entry> remainingEntries = helper.undo(true);
Assert.assertEquals(2, remainingEntries.size());
for (Entry entry : remainingEntries) {
System.out.println(entry.getMethodName());
}
}
}

------------------------

Could you pls explain how this Group/Suite thing works? The documentation and examples in the 5.1 distribution are out of date, referring to @Configuration annotations.

Alex, I didn't understand your explanation of the "Test level" in your previous reply. I am executing the appropriate group (from Eclipse), so why don't the Before/AfterGroups methods get called?

Thanks,
Ashwin (www.JavaForU.com)


---------------------------------------------------------------------
Posted via Jive Forums

http://forums.opensymphony.com/thread.jspa?threadID=42150&messageID=85681#85681

Ashwin Jayaprakash

unread,
Sep 10, 2006, 4:16:44 AM9/10/06
to testng...@googlegroups.com
Hello, I tried creating and running a Suite. But still no luck.

&lt;suite name="Suite" verbose="5" &gt;
&lt;test name="All"&gt;
&lt;groups&gt;
&lt;run&gt;
&lt;include name="oracle" /&gt;
&lt;/run&gt;
&lt;/groups&gt;

&lt;packages&gt;
&lt;package name="test.*" /&gt;
&lt;/packages&gt;
&lt;/test&gt;
&lt;/suite&gt;

Ashwin.


---------------------------------------------------------------------
Posted via Jive Forums

http://forums.opensymphony.com/thread.jspa?threadID=42150&messageID=85683#85683

Alexandru Popescu

unread,
Sep 10, 2006, 5:10:25 AM9/10/06
to testng...@googlegroups.com
I have noticed that BaseTest has a type level @Test annotation. What
is its meaning? Can you try removing it and than rerun both in Eclipse
and the suite?

./alex
--
.w( the_mindstorm )p.
TestNG co-founder

EclipseTestNG Creator

Ashwin Jayaprakash

unread,
Sep 10, 2006, 6:30:11 AM9/10/06
to testng...@googlegroups.com
I removed it and still, the methods don't get invoked.

---------------------------------------------------------------------
Posted via Jive Forums
http://forums.opensymphony.com/thread.jspa?threadID=42150&messageID=85692#85692

Ashwin Jayaprakash

unread,
Sep 10, 2006, 6:41:49 AM9/10/06
to testng...@googlegroups.com
When I changed the BeforeGroups to @BeforeSuite(groups = { "func" }, alwaysRun = true), it started getting invoked.

However, why does this get invoked even if the Suite I'm executing in the testng.xml says "oracle", while the @BeforeSuite says "func"? What is the significance of the "groups" parameter in the Before/AfterSuite annotation?

It would help users a lot if the docs described the possible combinations of all these tags and their meanings. This is one of the best features of TestNG, compared to Junit4.

Ashwin.


---------------------------------------------------------------------
Posted via Jive Forums

http://forums.opensymphony.com/thread.jspa?threadID=42150&messageID=85694#85694

Eran

unread,
Sep 10, 2006, 6:55:12 AM9/10/06
to testng...@googlegroups.com
On 9/10/06, Ashwin Jayaprakash < testng...@opensymphony.com> wrote:

When I changed the BeforeGroups to @BeforeSuite(groups = { "func" }, alwaysRun = true), it started getting invoked.

However, why does this get invoked even if the Suite I'm executing in the testng.xml says "oracle", while the @BeforeSuite says "func"?

From the docs:

alwaysRun For before methods (beforeSuite, beforeTest, beforeTestClass and beforeTestMethod, but not beforeGroups): If set to true, this configuration method will be run regardless of what groups it belongs to.

Regards,
Eran

Alexandru Popescu

unread,
Sep 10, 2006, 6:55:32 AM9/10/06
to testng...@googlegroups.com
On 9/10/06, Ashwin Jayaprakash <testng...@opensymphony.com> wrote:
>
> When I changed the BeforeGroups to @BeforeSuite(groups = { "func" }, alwaysRun = true), it started getting invoked.
>
> However, why does this get invoked even if the Suite I'm executing in the testng.xml says "oracle", while the @BeforeSuite says "func"? What is the significance of the "groups" parameter in the Before/AfterSuite annotation?
>

Hi Ashwin!

Unfortunately I cannot easily reproduce the problem you are
mentioning. If you can send me offline a test that reproduce the
problem than this would be really great.

> It would help users a lot if the docs described the possible combinations of all these tags and their meanings. This is one of the best features of TestNG, compared to Junit4.
>

Please let us know how would you improve the documentation and we will
be very happy to apply the changes you are suggesting.

./alex
--
.w( the_mindstorm )p.
TestNG co-founder
EclipseTestNG Creator

> Ashwin.

Ashwin Jayaprakash

unread,
Sep 10, 2006, 9:48:41 AM9/10/06
to testng...@googlegroups.com
Alex, I couldn't find you email Id. That's why I'm attaching some sample code to reproduce the issue.

The code is really crude, mind you; only to reproduce the issue and that's not how I normally write my programs :-)

Thanks,
Ashwin.


---------------------------------------------------------------------
Posted via Jive Forums

http://forums.opensymphony.com/thread.jspa?threadID=42150&messageID=85724#85724

TestNG_Before_Bug.zip

Alexandru Popescu

unread,
Sep 10, 2006, 12:02:53 PM9/10/06
to testng...@googlegroups.com
Ashwin I am a bit puzzled by the code you have sent: a single class.
What I am supposed to do with it?

./alex
--
.w( the_mindstorm )p.
TestNG co-founder
EclipseTestNG Creator

PS: if you check TestNG site http://testng.org you will notice on the
home page (top right corner) my private email.

On 9/10/06, Ashwin Jayaprakash <testng...@opensymphony.com> wrote:

Ashwin Jayaprakash

unread,
Sep 11, 2006, 12:25:45 AM9/11/06
to testng...@googlegroups.com
Sorry, I missed this file while zipping the files.

------------------

package temp;

import org.testng.Assert;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.AfterGroups;

/*
* Author: Ashwin Jayaprakash Date: Sep 10, 2006 Time: 9:33:34 PM
*/

public class BaseTestCase1 {
public static final String OLD_VAL = "OLD";

public static final String NEW_VAL = "New!!!";

public static String CHECK = OLD_VAL;


@BeforeGroups(groups = { "oracle" }, alwaysRun = true)
public void start() throws Exception {

System.out.println("start");

CHECK = NEW_VAL;
}

@AfterGroups(groups = { "oracle" }, alwaysRun = true)
public void end() throws Exception {

System.out.println("end");
}
}

------------------

Ashwin.


---------------------------------------------------------------------
Posted via Jive Forums

http://forums.opensymphony.com/thread.jspa?threadID=42150&messageID=85839#85839

Ashwin Jayaprakash

unread,
Sep 11, 2006, 10:20:47 PM9/11/06
to testng...@googlegroups.com
Alex, what's going on with this thing? Even the most basic BeforeAfter Groups are not working. Am I missing something very obvious?

Try this (the older attachment was messed up)

-------------------

@Test(groups = { "func", "oracle.sliding" })
public class SimpleTestNG {
protected boolean called = false;

@BeforeGroups
public void init() throws Exception {
called = true;

System.out.println("init");
}

@Test
protected void testSlidingWindow() throws Exception {
Assert.assertEquals(called, true);
}

@AfterGroups
public void discard() {
System.out.println("discard");
}
}

-------------------

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >



<suite name="Suite" verbose="5" >

<test name="All">
<groups>
<run>
<include name=".*" />
</run>
</groups>

<packages>
<package name="temp.*" />
</packages>
</test>
</suite>

-------------------

Ashwin.


---------------------------------------------------------------------
Posted via Jive Forums

http://forums.opensymphony.com/thread.jspa?threadID=42150&messageID=86056#86056

Cédric Beust ♔

unread,
Sep 11, 2006, 10:25:59 PM9/11/06
to testng...@googlegroups.com
You need to put your @BeforeGroups methods in the group that you are including, otherwise they'll be ignored.

--
Cedric

Mark Derricutt

unread,
Sep 11, 2006, 10:48:44 PM9/11/06
to testng...@googlegroups.com
Which means using @BeforeGroups(groups = { "func", "oracle.sliding" })   (I can see this question coming)...

Ashwin Jayaprakash

unread,
Sep 11, 2006, 11:23:55 PM9/11/06
to testng...@googlegroups.com
Nope, still doesn't work :-(

Even tried this one with method level Annos and a static variable:

public class SimpleTestNG {
protected static boolean called = false;

@BeforeGroups(groups = { "func", "oracle.sliding" })

public void init() throws Exception {
called = true;

System.out.println("init");
}

@Test(groups = { "func", "oracle.sliding" })


protected void testSlidingWindow() throws Exception {
Assert.assertEquals(called, true);
}

@AfterGroups(groups = { "func", "oracle.sliding" })


public void discard() {
System.out.println("discard");
}
}


Look at the logs attached. Is there some secret chant, that I'm not uttering while the test runs? Aaarrggh...


---------------------------------------------------------------------
Posted via Jive Forums

http://forums.opensymphony.com/thread.jspa?threadID=42150&messageID=86067#86067

logs.txt

Ashwin Jayaprakash

unread,
Sep 12, 2006, 11:21:48 PM9/12/06
to testng...@googlegroups.com
Got it - I referred this post and realised that the "value" attribute was missing!

But Cedric/Alex, this setting seems confusing. Why keep both a groups and a value tag? They seem to be redundant.

Ashwin.


---------------------------------------------------------------------
Posted via Jive Forums

http://forums.opensymphony.com/thread.jspa?threadID=42150&messageID=86308#86308

Cédric Beust ♔

unread,
Sep 12, 2006, 11:33:52 PM9/12/06
to testng...@googlegroups.com
On 9/12/06, Ashwin Jayaprakash <testng...@opensymphony.com> wrote:

Got it - I referred this post and realised that the "value" attribute was missing!

But Cedric/Alex, this setting seems confusing. Why keep both a groups and a value tag? They seem to be redundant.

They're not:  value is the groups this method will be running before, groups is the group this method belongs to.

Still a big confusing, admittedly, but I can't think of a better way...

--
Cédric

Cédric Beust ♔

unread,
Sep 12, 2006, 11:37:19 PM9/12/06
to testng...@googlegroups.com
Well, actually...  thinking about what I just wrote, you might just be right.

It makes little sense for a @BeforeMethod to be run before group "x" if it doesn't belong to group "x" as well...

Can you guys think of a counter-example?  If not, I'll probably remove one of the attributes.

--
Cedric


On 9/12/06, Cédric Beust ♔ < cbe...@google.com> wrote:



--
Cédric

Ashwin Jayaprakash

unread,
Sep 13, 2006, 1:08:15 AM9/13/06
to testng...@googlegroups.com
A BeforeGroups anno does not have to belong to any group (which would be confusing), it just has to be run before some Test methods in the Groups specified.

If the BeforeGroups says @BeforeGroups(groups = { "grp-a", "grp-b" }), will it get invoked multiple times?

Also, since TestNG does not enforce static/instance level methods, how do the instances get created for the example above? Will the class get instantiated twice - once for grp-a and then again for grp-b? And will the method get invoked twice?

And, what bearing does the Class-level groups have on Before/AfterGroups annos? Don't they become members automatically? You said in your comment above that @Before/After annos must have explicit groups defined.

I feel such scenarios/behaviours need to be documented, now that TestNG has reached version 5.1. Otherwise, people will simply turn to JUnit4, which is straight-forward even though it does not provide such "advanced" features.

Anyway, thanks and "Nice work!".

Ashwin.


---------------------------------------------------------------------
Posted via Jive Forums

http://forums.opensymphony.com/thread.jspa?threadID=42150&messageID=86325#86325

Reply all
Reply to author
Forward
0 new messages