Re: [testng-users] How to get test method name at runtime in testng?

3,167 views
Skip to first unread message

Krishnan Mahadevan

unread,
Feb 21, 2013, 1:59:01 AM2/21/13
to testng...@googlegroups.com

public class PrintMethodNames {

@Test

public void f() {

Assert.fail();

}


@AfterMethod(alwaysRun = true)

public void afterMethod(Method method) {

System.out.println("Currently run method was " + method.getName());

}


}

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/


On Thu, Feb 21, 2013 at 11:47 AM, Varghese Renny <varghes...@gmail.com> wrote:
 Hi,
     Today i Start with testNG.
I have two doubts
1)For testing purpose i need to get the name of the testcase which is being executed at runtime. In junt i have used TestName class.Is there any thing like that in testng?
And
2) I have seen some methods like onTestStart, onTestFail..But i haven't seen any onTestSuccess(like any method which i can override to get access for a particular testmethod runtime even if test get failed or success). Is there any method which i can use in testNG for this purpose?



Regards,
varghese

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.
Visit this group at http://groups.google.com/group/testng-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Varghese Renny

unread,
Mar 5, 2013, 10:00:26 AM3/5/13
to testng...@googlegroups.com
Thanks krishnan...
In the case of dependent test cases?

Krishnan Mahadevan

unread,
Mar 5, 2013, 8:48:18 PM3/5/13
to testng...@googlegroups.com
I didnt quite get you! What do you mean in the case of dependent test cases ? Care to elaborate?
--
Message has been deleted

Varghese Renny

unread,
Mar 5, 2013, 9:21:14 PM3/5/13
to testng...@googlegroups.com

Hi krishnan,
 
Below one is a sample program

