Newbie seeks TestNG idiom: how to force @BeforeGroups to run before @DataProvider?

261 views
Skip to first unread message

AE6RT

unread,
Jan 6, 2009, 11:46:35 AM1/6/09
to testng-users
Good day. I am new to TestNG, and am enjoying learning and using it.
Great work.

It took me 30 minutes of head scratching to notice that my
@DataProvider methods were running before my @BeforeGroups method.
Problem is, as I conceived my setup, the @BeforeGroups must be run
before the @DataProvider so config data is initialized for that
@DataProvider.

I'm sure there is an idiom to adopt in solving this problem, but I
don't know what it is. Can someone please suggest an idiom or
workaround?

Thanks.

Cédric Beust ♔

unread,
Jan 6, 2009, 1:55:03 PM1/6/09
to AE6RT, testng-users
Hi Mark,

Data Providers should only be needed to provide data to your test methods, which is why they are run at the very last minute (this is also to save memory since data providers can sometimes consume a lot of it).

If you need data initialized before that, how about using @BeforeClass or @BeforeSuite?

-- 
Cedric
--
Cédric


AE6RT

unread,
Jan 6, 2009, 3:35:45 PM1/6/09
to testng-users
Cedric, thank you for the speedy reply.

Using @BeforeSuite was precisely where I started, although I did not
mention it. @BeforeSuite was intended to mark a setup() method that
loads a required properties file, and @DataProvider was supposed to
use those loaded properties.

Problem was, I did not specify any arguments whatsoever to
@BeforeSuite, which resulted in the annotated method not running at
all. The experiments to fix this in my test included resorting to
@BeforeGroups and @BeforeClass, which I found less than satisfying, as
I wanted to use the more appropriately named "BeforeSuite" to work.

Following your note, I reverted back to using @BeforeSuite, and
"fixed" my test by including alwaysRun=true in its argument list.
Now, the properties file is loaded before the DataProviders are called
and my DataProviders are happy.

I have to ask: should TestNG have silently not run the no-arg,
@BeforeSuite-annotated setup method, and is alwaysRun=true the best/
right way to ensure that the @BeforeSuite method is run before any and
all other methods in the suite?

Thanks much.

Mark
> ***Cédric
> *

Cédric Beust ♔

unread,
Jan 6, 2009, 3:39:57 PM1/6/09
to testng...@googlegroups.com
Hi Mark,

I'm confused about why your @BeforeSuite did not run in the first place.  This is not related to whether it takes arguments or not so I'm betting you were trying to include a few groups and the @BeforeSuite method did not belong to these groups, is this correct?

This is exactly what alwaysRun is for:  make sure a method is run regardless of what groups are being included (otherwise you would have to make it belong to more and more groups as you add them).

-- 
Cedric
--
Cédric


AE6RT

unread,
Jan 6, 2009, 4:17:37 PM1/6/09
to testng-users
Cedric,

Here is the setup() method, with annotations:

@BeforeSuite(alwaysRun = true)
private void setup() {
System.out.println("############# Reading test
properties...");
testProperties = Util.readPropertiesAsResource("test/
register.properties");
}

@Test(groups = { "account" }, dataProvider = "register.properties")
...

The suite only has one group: account. I'm not sure what is meant by
a helper method belonging to a group. I know a @Test method can
belong to a group, but I treated my setup() method as belonging to no
group. Maybe it should belong to the account group? If yes, how to
express that through an annotation?

Mark


On Jan 6, 12:39 pm, Cédric Beust ♔ <cbe...@google.com> wrote:
> Hi Mark,
> ***Cédric
> *

Cédric Beust ♔

unread,
Jan 6, 2009, 4:21:02 PM1/6/09
to testng...@googlegroups.com
With @BeforeSuite(groups = "account"), but it seems to me you really want alwaysRun=true in this case, so you're fine.

-- 
Cedric

--
Cédric


AE6RT

unread,
Jan 6, 2009, 4:23:24 PM1/6/09
to testng-users
Got it. Thank you very much.

Mark
> ***Cédric
> *
Reply all
Reply to author
Forward
0 new messages