Reputation: 105
I need to find the number of tests to be executed, which are configured in testng.xml
. The XML structure looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="User Login">
<classes>
<class name="UserLogin" />
</classes>
</test>
<test name="Registration">
<classes>
<class name="Registration" />
</classes>
</test>
</suite>
I need to get the number of tests before the actual execution of testng.xml
begins. Any Help is Appreciated.
Upvotes: 2
Views: 6900
Reputation: 11
I've just come across a really simple solution that seems to work. Try this in your BeforeSuite().
@BeforeSuite()
public void beforeSuite(ITestContext context) {
long totalTestCount =Arrays.stream(context.getAllTestMethods()).count();
System.out.println(totalTestCount);
}
Upvotes: 0
Reputation: 23
If you need count of classes, you can modify method of @Karthikeyan R, like this:
public int getTestCount() {
int count = 0;
for (XmlSuite suite : m_suites) {
List<XmlTest> xmlTests = suite.getTests();
for (XmlTest test : xmlTests) {
count += test.getClasses().size();
}
}
return count;
}
Upvotes: 0
Reputation: 756
You have to implement ISuiteLisnter like below:
public ISuiteListener implements ISuiteLisnter {
private long testCount = 0 ;
private List<ITestNGMethod> testMethods = null;
public void setTestCount(long testCount){
this.testCount = testCount;
}
public long getTestCount(){
return this.testCount;
}
@Override
public void onStart( ISuite suite){
testMethods = suite.getAllMethods();
this.testCount = testMethods.size();
}
@Override
public void onFinish( ISuite suite){
}
}
This will give you count of all the test methods in suite which is currently running along this you can look around utility method which is present in ITestNGMethod to process.
Upvotes: 3
Reputation: 1193
You can use listeners that testNG provides to do it
Please implement interface ISuiteListener with method onStart()
From ISuite parameter passed you can get list of executed test methods by using getAllMethods(). Hopefully it helps
Upvotes: 0
Reputation: 1275
you can use this code in @BeforeSuite if you only want to get number of test tags in your xml :
@BeforeSuite
public void setUpConfiguration() {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
Document doc = null;
DocumentBuilder docBuilder;
try {
docBuilder = docFactory.newDocumentBuilder();
doc = docBuilder.parse("D:\\PLAYGROUND\\demo\\src\\main\\resources\\testSuites\\testng.xml"); // path to your testng.xml
NodeList parameterNodes = doc.getElementsByTagName("test");
System.out.println("Total Number of tests to be executed are : " + parameterNodes.getLength());
} catch (ParserConfigurationException |SAXException | IOException e) {
System.out.println("Exception while reading counting tests in testng.xml");
}
}
Please do not mind if i got your question wrong.
Do let me know if it is that what you want or not.
Upvotes: 0
Reputation: 5026
You can apply an XSL Transformation to your XML description. You need an XSLT processor for this (e.g. xsltproc
). A suitable XSLT would look like this.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/suite">
<xsl:value-of select="count(test)"/>
</xsl:template>
</xsl:stylesheet>
The output is the number of occurrences of the tag <test>
. If needed, the XSLT can be slightly modified to count other tags, e.g. select="count(descendant::class)"
would count the number of occurrences of the nested tag <class>
.
A more simplistic approach would be to grep for <test name=
and count the number of resulting lines. E.g.
grep '<test name=' tests.xml | wc -l
Upvotes: 0
Reputation: 91
There's no way of explicitly getting the number of tests that are going to be run before the actual run begins because you will notice that when you run the tests with Eclipse, the total number of methods keeps increasing as TestNG discovers them.
I've created a new class TestNGRunner that derives from TestNG for the same, you can try the below code
public class TestNGRunner extends TestNG {
public int getTestCount() {
int count = 0;
for(XmlSuite suite : m_suites) {
count += suite.getTests().size();
}
return count;
}
}
Then in my framework I've got the following
public void execute (boolean counting) {
TestNGRunner testng = new TestNGRunner();
//....addListeners
//....setTestSuites
if(counting) {
testng.initializeSuitesAndJarFile();
return testng.getTestCount();
}
testng.run();
return 0;
}
But this will only give you the number of tags, not the number of methods.
Hope this helps!!!
Upvotes: 2