cant get @beforegroups to work

286 views
Skip to first unread message

shay

unread,
Oct 30, 2009, 9:59:09 PM10/30/09
to testng-users
Hi All,

before groups always runs after the test method

here is my code:

@Test(groups={"NewUser"})
public class RegisterCommandTest {

Logger l = LoggerFactory.getLogger(this.getClass());

@BeforeClass
public void beforeClass(ITestContext itc)
{
l.debug("getting injector from context");
m_inj = (Injector) itc.getAttribute("injector");
m_inj.injectMembers(this);

}
public void testExecute(ITestContext itc) throws Exception {

l.debug("do somthing");
}
}

public class BeforeGroupTest {

Logger l = LoggerFactory.getLogger(this.getClass());

@BeforeGroups(groups = { "NewUser"},value={ "NewUser"})
public void preNewUser(ITestContext itc) {
l.debug("perfroming pre groups init");
m_inj = Guice.createInjector(new JUnitModule(), new RequestModule(),
new GenericModule(), new SecurityModule());
itc.setAttribute("injector", m_inj);
}
}

I am using Maven.


any advice,
Shay

Cédric Beust ♔

unread,
Oct 31, 2009, 7:41:59 PM10/31/09
to testng...@googlegroups.com
Hi,

I tried your code and it behaves as expected for me:

BEFORECLASS getting injector from context
BEFOREGROUPS perfroming pre groups init
TESTEXECUTE do somthing

===============================================
Single
Total tests run: 1, Failures: 0, Skips: 0
===============================================

Note that I ran it directly (without Maven).  Can you try without Maven as well?

--
Cédric

shay

unread,
Oct 31, 2009, 7:47:36 PM10/31/09
to testng-users
Hi Cedric,

Thank you for you time.

Shouldn't BEFOREGROUPS run before BEFORECLASS , as all methods of the
class belong to the group?

if @beforeclass is executed before @beforegroup , how do you
recommend that i initiate the injector?

Thanks,
Shay


On Oct 31, 7:41 pm, Cédric Beust ♔ <cbe...@google.com> wrote:
> Hi,
>
> I tried your code and it behaves as expected for me:
>
> BEFORECLASS getting injector from context
> BEFOREGROUPS perfroming pre groups init
> TESTEXECUTE do somthing
>
> ===============================================
> Single
> Total tests run: 1, Failures: 0, Skips: 0
> ===============================================
>
> Note that I ran it directly (without Maven).  Can you try without Maven as
> well?
>
> --
> Cédric
>

Cédric Beust ♔

unread,
Nov 1, 2009, 10:51:06 AM11/1/09
to testng...@googlegroups.com
Hi Shay,

There is no particular reason why @BeforeClass should run before @BeforeGroups or the other way around, so you should not rely on this.  I understand it would make sense in your situation but in other scenarios, @BeforeClass methods have to run before @BeforeGroups.

For example, consider:

class A
  f (group "aa")
  i (group "bb")

class B
  h (group "aa")

If I run group "bb" and "aa", you would see the following order:

beforeClass A
beforeGroups bb
A.i
beforeGroups aa
A.f
beforeClass B
B.h

Back to your problem.

I don't see your m_inj field defined in any of your classes, so I'm guessing it's static or inherited from some base class?

This field looks global enough that you might want to initialize it in a @BeforeSuite method.  Would this solve your problem?

--
Cédric

shay

unread,
Nov 3, 2009, 10:13:07 PM11/3/09
to testng-users
Hi Cedric,

thanks that makes sense.

up until i have been avoiding an XML config , by using only
annotations . is there a way to use suites without an xml?

Thanks,
Shay

Cédric Beust ♔

unread,
Nov 3, 2009, 10:17:20 PM11/3/09
to testng...@googlegroups.com
Hi Shay,

If you don't use an XML file, TestNG will create its own suite to wrap your tests, so @BeforeSuite should work for you anyway.

I'm betting that you will end up having to use an XML file as your tests grow, though...

--
Cédric

Reply all
Reply to author
Forward
0 new messages