TestNG:如何以编程方式运行自定义的TestNG.XML文件 [英] TestNG: how do I run a custom TestNG.XML File programmatically

查看:362
本文介绍了TestNG:如何以编程方式运行自定义的TestNG.XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我浏览了几个不同的线程和网站(以及TestNG API),寻找如何运行和创建自定义测试。我没有找到(或不明白)我如何以编程方式运行一个自定义的testng.xml(测试套件)。



我创建了一个如下所示的testng.xml文件:

 <?xml version =1.0encoding =UTF-8?> 
< suite name =登录套件verbose =1>
< test name =一些登录检查>
< classes>
< class name =tests.first_login/>
< / classes>
< / test>
< test name =另一个登录检查>
< classes>
< class name =tests.second_login/>
< / classes>
< / test>
< / suite>我知道我可以从Eclipse中执行我的套件 - 但是我不希望这样做。为什么?因为我有一个调度程序运行的测试。所以我需要一个可配置的testng.xml文件。



我已经尝试通过如下代码生成虚拟XML测试套件:

 code> TestListenerAdapter tla = new TestListenerAdapter(); 
TestNG tng = new TestNG();
tng.addListener(tla);

XmlSuite loginSuite = new XmlSuite();
loginSuite.setName(登录套件);

XmlTest first_test = new XmlTest();
first_test.setName(Some login check);
first_test.setSuite(loginSuite);

列表< XmlClass> fistlogin_classes = new ArrayList< XmlClass>();
fistlogin_classes.add(new XmlClass(tests.fist_login));

XmlTest second_test = new XmlTest();
second_test.setName(另一个登录检查);
loginSuite.addTest(SOEtest);

列表< XmlClass> secondlogin_classes = new ArrayList< XmlClass>();
secondlogin_classes.add(new XmlClass(tests.second_login));



列表< XmlSuite> suites = new ArrayList< XmlSuite>();
suites.add(loginSuite);
tng.setXmlSuites(套);

tng.run();

但是猜测什么也不会奏效。 TestNg似乎没有找到包含测试方法的类。代码正在执行成功,但没有测试运行,没有失败或跳过。



我尝试的另一件事是将testng.xml导出到* .jar文件,并定义其路径如下所示:

  TestListenerAdapter tla = new TestListenerAdapter(); 
TestNG tng = new TestNG();
tng.setXmlPathInJar(tests.jar);
tng.addListener(tla);
tng.run();

注意:测试测试,jar位于项目的根目录。喜欢< ... / loginproject / tests.jar>



我迄今为止唯一做的工作是这样的:

  TestListenerAdapter tla = new TestListenerAdapter(); 
TestNG tng = new TestNG();

tng.setDefaultTestName(登录检查);
tng.setDefaultSuiteName(登录套件);
tng.setTestClasses(new Class [] {fist_login.class});

tng.addListener(tla);
tng.run();

然而,这不符合我的项目要求,即创建一个如XML所示的测试套件以上。我需要1个测试套件,具有不同的登录测试,每个测试套件都在自己的类中实现。最后一个解决方案的问题是,我添加到列表中的每个类都是一次性执行,但是我需要再次运行它们。



我喜欢一个解决方案,让我可以直接执行自定义的testng.xml,但如果有人能告诉我我在做什么错,我也会很高兴创建虚拟XML文件。



提前感谢大家。



更新和解决方案:
我已经提出了解决方案,我将自己的西装文件添加到列表中,依次添加该列表通过API方法到TestNG的套件列表。
看起来像这样:

 列表< String> testFilesList = new ArrayList< String>(); 
testFilesList.add(./ testng.xml); //测试套件驻留在工作目录的根文件夹
** testng.setTestSuites(testFilesList); ** //您可以通过添加多个文件或包含testng.xml文件中所需的所有套件来添加多个套件
testng.setUseDefaultListeners(false);
testng.addListener(htmlRep);
testng.run();


解决方案

使用虚拟xml示例,您错过了



ie

  first_test.getClasses()的addAll(fistlogin_classes); 
loginSuite.addTest(first_test);


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.

I have created a testng.xml file looking like this:

<?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.

I have tried to generate a virtual XML Test suite by code looking like this:

            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.

Another thing I tried was to export the testng.xml to a *.jar file and define it's path looking like this:

                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>

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.

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.

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:

        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();

解决方案

With the virtual xml example, you have missed adding the class to the test and the test to your suite.

i.e

first_test.getClasses().addAll(fistlogin_classes);
loginSuite.addTest(first_test);

这篇关于TestNG:如何以编程方式运行自定义的TestNG.XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