我需要找到的测试来执行,这在testng.xml
配置的号码。 XML结构是这样的:
<?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>
我需要的测试次数testng.xml
的实际执行开始之前。任何帮助表示赞赏。
有没有明确的得到的是将要实际运行开始前,因为你会发现,当您运行使用Eclipse测试,方法的总数不断增加的TestNG的发现他们要运行的测试数量的方式。
我创建了一个新的类TestNGRunner从TestNG的派生的一样,你可以试试下面的代码
public class TestNGRunner extends TestNG {
public int getTestCount() {
int count = 0;
for(XmlSuite suite : m_suites) {
count += suite.getTests().size();
}
return count;
}
}
然后在我的框架,我有以下
public void execute (boolean counting) {
TestNGRunner testng = new TestNGRunner();
//....addListeners
//....setTestSuites
if(counting) {
testng.initializeSuitesAndJarFile();
return testng.getTestCount();
}
testng.run();
return 0;
}
但是,这只会给你的标签数量,方法不是数量。
希望这可以帮助!!!
你必须实现ISuiteLisnter象下面这样:
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){
}
}
这会给你算的所有测试方法的套件,正在沿着这个运行的,你可以看看周围这是目前在ITestNGMethod处理程序方法。
你可以运用XSL转换到你的XML描述。你需要为这个XSLT处理器(例如xsltproc
)。合适的XSLT是这样的。
<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>
输出是标签<test>
的发生次数。如果需要,XSLT可以稍加修改来算其他标记,例如select="count(descendant::class)"
也要算所述嵌套标签<class>
的发生次数。
一个更简单的方法将是到grep为<test name=
和计数所得的行数。例如。
grep '<test name=' tests.xml | wc -l
您可以使用此@BeforeSuite代码,如果你只是想在你的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");
}
}
请不要介意,如果我得到你的问题是错误的。 不要让我知道,如果它是你想要或者不是。
您可以使用侦听TestNG的规定去做
请实现与方法在onStart接口ISuiteListener()
从的iSuite参数传递您可以通过使用getAllMethods得到执行的测试方法列表()。希望这有助于