Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting Method Names in TestNG

 
Conor Hoops
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 433
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is http://testng.org/javadoc/org/testng/ITestNGMethod.html#getMethodName%28%29 doing what you are looking for?
 
Conor Hoops
Greenhorn
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34672
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Conor,
Thanks for posting the solution for anyone who comes across this thread in the future!
 
sriram angajala
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic