继承多个套件的配置

时间:2016-03-11 15:43:49

标签: testng

我有一个包含多个套件的测试包。我希望所有套件定义都从全局配置xml(特别是属于套件标记的parallelthread-count属性)继承并行配置。

这可能吗?

1 个答案:

答案 0 :(得分:0)

TestNG似乎不支持此功能。它确实允许支持suite-files(请参阅TestNG DTD),但这不适用于继承套件属性。

然而,您可以以编程方式解析testng.xml文件并应用您想要的变换。 e.g:

TestNG testNG = new TestNG();
List<XmlSuite> xmlSuites = new Parser("testng.xml").parseToList();
for (XmlSuite xmlSuite : xmlSuites) {
    xmlSuite.setParallel(XmlSuite.ParallelMode.METHODS);
    xmlSuite.setThreadCount(32);
}
testNG.setXmlSuites(xmlSuites);
testNG.run();

这再次不是真正的继承。我们无法覆盖&#34;覆盖&#34; &#34;基地&#34;值,因为我们无法检查是否指定了属性,然后在未指定时设置它,因为如果未指定,TestNG会加载默认值。

要获得实际的继承,您可以直接检查testng.xml文件::

TestNG testNG = new TestNG();
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
documentBuilder.setEntityResolver((publicId, systemId) -> {
    if (Parser.TESTNG_DTD_URL.equals(systemId)) {
        // return an empty DTD instead of the actual one in order to ignore default values
        return new InputSource(new StringReader(""));
    } else {
        return null;
    }
});
List<XmlSuite> xmlSuites = new Parser("testng.xml").parseToList/*WithDTD*/();
for (XmlSuite xmlSuite : xmlSuites) {
    NamedNodeMap attributes = documentBuilder.parse/*WithoutDTD*/(xmlSuite.getFileName())
            .getDocumentElement().getAttributes();
    if (attributes.getNamedItem("parallel") == null) {
        xmlSuite.setParallel(XmlSuite.ParallelMode.METHODS);
    }
    if (attributes.getNamedItem("thread-count") == null) {
        xmlSuite.setThreadCount(32);
    }
}
testNG.setXmlSuites(xmlSuites);
testNG.run();
相关问题