@BeforeMethod(groups="group1") runs before a test method from group2

1,649 views
Skip to first unread message

kevinstembridge

unread,
Mar 15, 2007, 11:49:07 AM3/15/07
to testng...@googlegroups.com
Hi all,
I'm seeing some behaviour that is a little counter-intuitive and I don't know if it's the intended behaviour or not.

If I define a test class with 2 groups in it, and then I also define 2 separate @BeforeMethods for each group, when I run the test class both BeforeMethods are run before each test regardless of the group that the test belongs to.

For example, if I have the following methods defined:

@Test(groups="group1")
public void test1() {}

@Test(groups="group2")
public void test2() {}

@BeforeMethod(groups="group1")
public void beforeGroup1() {}

@BeforeMethod(groups="group2")
public void beforeGroup2() {}

When I run this test class for both groups, beforeGroup1() and beforeGroup2() will both run before test1() and then they will both run before test2().

I would have expected only beforeGroup1() to run before test1() and only beforeGroup2() to run before test2(). I could probably figure out how to work around it but I thought I'd raise the question because it doesn't seem quite right to me.

BTW, I'm using 5.6beta that Cedric produced yesterday.

Cheers,
Kevin
---------------------------------------------------------------------
Posted via Jive Forums
http://forums.opensymphony.com/thread.jspa?threadID=70839&messageID=132780#132780

Cédric Beust ♔

unread,
Mar 15, 2007, 12:34:11 PM3/15/07
to testng...@googlegroups.com
Kevin,

I just tried your example and I'm seeing what I think is the correct behavior.

Without any groups:

[B] before2
[B] before1
[B] test1
[B] before2
[B] before1
[B] test2

Including group1:

[B] before1
[B] test1

Including group2:

[B] before2
[B] test2

Note that if you are running in parallel, you will see results that might be surprising, but which actually are still correct if you take a look at the thread ID's:

7 [B] before2
7 [B] before1
8 [B] before2
7 [B] test1
8 [B] before1
8 [B] test2

Does this help?

--
Cedric
--
Cédric

kevinstembridge

unread,
Mar 15, 2007, 1:19:29 PM3/15/07
to testng...@googlegroups.com
Hi Cedric,
Thanks for the reply.

Your first example is the one that I thought seemed like strange behaviour. A BeforeMethod for group 2 is run before a test method from group 1.

I'll take your word for it if you think that it's supposed to be that way but it is causing me a bit of a problem because it means that a beforeMethod for group 2 is being run before the BeforeGroups method for group 2.

You end up with something like this:

BeforeGroups group1
BeforeMethod group1
BeforeMethod group2 //This should not run before BeforeGroups group2
Test group1

BeforeGroups group2
BeforeMethod group2
BeforeMethod group1
Test group2

What do you think?

Cheers,
Kevin
---------------------------------------------------------------------
Posted via Jive Forums

http://forums.opensymphony.com/thread.jspa?threadID=70839&messageID=132800#132800

Cédric Beust ♔

unread,
Mar 15, 2007, 1:39:58 PM3/15/07
to testng...@googlegroups.com
Hi Kevin,

On 3/15/07, kevinstembridge < testng...@opensymphony.com> wrote:

Hi Cedric,
Thanks for the reply.

Your first example is the one that I thought seemed like strange behaviour. A BeforeMethod for group 2 is run before a test method from group 1.

This doesn't violate the contract of a @BeforeMethod.  The only guarantee that TestNG makes is that within the same group, a @BeforeMethod will always be invoked before each @Test method.  With this in mind, the following runs are valid:

before1
test1
before2
test2

before1
before2
test1
test2

I'll take your word for it if you think that it's supposed to be that way but it is causing me a bit of a problem because it means that a beforeMethod for group 2 is being run before the BeforeGroups method for group 2.

You end up with something like this:

BeforeGroups group1
BeforeMethod group1
BeforeMethod group2 //This should not run before BeforeGroups group2
Test group1

BeforeGroups group2
BeforeMethod group2
BeforeMethod group1
Test group2

What do you think?

Ah but this is different:  you are now throwing @BeforeGroups methods in the mix.  If I'm not mistaken, you didn't mention these at all in your original email.

The answer is the same, though.  TestNG doesn't make any guarantee on the ordering of @Before methods:  the only guarantee is that they will be run before the test method. 

The following two traces are therefore correct:

BeforeGroup group2
BeforeMethod group2
Test group2

BeforeMethod group2
BeforeGroup group2
Test group2

Do you need an ordering between a BeforeMethod and a BeforeGroups method? 

--
Cédric

kevinstembridge

unread,
Mar 15, 2007, 2:22:22 PM3/15/07
to testng...@googlegroups.com
Right I see what you're getting at.

I guess I don't _need_ an ordering between the BeforeGroups and BeforeMethod because I found that I could use a BeforeClass instead. I'll explain the scenario I've got to show you why it was throwing errors.

This is my situation:

I have two types of test in every test class; check-in and integration.
The integration tests require some initialization to be performed, e.g. instantiate Hibernate session factory, transaction manager, data source etc. and I'm doing this work in a BeforeGroups(group="integration") method.
I have a BeforeMethod for the integration group that starts a transaction rather than having to code it in every test method. But I'm getting an error because the startTransaction method is being run before each check-in group test method, prior to the BeforeGroups method for the integration group, which initializes the transaction manager. I changed the integration initialization method to be @BeforeClass(groups="integration") and everything works fine.

So at the end of the day I guess its not a problem really. But like I said in the initial posting, it seems counter-intuitive to me. I think people would intuitively expect the BeforeGroups method to run before any of the BeforeMethods for the same group. I also think its counter intuitive (and unnecessary) to see a before method for one group running before a test method from a different group.

It also makes me wonder when would I want to use a BeforeGroups method when I can achieve the same thing with a BeforeClass method? I'm still a TestNG novice so maybe I might be completely missing the point :) but it seems like I could use BeforeClass in any situation I might otherwise choose to use BeforeGroups. And it guarantees to run before the BeforeMethod methods for the same group.


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

http://forums.opensymphony.com/thread.jspa?threadID=70839&messageID=132813#132813

Cédric Beust ♔

unread,
Mar 15, 2007, 2:48:48 PM3/15/07
to testng...@googlegroups.com
Hi Kevin,

A quick comment on one point:

On 3/15/07, kevinstembridge <testng...@opensymphony.com > wrote:


It also makes me wonder when would I want to use a BeforeGroups method when I can achieve the same thing with a BeforeClass method? I'm still a TestNG novice so maybe I might be completely missing the point :) but it seems like I could use BeforeClass in any situation I might otherwise choose to use BeforeGroups. And it guarantees to run before the BeforeMethod methods for the same group.

When you are trying to run methods that belong to the same group but that are in a different class, @BeforeClass won't work, so you'll have to use @BeforeGroups...

--
Cédric

kevinstembridge

unread,
Mar 15, 2007, 4:19:46 PM3/15/07
to testng...@googlegroups.com
Of course. I should have known there'd be a good reason behind it.

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

Reply all
Reply to author
Forward
0 new messages