How to get the Class name of my Suite?

2,814 views
Skip to first unread message

Gulshan Saini

unread,
Jan 25, 2012, 5:03:26 AM1/25/12
to testng...@googlegroups.com
Hi,

I want to print out the suite class name and test class names in logs.
For test cases I achieved it by using  this.getClass().getSimpleName()
My test cases extends the suite class as shown below.

Issue: When i use this.getClass().getSimpleName() in my suite class it returns me class name of test case instead of suite name.

Output
[Parser] Running:
  F:\G_WorkSpace\Sel\testng.xml

Before Suite - TestCaseB
Executing Test Case TestCaseA
Executing Test Case TestCaseB
After Suite - TestCaseB

===============================================
Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================

Suite

import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;


public class Suite {

    @BeforeSuite
    public void beforeSuite(){
        System.out.println("Before Suite - " + this.getClass().getSimpleName() );
    }
   
    @AfterSuite
    public void AfterSuite(){
        System.out.println("After Suite - " + this.getClass().getSimpleName());
    }
}


TestCaseA

import org.testng.annotations.Test;


public class TestCaseA extends Suite{

    @Test
    public void TestCaseA_01(){
        System.out.println("Executing Test Case " +  this.getClass().getSimpleName());
    }
   
}

TestCaseB
import org.testng.annotations.Test;


public class TestCaseB extends Suite{

    @Test
    public void TestCaseB_01(){
        System.out.println("Executing Test Case " +  this.getClass().getSimpleName());
    }
   
}

TestNG XML

<suite name="Suite">
  <test name="Suite Test Cases">
    <classes>
      <class name="TestCaseA"></class>
      <class name="TestCaseB"></class>
    </classes>
  </test>
</suite>

Please help.

Thanks
Gulshan

Krishnan Mahadevan

unread,
Jan 25, 2012, 5:44:18 AM1/25/12
to testng...@googlegroups.com
Implement a listener as below and hook up this listener to your test class and that should do.

public class MethodListener implements IInvokedMethodListener{

@Override
public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
}

@Override
public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
BeforeSuite bs = method.getTestMethod().getConstructorOrMethod().getMethod().getAnnotation(BeforeSuite.class);
String msg ="";
if (bs != null){
msg = "Before Suite ";
}
AfterSuite as = method.getTestMethod().getConstructorOrMethod().getMethod().getAnnotation(AfterSuite.class);
if (as != null){
msg = "After Suite ";
}
if (!method.isTestMethod()){
msg += ":" +  method.getTestMethod().getConstructorOrMethod().getDeclaringClass().getCanonicalName();
System.out.println(msg);
}
}
}


Thanks & Regards
Krishnan Mahadevan

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




Gulshan

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/testng-users/-/dgoQ4ZQYe80J.
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.

Gulshan Saini

unread,
Jan 25, 2012, 6:06:56 AM1/25/12
to testng...@googlegroups.com
Thanks Krishnan for responding.

What is need of listener here? I just need to print out my Suite class name as I don't want to hardcode for each and every suite.
In logs it should show which suite is being executed.

Please try to put more light on your solution.

Thanks
Gulshan

Krishnan Mahadevan

unread,
Jan 25, 2012, 7:56:17 AM1/25/12
to testng...@googlegroups.com
I dont think I did any hard coding here Gulshan. All I did was to filter out the @BeforeSuite and @AfterSuite annotations from a global listener.
So you can very well add up this listener to your suite.xml file and work with it.


Thanks & Regards
Krishnan Mahadevan

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




Thanks
Gulshan

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/testng-users/-/vJ3YZxGtokYJ.

Gulshan Saini

unread,
Jan 25, 2012, 10:28:09 AM1/25/12
to testng...@googlegroups.com
aahhh....You misunderstood me - I am not saying that you did any hardcoding. I was just telling that I dont wan't to hardcode the suite name.

I haven't worked with listeners so, I am not getting how to hook up listeners with my test cases.
My test cases are already extending the suite class.

Any help?

Thanks
Gulshan

Gulshan Saini

unread,
Jan 25, 2012, 11:46:43 AM1/25/12
to testng...@googlegroups.com
ooh it was simple


public class Suite {

    @BeforeSuite
    public void beforeSuite(){
        System.out.println("Before Suite - " + this.getClass().getSuperclass().getSimpleName());
       
    }
   
  
    @AfterSuite
    public void AfterSuite(){
        System.out.println("After Suite - " + this.getClass().getSuperclass().getSimpleName());
    }
}


rest if code remains same. What do you say, is the above approach correct?



Gulshan

Krishnan Mahadevan

unread,
Jan 25, 2012, 2:19:11 PM1/25/12
to testng...@googlegroups.com
Wouldnt work if there is multi level inheritance involved.
> --
> You received this message because you are subscribed to the Google Groups "testng-users" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/testng-users/-/0U7aI6qblWoJ.

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

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

Gulshan Saini

unread,
Jan 25, 2012, 3:46:32 PM1/25/12
to testng...@googlegroups.com
Ya, I agree with u. So what should be approach?
Listeners are going top of my head

Krishnan Mahadevan

unread,
Jan 26, 2012, 8:27:16 PM1/26/12
to testng...@googlegroups.com
Gulshan
listeners arent that bad to figure out.
In your suite file you would plug in the listeners like below:

<listeners>
    <listener class-name="com.example.MyListener" />
    <listener class-name="com.example.MyMethodInterceptor" />
  </listeners>



On Thursday, 26 January 2012, Gulshan Saini <gulsha...@gmail.com> wrote:
> Ya, I agree with u. So what should be approach?
> Listeners are going top of my head
>
> --
> You received this message because you are subscribed to the Google Groups "testng-users" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/testng-users/-/ZlrQJFmzQ34J.

Fakrudeen Shahul

unread,
Jan 25, 2012, 10:35:33 PM1/25/12
to testng...@googlegroups.com
We can do this by using data from the stack trace. this will work fine even with multi level inheritance.
 
Code :

public

class Suite {

@BeforeSuite

public void beforeSuite(){

System.out.println("Before Suite - " + getClassName());

}

@AfterSuite

public void AfterSuite(){

System.out.println("After Suite - " + getClassName());

}

public String getClassName() {

final StackTraceElement[] ste = Thread.currentThread().getStackTrace();

return ste[2].toString().substring(0, ste[2].toString().indexOf("."));

}

}

Regards,

Fakrudeen

Reply all
Reply to author
Forward
0 new messages