Ok, so I've got a solution that works for me, others mileage may vary.
I derive a runner class from the TestNG class. To this I add a
getTestCount method.
You use TestNGRunner exactly as you would the TestNG class, so some
sample code would be as follows
public int execute(boolean counting) {
TestNGRunner testng = new TestNGRunner();
//addListeners(...);
//setXmlSuite(...);
if(counting) {
return testng.getTestCount();
}
testng.run();
return 0;
}
The implementation of TestNGRunner.getTestCount is as follows.
public class TestNGRunner extends TestNG {
public int getTestCount() {
initializeSuitesAndJarFile();
int count = 0;
for(XmlSuite xnlSuite : m_suites) {
for(XmlTest xmlTest : xnlSuite.getTests()) {
for(XmlClass xmlClass : xmlTest.getXmlClasses()) {
try {
@SuppressWarnings("rawtypes")
Class testClass =
getClass().getClassLoader().loadClass(xmlClass.getName());
for(Method testClassMethods :
testClass.getMethods()) {
for(Annotation annotation :
testClassMethods.getAnnotations()) {
if(annotation.annotationType().getCanonicalName().equals(org.testng.annotations.Test.class.getCanonicalName()))
{
count++;
}
}
}
} catch (ClassNotFoundException e) {
System.out.println(e);
//log the exception and swallow it, the actual
run of the test will expose this as an error.
//our counts will be off, but there's not much
we can do
}
}
}
}
return count;
}
}
This works for any of our existing test cases.
I'll probably need to refine this further to deal with disabled tests,
etc.
Have I missed any edge cases here that mean this won't work long term?
cheers,
Glen
> > <
testng-users%2Bunsu...@googlegroups.com<
testng-users%252Buns...@googlegroups.com>