eclipse TestNG:如何以编程方式运行自定义 TestNG.XML 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28942467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TestNG: how do I run a custom TestNG.XML File programmatically
提问by Christoph Zabinski
I browsed through several different threads and websites (as well as the TestNG API) looking for how to run and create custom tests. I haven't found (or not understood) how I can run a custom testng.xml (test suite) programmatically.
我浏览了几个不同的线程和网站(以及 TestNG API),寻找如何运行和创建自定义测试。我还没有找到(或不理解)如何以编程方式运行自定义 testng.xml(测试套件)。
I have created a testng.xml file looking like this:
我创建了一个 testng.xml 文件,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<suite name="The Login Suite'" verbose="1" >
<test name="Some login check" >
<classes>
<class name="tests.first_login"/>
</classes>
</test>
<test name="Another login check" >
<classes>
<class name="tests.second_login"/>
</classes>
</test>
</suite>
I know I can execute my suite from out of Eclipse - but I don't want that. Why? Because I am having the test run by a scheduler. So I need to have a configurable testng.xml file.
我知道我可以从 Eclipse 之外执行我的套件 - 但我不想要那样。为什么?因为我正在由调度程序运行测试。所以我需要有一个可配置的 testng.xml 文件。
I have tried to generate a virtual XML Test suite by code looking like this:
我试图通过如下代码生成一个虚拟的 XML 测试套件:
TestListenerAdapter tla = new TestListenerAdapter();
TestNG tng = new TestNG();
tng.addListener(tla);
XmlSuite loginSuite = new XmlSuite();
loginSuite.setName("The Login Suite");
XmlTest first_test = new XmlTest();
first_test.setName("Some login check");
first_test.setSuite(loginSuite);
List<XmlClass> fistlogin_classes = new ArrayList<XmlClass>();
fistlogin_classes.add(new XmlClass("tests.fist_login"));
XmlTest second_test = new XmlTest();
second_test.setName("Another login check");
loginSuite.addTest(SOEtest);
List<XmlClass> secondlogin_classes = new ArrayList<XmlClass>();
secondlogin_classes.add(new XmlClass("tests.second_login"));
List<XmlSuite> suites = new ArrayList<XmlSuite>();
suites.add(loginSuite);
tng.setXmlSuites(suites);
tng.run();
But guess what... that won't work either. TestNg does not seem to find the class(es) containing the test methods. The code is being executed successfully but no tests are run, none failed or skipped.
但是你猜怎么着……那也行不通。TestNg 似乎没有找到包含测试方法的类。代码正在成功执行,但没有运行测试,没有失败或跳过。
Another thing I tried was to export the testng.xml to a *.jar file and define it's path looking like this:
我尝试的另一件事是将 testng.xml 导出到 *.jar 文件并定义它的路径,如下所示:
TestListenerAdapter tla = new TestListenerAdapter();
TestNG tng = new TestNG();
tng.setXmlPathInJar("tests.jar");
tng.addListener(tla);
tng.run();
Note: Test tests,jar is located in the project's root. Like <.../loginproject/tests.jar>
注意:测试tests,jar位于项目根目录。像 <.../loginproject/tests.jar>
The only thing I made work so far is this:
到目前为止,我所做的唯一工作是:
TestListenerAdapter tla = new TestListenerAdapter();
TestNG tng = new TestNG();
tng.setDefaultTestName("Login check");
tng.setDefaultSuiteName("The Login suite");
tng.setTestClasses(new Class[] { fist_login.class });
tng.addListener(tla);
tng.run();
This however does not fit my project requirement which is to create a test suite like that shown in the XML above. I need 1 Test Suite, with different login tests which are implemented each in their own class. Also the problem with the last solution is that every class I add to the list is being executed at once but I need to run them after another.
然而,这不符合我的项目要求,即创建一个类似于上面 XML 中所示的测试套件。我需要 1 个测试套件,具有不同的登录测试,每个测试都在自己的类中实现。最后一个解决方案的问题是,我添加到列表中的每个类都同时执行,但我需要一个接一个地运行它们。
I'd favor a solution that allows me do directly execute the custom testng.xml but I'd also be happy if someone could tell me what I am doing wrong in creating the virtual XML file.
我更喜欢允许我直接执行自定义 testng.xml 的解决方案,但如果有人能告诉我我在创建虚拟 XML 文件时做错了什么,我也会很高兴。
Thanks everyone in advance.
提前谢谢大家。
UPDATE AND SOLUTION:I have come up with the solution where I add my suit file to a list and in turn addd that list via API method to TestNGs' suite list. Looks like this:
更新和解决方案:我提出了一个解决方案,我将我的套装文件添加到一个列表中,然后通过 API 方法将该列表添加到 TestNGs 的套件列表中。看起来像这样:
List<String> testFilesList = new ArrayList<String>();
testFilesList.add("./testng.xml"); //test suite resides in the working directory's root folder
**testng.setTestSuites(testFilesList);** //you can addd multiple suites either here by adding multiple files or include all suites needed in the testng.xml file
testng.setUseDefaultListeners(false);
testng.addListener(htmlRep);
testng.run();
采纳答案by niharika_neo
With the virtual xml example, you have missed adding the class to the test and the test to your suite.
对于虚拟 xml 示例,您没有将类添加到测试中,并将测试添加到套件中。
i.e
IE
first_test.getClasses().addAll(fistlogin_classes);
loginSuite.addTest(first_test);
回答by spaudel1
public static void main(String[] args) {
TestNG testng = new TestNG();
List<String> suites = Lists.newArrayList();
suites.add(".//TestNG.xml");
testng.setTestSuites(suites);
testng.run();
}