correct way to share state between before/aftermethod and the test method

155 views
Skip to first unread message

D_R

unread,
Jan 31, 2012, 1:16:15 PM1/31/12
to testng-users
I would do some setup and cleanup after each test case method.
it seems best to use the beforeMethod and afterMethod for this.

The setup involves getting some beans from spring config. and run
clean up on the beans after the test method. Each test method needs
new instances of the beans.

But since these are not class members variables, what is the best way
to make a new instance available to each test method and clean up
after it?
The test methods can run in parallel as well.
Thanks!


@BeforeMethod.
beforeMethod(){
A a = context.getBean("a");
B b = context.getBean("b")
}

testMethod1(){
//use bean a - unique instance
//use bean b - unique instance
}

testMethod2(){
//use bean a - unique instance
//use bean b - unique instance
}

@AfterMethod(){
//run clean up on bean a
}

Cédric Beust ♔

unread,
Jan 31, 2012, 1:47:47 PM1/31/12
to testng...@googlegroups.com
Use ITestContext#getAttribute/setAttribute (look for "injection" in the documentation on how to get a test context).

-- 
Cédric





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


D_R

unread,
Feb 10, 2012, 5:16:58 PM2/10/12
to testng-users
Thanks cedric.

to use the attributes in the context in the beforeMethod/testMethod/
AfterMethod I can set the key as "classname.methodname.keyname" in
beforemethod and use the same key in beforeMethod/testMethod/
AfterMethod.
This would be ok if we say parallel=mehods and method is run once.

However in the case of specifying multiple invocations on the same
methods need to create the keys such as
"classname.methodname.threadid.invocationNumber. keyname" to use the
key in beforemethod/testMethod/Aftermethod.?

The thread id can be retrieved using (thread.currentthread.getid()..
Is there a way to get the current invocationNumber from the
testContext that is injected into beforemethod/aftermethod/
testmethod?

Thanks.

Cédric Beust ♔

unread,
Feb 10, 2012, 5:31:48 PM2/10/12
to testng...@googlegroups.com
Why not keep track of this count in the method itself and make it a part of the key?

-- 
Cédric

Reply all
Reply to author
Forward
0 new messages