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