[testng-users] Order of execution for configuration methods involving groups.

104 views
Skip to first unread message

Choey

unread,
Apr 22, 2010, 7:12:43 PM4/22/10
to testng...@googlegroups.com
Hi. I'm trying to execute special setup and teardown logic for a certain group of tests, like so:

public abstract class MyAbstractTestBase
{
@BeforeMethod
protected void setUp()
{
System.out.println("MyTestBase.setUp()");
}

@BeforeMethod(groups = "Group1")
protected void setUpBeforeGroup1()
{
System.out.println("MyTestBase.setUpBeforeGroup1()");
}

@AfterMethod
protected void tearDown()
{
System.out.println("MyTestBase.tearDown()");
}

@AfterMethod(groups = "Group1")
protected void tearDownAfterGroup1()
{
System.out.println("MyTestBase.tearDownAfterGroup1()");
}
}

and

public class MyFooTest extends MyAbstractTestBase
{
@Test(groups = "Group1")
public void testFoo()
{
System.out.println("MyFooTest.testFoo()");
}
}

When I run testFoo(), I see

MyTestBase.setUp()
MyTestBase.setUpBeforeGroup1()
MyFooTest.testFoo()
MyTestBase.tearDown()
MyTestBase.tearDownAfterGroup1()

but should I not see

MyTestBase.setUp()
MyTestBase.setUpBeforeGroup1()
MyFooTest.testFoo()
MyTestBase.tearDownAfterGroup1()
MyTestBase.tearDown()

? That is, should the @AfterMethod for Group1 be executed before the other @AfterMethod?

Thanks!

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

Nalin

unread,
Apr 22, 2010, 7:55:15 PM4/22/10
to testng-users
You can add dependsOnMethods annotation to tearDown() method making it
dependent on tearDownAfterGroup1. Should work.

Choey

unread,
Apr 22, 2010, 8:08:54 PM4/22/10
to testng...@googlegroups.com
But wouldn't that always run tearDownAfterGroup1() before tearDown()?

In MyFooTest:

@Test(groups = "Group2")
public void testFoo2()
{
System.out.println("testFoo2");
}

Executing this results in

MyTestBase.setUp()
MyTestBase.setUpBeforeGroup1()
testFoo2
MyTestBase.tearDownAfterGroup1()
MyTestBase.tearDown()

But I just noticed that setUpBeforeGroup1() is also called... Am I misunderstanding @Before/AfterMethod.groups? I was under the impression that if you specify @Before/AfterMethod.groups, the configuration method would be run iff any of the groups specified for the test is in the groups specified for the configuration method.

Thanks.

Cédric Beust ♔

unread,
Apr 22, 2010, 9:21:45 PM4/22/10
to testng...@googlegroups.com
All you are doing in this code is specify that certain methods belong to a group and others not.  But you are not specifying any ordering between these groups, so the ordering is arbitrary.

You will need to use dependsOnGroups if you want a specific ordering.

Also be careful that if one day you decide to run only the group "Group1", all the methods that don't belong to any group will *not* be run.

-- 
Cédric
--
Cédric

Suk-Hyun Cho

unread,
Apr 23, 2010, 12:33:24 AM4/23/10
to testng...@googlegroups.com
Thanks. I misunderstood dependsOnGroups and dependsOnMethods.

Now, I have setUpBeforeGroup1 annotated as @BeforeMethod(dependsOnGroups = "Group1"), but it is run before setUp(). In order to make setUpBeforeGroup1 depend on setUp(), I annotated it further as @BeforeMethod(dependsOnGroups = "Group1", dependsOnMethods = "setUp"), but I get the following error:

MyAbstractTestBase.setUpBeforeGroup1() is not allowed to depend on MyAbstractTestBase.setUp()

Any idea why this might happen?

Yvesd

unread,
Apr 26, 2010, 12:17:46 PM4/26/10
to testng-users
> Now, I have setUpBeforeGroup1 annotated as @BeforeMethod(dependsOnGroups = "Group1"), but it is run before setUp(). In order to make setUpBeforeGroup1 depend on setUp(), I annotated it further as @BeforeMethod(dependsOnGroups = "Group1", dependsOnMethods = "setUp"), but I get the following error:
>
> MyAbstractTestBase.setUpBeforeGroup1() is not allowed to depend on MyAbstractTestBase.setUp()
>
> Any idea why this might happen?

Hi,

Correct me if I'm wrong, but I think "dependsOnMethods" only applies
to test methods (i.e. methods annotated with @Test) and is therefore
not applicable for @BeforeMethod-annotated methods?

I would create a @BeforeMethod for "Group1" and another @BeforeMethod
for other groups. These two methods can then call a "common" method.
What do you think of that?

Yves

Cédric Beust ♔

unread,
Apr 26, 2010, 12:21:30 PM4/26/10
to testng...@googlegroups.com


On Mon, Apr 26, 2010 at 9:17 AM, Yvesd <yvesd....@gmail.com> wrote:
Correct me if I'm wrong, but I think "dependsOnMethods" only applies
to test methods (i.e. methods annotated with @Test) and is therefore
not applicable for @BeforeMethod-annotated methods?

Correct, you never need to depend on configuration methods, it's implicit.

--
Cédric

Choey

unread,
Apr 26, 2010, 12:55:06 PM4/26/10
to testng...@googlegroups.com
But if that common method happens to be an ungrouped configuration
method, it gets run twice.

Yves Dessertine ☼

unread,
Apr 26, 2010, 12:59:03 PM4/26/10
to testng-users
> But if that common method happens to be an ungrouped configuration  
> method, it gets run twice.

