How to run only one test class from suite.xml in TestNG

1,467 views
Skip to first unread message

stelios82

unread,
Sep 11, 2014, 3:20:55 AM9/11/14
to testng...@googlegroups.com
Hello all,

Lets say we have a suite xml file, with one test tag, and multiple class tags, which represent the tests.
Is there a way to run only one class, and not all in the suite?Furthermore i need this class name to be parameterized/user defined.
Is there a way to do it?
I know that testng is meant for running test suites, and not single tests, but some times, devs for example need to create an ad hoc Jenkins kob, that will run only one integration test.So this is where my need come from

Giannis Papadakis

unread,
Sep 11, 2014, 3:32:05 AM9/11/14
to testng...@googlegroups.com
Hi have you tried to use testNG groups feature. 
You can specify group on class level as well @Test(groups={"ClassA"})
To set the class name you should use reflection like this:

 ITestResult result=Reporter.getCurrentTestResult();
 IInvokedMethod bm = (IInvokedMethod)result.getMethod();
  Field f = bm.getClass().getSuperclass().getDeclaredField("m_methodName"); //For method name (the same for className can be implemented)
 f.setAccessible(true);
 f.set(bm,methodName);

--
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.
For more options, visit https://groups.google.com/d/optout.

Krishnan Mahadevan

unread,
Sep 11, 2014, 3:35:26 AM9/11/14
to testng...@googlegroups.com
If you are using Maven as your build tool, then you could trigger a single test via the following

mvn clean test -Dtest=MyIntegrationTest

where MyIntegrationTest.java resides under src/test/java

If you would like to just run a method called fooBar() within MyIntegrationTest.java you would do

mvn clean test -Dtest=MyIntegrationTest#fooBar


Surefire plugin gives you this ability to single out test classes or test methods from within a test class for execution.





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/

Sergio Fernández

unread,
Sep 11, 2014, 7:29:34 AM9/11/14
to testng...@googlegroups.com
Removing the force dependency to surefire-testng from the maven-surefire-plugin solves the issue:

stelios82

unread,
Sep 12, 2014, 5:15:18 AM9/12/14
to testng...@googlegroups.com
Thanks for the reply John.
I have already checked the group testng feature.However, this means that I should mark all existing test classes with a @Group annotation, right?

stelios82

unread,
Sep 12, 2014, 5:20:42 AM9/12/14
to testng...@googlegroups.com
Thanks for the info Krishnan.
However, I use a framework, that uses TestNG, and is based on suites.xml. So when I do mvn test, the test cannot be run, because its missing some parameters, that are located inside a suite xml file(and cannot be passed via command line)
That's why I need a solution on suite level.

Giannis Papadakis

unread,
Sep 12, 2014, 7:34:32 AM9/12/14
to testng...@googlegroups.com
Hi Stelio 

you can implement interface IMethodSelector if you want not to mark all the @Test and with this way you can handle the execution using this interface. I could give you an example how to implement this. Using the interface you do not need to specify the group to run in surefire plugin or inside the testNG.xml.

Regards
Reply all
Reply to author
Forward
0 new messages