my dog learned polymorphism*
The moose likes Testing and the fly likes Getting Method Names in TestNG Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Getting Method Names in TestNG" Watch "Getting Method Names in TestNG" New topic
Author

Getting Method Names in TestNG

Conor Hoops
Greenhorn

Joined: May 21, 2010
Posts: 2
Howdy, sorry if this question has been asked before ... but i've been searching around and havent been able to find a direct answer for finding method names in testNG.

We've recently switched from Junit to TestNG for parallel testing (explored both options in parallel and have found testNG is a tad more suited to our needs.)
For logging purpouses we get the test method that has invoked startSession() name in JUnit with the command getName(), however there doesnt seem to be an equivalent command in testNG.

i've found another way to get the test name, by using testName=Thread.currentThread().getStackTrace()[2].getMethodName(); however i really doubt the reliability of this command.

Is there a way to find a test method that has invoked another method in testNG?

thanks
Joachim Rohde
Ranch Hand

Joined: Nov 27, 2006
Posts: 423

Is http://testng.org/javadoc/org/testng/ITestNGMethod.html#getMethodName%28%29 doing what you are looking for?
Conor Hoops
Greenhorn

Joined: May 21, 2010
Posts: 2
Howdy, thanks for the reply but that wasnt what i was really looking for, however i managed to come across what i needed

Ill post it here for future reference :


import java.lang.reflect.Method;

@BeforeMethod
protected void startSession(Method method) throws Exception {

testName = method.getName();



Thanks!
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30537
    
150

Conor,
Thanks for posting the solution for anyone who comes across this thread in the future!


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
sriram angajala
Greenhorn

Joined: Apr 23, 2012
Posts: 1
Conor Hoops wrote:Howdy, thanks for the reply but that wasnt what i was really looking for, however i managed to come across what i needed

Ill post it here for future reference :


import java.lang.reflect.Method;

@BeforeMethod
protected void startSession(Method method) throws Exception {

testName = method.getName();



Thanks!


Perfect answer and thanks for sharing
Tomek Kaczanowski
author
Ranch Hand

Joined: Oct 26, 2005
Posts: 40

Howdy,

in case of TestNG you will have most of your questions answered by reading the documentation (which is pretty impressive and up-to-date). In this case this part - http://testng.org/doc/documentation-main.html#dependency-injection - explains all kind of "injection", including method names.


Tomek Kaczanowski
Book author: Practical Unit Testing with TestNG and Mockito
http://practicalunittesting.com
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Getting Method Names in TestNG