Not if you have:

// Specify all other groups
@BeforeMethod(groups={"Group2", "Group3", "BlahBlahGroup"}
protected void setUp()
{
doCommonSetup();
System.out.println("MyTestBase.setUp()");

}

@BeforeMethod(groups = "Group1")
protected void setUpBeforeGroup1()
{
doCommonSetup();
System.out.println("MyTestBase.setUpBeforeGroup1()");

}




The drawback is that you have to specify each group, but I don't see a
better method yet.

Yves

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

Choey

unread,
Apr 26, 2010, 1:17:43 PM4/26/10
to testng...@googlegroups.com
Hmm, that works. That requires figuring out the set of all groups,
minus the group for which I'm trying to create the configuration
method, but that's not a big issue for me.

Thanks!


On Apr 26, 2010, at 9:59 AM, Yves Dessertine ☼

Suk-Hyun Cho

unread,
May 12, 2010, 9:21:12 PM5/12/10
to testng-users
Actually, that does not work. Specifying groups on a configuration
method only says that the configuration method is in those groups, not
that it will run before those groups and none else. What I want to do
is like @BeforeMethod.forGroups--run the configuration method before
every method in a matching group. So, for a test class with

@BeforeMethod
public void beforeMethodForAll() {
System.out.println("beforeMethodForAll");
}

@BeforeMethod(forGroups = "GroupA")
public void beforeMethodForGroupA() {
System.out.println("beforeMethodForGroupA");
}

@Test
public void testWithoutGroups() {
System.out.println("testWithoutGroups");
}

@Test(groups = "GroupA")
public void testInGroupA() {
System.out.println("testInGroupA");
}

@Test(groups = "GroupB")
public void testInGroupB() {
System.out.println("testInGroupB");
}

running the entire test class in a single suite, and if the order of
execution is testWithoutGroups, then testInGroupA, then testInGroupB,
I would see

beforeMethodForAll
testWithoutGroups
---
beforeMethodForAll
beforeMethodForGroupA
testInGroupA
--
beforeMethodForAll
testInGroupB

Am I overlooking an existing way of achieving this?

On Apr 26, 10:17 am, Choey <choey...@gmail.com> wrote:
> Hmm, that works. That requires figuring out the set of all groups,  
> minus the group for which I'm trying to create theconfiguration method, but that's not a big issue for me.
>
> Thanks!
>
> On Apr 26, 2010, at 9:59 AM, Yves Dessertine ☼  
>
>
>
>
>
> <yvesd.pub...@gmail.com> wrote:
> >> But if that commonmethodhappens to be an ungroupedconfiguration
> >>method, it gets run twice.
>
> > Not if you have:
>
> > // Specify all other groups
> > @BeforeMethod(groups={"Group2", "Group3", "BlahBlahGroup"}
> > protected void setUp()
> > {
> > doCommonSetup();
> > System.out.println("MyTestBase.setUp()");
>
> > }
>
> > @BeforeMethod(groups = "Group1")
> > protected void setUpBeforeGroup1()
> > {
> > doCommonSetup();
> > System.out.println("MyTestBase.setUpBeforeGroup1()");
>
> > }
>
> > The drawback is that you have to specify each group, but I don't see a
> > bettermethodyet.
>
> > Yves
>
> > --
> > 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 athttp://groups.google.com/group/testng-users?hl=en

Cédric Beust ♔

unread,
May 12, 2010, 10:32:50 PM5/12/10
to testng...@googlegroups.com
Isn't this exactly what dependOnGroups does?

-- 
Cédric
--
Cédric

Suk-Hyun Cho

unread,
May 13, 2010, 1:51:13 PM5/13/10
to testng-users
Perhaps, but that is not clear from the javadoc:

@BeforeMethod.dependsOnGroups:

"The list of groups this method depends on. Every method
member of one of these groups is guaranteed to have been
invoked before this method. Furthermore, if any of these
methods was not a SUCCESS, this test method will not be
run and will be flagged as a SKIP."

But the method annotated with @BeforeMethod is not a test method.

I tried the following:

@BeforeMethod(dependsOnGroups = {"Group1", "Group2"})
public void initCommon()
{
System.out.println("initCommon");
}

@BeforeMethod(dependsOnGroups = "Group2")
public void initGroup2()
{
System.out.println("initGroup2");
}

@Test(groups = "Group1")
public void testInGroup1()
{
System.out.println("testInGroup1");
}

@Test(groups = "Group2")
public void testInGroup2()
{
System.out.println("testInGroup2");
}

Running testInGroup1() produces

initCommon
initGroup2
testInGroup1

and running testInGroup2() produces

initCommon
initGroup2
testInGroup2

In this case, dependsOnGroups on the configuration methods made no
difference.
> > testng-users...@googlegroups.com<testng-users%2Bunsubscribe@google groups.com>
> > > > .
> > > > For more options, visit this group athttp://
> > groups.google.com/group/testng-users?hl=en
> > > > .
>
> > > --
> > > 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<testng-users%2Bunsubscribe@google groups.com>
> > .
> > > For more options, visit this group athttp://
> > groups.google.com/group/testng-users?hl=en.
>
> > --
> > 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<testng-users%2Bunsubscribe@google groups.com>
> > .

Cédric Beust ♔

unread,
May 14, 2010, 8:47:00 AM5/14/10
to testng...@googlegroups.com
Your configuration methods do not belong to any groups, so they are depending on empty groups...

-- 
Cedric
Reply all
Reply to author
Forward
0 new messages