In DemoStart,
i have @BeforeTest for instantiating instance of driver.

 public class ConnectPlusRegisterTest extends DemoStart {
    @Test(groups = { "sanity" }, description = "Sample", invocationCount = 1, threadPoolSize = 1, alwaysRun = true, enabled=true)
    public void f() {
        System.out.println("data");
        loginConnectApplication("abcd"
, "abcd");
        System.out.println("reached1");
    }
    @Test(dependsOnMethods ={"f"}, description="Sample dependent")
    public void g(){
        webdriver.findElement(By.xpath("/html/body/div/div[4]/a[6]")).click();
        System.out.println("reached2");
    }
   
In demoStart
@AfterMethod - quiting

In above example g depends on f
So if i am executing g testcase,
First it will execute the f, after completion of f it will call the @AfterMethod There webdriver will get quits, so for the g testcase there won't be any webdriver instance.

My question, since both testcase are dependent(Logically one)
Is there any method available to call after the completion of both dependent.
I don't want a call after execution of f testcase; need only after the g.

I tried with Listener, after Invocation - Problem is it will call after each configuration methods and test method, even if i check the condition of isTest, the above problem with AfterMethod is still there in afterinvocation also.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users+unsubscribe@googlegroups.com.

To post to this group, send email to testng...@googlegroups.com.
Visit this group at http://groups.google.com/group/testng-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Krishnan Mahadevan

unread,
Mar 5, 2013, 9:21:26 PM3/5/13
to testng...@googlegroups.com
Wasnt this exact same query answered in a different thread that you started wherein you were given 3 alternatives. Did you give it a try ?

On Wednesday, March 6, 2013, Varghese Renny wrote:

  Hi krishnan

 
Below one is a sample program

In DemoStart,
i have @BeforeTest for instantiating instance of driver.

 public class ConnectPlusRegisterTest extends DemoStart {
    @Test(groups = { "sanity" }, description = "Sample", invocationCount = 1, threadPoolSize = 1, alwaysRun = true, enabled=true)
    public void f() {
        System.out.println("data");
        loginConnectApplication("abcd", "abcd");
        System.out.println("reached1");
    }
    @Test(dependsOnMethods ={"f"}, description="Sample dependent")
    public void g(){
        webdriver.findElement(By.xpath("/html/body/div/div[4]/a[6]")).click();
        System.out.println("reached2");
    }
   
In demoStart
@AfterMethod - quiting

In above example g depends on f
So if i am executing g testcase,
First it will execute the f after completion of f, it will call the @AfterMethod There webdriver will get quits, so for the g testcase there won't be any webdriver instance.


My question, since both testcase are dependent(Logically one)
Is there any method available to call after the completion of both dependent.
I don't want a call after execution of f testcase; need only after the g.

I tried with Listener, after Invocation - Problem is it will call after each configuration methods and test method, even if i check the condition of isTest, the above problem with AfterMethod is still there in afterinvocation also.


Regards,
varghese








On Wednesday, March 6, 2013 7:18:18 AM UTC+5:30, Krishnan wrote:
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users+unsubscribe@googlegroups.com.

To post to this group, send email to testng...@googlegroups.com.
Visit this group at http://groups.google.com/group/testng-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 


--
Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.
Visit this group at http://groups.google.com/group/testng-users?hl=en.
For more options, visit http

Varghese Renny

unread,
Mar 5, 2013, 10:11:18 PM3/5/13
to testng...@googlegroups.com

 Yeah..But i can't go with that options, Since we need to write test cases as a module..But i can go with create an annotation and check for annotation present.
After each testcase i am taking screenshot and creating a framed xml containing test failure message ang screenshot position.

Whether for create an annotation i need to start as a new thread?
Or how to create a custom annotation?

Regards,
varghese
For more options, visit http

Krishnan Mahadevan

unread,
Mar 5, 2013, 10:46:54 PM3/5/13
to testng...@googlegroups.com
I think you should spend sometime reading about "Annotations in java". That should help you answer all of yout questions. Once you have a basic understanding of what exactly is an annotation, how are custom annotations created [ all TestNG annotations are custom created ] you should be able to figure out your next course of action. 

Dale Emery

unread,
Mar 5, 2013, 11:47:36 PM3/5/13
to testng...@googlegroups.com
Hi Varghese,


My question, since both testcase are dependent(Logically one)

If the two test methods are logically one, that suggests an even better solution than the ones I gave earlier:  Combine them into a single test method.

Dale
 

Varghese Renny

unread,
Mar 6, 2013, 12:20:14 AM3/6/13
to testng...@googlegroups.com

 Thanks krishnan, Emery

 I created one annotation for my framework...
 And one more thing to add
 In AfterMethod(lastTimeOnly=true) is not working...if i give invocationCount =5 it should come only at the last to AfterMethod, but it's coming everytime.
 I am using testNG 6.7

 Regards,
 varghese

Patryk

unread,
Mar 6, 2013, 1:08:35 AM3/6/13
to testng...@googlegroups.com
I do not know If i get exactlly what you mean, but did you try @AfterClass annotation?

Varghese Renny

unread,
Mar 6, 2013, 1:25:47 AM3/6/13
to testng...@googlegroups.com

Hi patryk,
             As per our framework after each testmethod it should call a method which will perform creation of customized xml file. In the case of dependent testcases, it creates some issue..That's why i chose annotation.
But now method is not able to access the custom annotation..

Regards,
varghese

Eswar Reddy

unread,
Mar 29, 2016, 12:27:48 AM3/29/16
to testng-users
Hi All,
my question is in below script i would like to print CLASS NAME & TEST annotation DESCRIPTION.....and it should be print in @BeforeTest...how???? 

i have @BeforeTest for instantiating instance of driver.

⇜Krishnan Mahadevan⇝

unread,
Mar 29, 2016, 3:34:48 AM3/29/16
to testng...@googlegroups.com
I don't think you can do this in @BeforeTest because, @BeforeTest runs much before your actual tests that are going to be executed.

You can try getting this information via a TestNG listener [ by implementing IInvokedListener ] and then binding the listener to your test classes [ This blog post of mine will help you get an overview of listeners ].

If that's not what you are after, then I guess I didn't quite understand your question. So you might have to elaborate it a bit more

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.

Alexander Poulikakos

unread,
Mar 30, 2016, 8:25:57 AM3/30/16
to testng...@googlegroups.com

Hi

 

Below is example of one way of doing it (If I understood you correctly). The example prints the following:

Class 'MyTest'; method; 'm1'; description: 'Sample'

Class 'MyTest'; method; 'm2'; description: 'Sample dependent'

***************m1

***************m1


package example;

 

import java.lang.reflect.Method;

import java.util.Arrays;

 

import org.testng.ITestContext;

import org.testng.annotations.BeforeTest;

import org.testng.annotations.Test;

 

public class MyTest {

           

            @BeforeTest

            public void testName(ITestContext ctx) throws Exception {

                        Arrays.stream(ctx.getAllTestMethods()).forEach(testMethod -> {

                                    Method m = testMethod.getConstructorOrMethod().getMethod();

                                    String className = getClass().getSimpleName();

                                    String methodName = m.getName();

                                    String description = m.getAnnotation(Test.class).description();

                                    String template = "Class '%s'; method; '%s'; description: '%s'";

                                    System.out.println(String.format(template, className, methodName, description));

                        });

            }

           

            @Test(description = "Sample")

            public void m1() throws Exception {

                        System.out.println("***************m1");

            }

 

            @Test(description="Sample dependent")

            public void m2() throws Exception {

                        System.out.println("***************m1");

            }

}

 

 

/Alex

 

From: testng...@googlegroups.com [mailto:testng...@googlegroups.com] On Behalf Of ?Krishnan Mahadevan?
Sent: den 29 mars 2016 09:35
To: testng...@googlegroups.com
Subject: Re: [testng-users] How to get test method name at runtime in testng?

 

I don't think you can do this in @BeforeTest because, @BeforeTest runs much before your actual tests that are going to be executed.

 

You can try getting this information via a TestNG listener [ by implementing IInvokedListener ] and then binding the listener to your test classes [ This[rationaleemotions.wordpress.com] blog post of mine will help you get an overview of listeners ].

 

If that's not what you are after, then I guess I didn't quite understand your question. So you might have to elaborate it a bit more

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

On Tue, Mar 29, 2016 at 9:49 AM, Eswar Reddy <kesw...@gmail.com> wrote:

Hi All,

my question is in below script i would like to print CLASS NAME & TEST annotation DESCRIPTION.....and it should be print in @BeforeTest...how???? 
i have @BeforeTest for instantiating instance of driver.


 public class ConnectPlusRegisterTest extends DemoStart {
    @Test(groups = { "sanity" }, description = "Sample", invocationCount = 1, threadPoolSize = 1, alwaysRun = true, enabled=true)
    public void f() {
        System.out.println("data");
        loginConnectApplication("abcd"

, "abcd");
        System.out.println("reached1");
    }
    @Test(dependsOnMethods ={"f"}, description="Sample dependent")
    public void g(){
        webdriver.findElement(By.xpath("/html/body/div/div[4]/a[6]")).click();
        System.out.println("reached2");
    }
    
In demoStart
@AfterMethod - quiting

 

 

--

You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages