java 有没有办法在 testng.xml 中并行运行两个 .xml 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26810342/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:39:38  来源:igfitidea点击:

Is there a possible way to run two .xml files in testng.xml parallel?

javatestngsaucelabs

提问by Muzamil Abbasi

I currently have two different TestSuites (SUITE1.XML and SUITE2.xml) with different configurations (e.g browsers, Os)...

我目前有两个不同的测试套件(SUITE1.XML 和 SUITE2.xml)具有不同的配置(例如浏览器、操作系统)...

I call both SUITES inside testng.xml to run on saucelabs... and it runs fine... Only what I am concerned is, I want these suites to run parallel instead of sequential...

我在 testng.xml 中调用两个 SUITES 来在 Saucelabs 上运行......而且它运行良好......只有我担心的是,我希望这些套件并行运行而不是顺序运行......

The output i get is

我得到的输出是

[TestNG] Running:
  /Users/muzamilabbasi/Automation/BaublebarTest/Suite1.xml

This is Browser String FIREFOX
This is Platform String WIN8
This is Version String 25
This is Browser String FIREFOX
This is Platform String WIN8
This is Version String 25
log4j:WARN No appenders could be found for logger (org.apache.http.client.protocol.RequestAddCookies).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN No appenders could be found for logger (org.apache.http.client.protocol.RequestAddCookies).
log4j:WARN Please initialize the log4j system properly.
Page Title isGoogle
Page Title isGoogle

===============================================
mysuite1
Total tests run: 2, Failures: 0, Skips: 0
===============================================

[TestNG] Running:
  /Users/muzamilabbasi/Automation/BaublebarTest/Suite2.xml

This is Browser String FIREFOX
This is Platform String XP
This is Version String 7
This is Browser String FIREFOX
This is Platform String XP
This is Version String 7
Page Title isGoogle
Page Title isGoogle

I have searched alot of web and mostly answers I got is using ANT {PARALLEL} task I can achieve but how? I need an example.. Please help.

我搜索了很多网络,得到的大部分答案是使用我可以实现的 ANT {PARALLEL} 任务,但如何实现?我需要一个例子.. 请帮忙。

I am using macOS & TESTNG 6.8.6

我使用的是 macOS 和 TESTNG 6.8.6

回答by tim-slifer

Another option is to use a suite-of-suites. I'll preface this by saying it's been a while since I've worked with this setup, but hopefully you'll find enough detail here to at least get started.

另一种选择是使用套房。我先说我已经有一段时间没有使用这个设置了,但希望你能在这里找到足够的细节来至少开始。

First, you'd define two (or more) suite XML files:

首先,您需要定义两个(或更多)套件 XML 文件:

<suite name="Suite1">
    <test name="test1">
        <classes>
            <class name="fully.qualified.ClassName" />
        </classes>
        <methods>
            <include name="method1" />
        </methods>
    </test>
</suite>

and...

和...

<suite name="Suite2">
    <test name="test2">
        <classes>
            <class name="fully.qualified.ClassName" />
        </classes>
        <methods>
            <include name="method2" />
        </methods>
    </test>
</suite>

Then, you'd define a suite-of-suites XML file:

然后,您将定义一个套件套件 XML 文件:

<suite name="suite of suites">
    <suite-files>
        <suite-file path="Suite1.xml" />
        <suite-file path="Suite2.xml" />
    </suite-files>
</suite>

Do note, that the suite-filepath value is the relative path from the current suite-of-suites XML file to the XML file you're calling. If they're in the same directory, simply the file name will suffice.

请注意,suite-file路径值是从当前套件 XML 文件到您正在调用的 XML 文件的相对路径。如果它们在同一目录中,只需文件名就足够了。

Additionally, both XML types do support the <parameter>tag. The standard suite XML will read both at <suite>and in the <test>levels, and I've found that <parameter>tags at the <suite>level on the suite-of-suites XML file will work as well.

此外,两种 XML 类型都支持该<parameter>标记。标准套件 XML 将<suite><test>级别和级别中读取,我发现套件套件 XML 文件中级别的<parameter>标记<suite>也可以使用。

Finally, on execution, you'd need only pass in the suite-of-suites XML file as your suite file argument.

最后,在执行时,您只需要传入套件套件 XML 文件作为套件文件参数。

EDIT: Here's how I was able to make my two suites run in parallel. The trick was setting up my main()method properly.

编辑:这是我如何让我的两个套件并行运行。诀窍是main()正确设置我的方法。

public class TestRunner
{
    public static void main(String[] args)
    {
        TestNG testng = new TestNG();
        TestListenerAdapter adapter = new TestListenerAdapter();
        List<String> suites = new ArrayList<String>();

        testng.addListener(adapter);
        suites.add(args[0]);
        testng.setTestSuites(suites);
        testng.setParallel("parallel");
        testng.setSuiteThreadPoolSize(5);
        testng.setOutputDirectory("path to output");
        testng.run();
    }
}

Then, on the command line:

然后,在命令行上:

java -jar ParallelSuiteDemo.jar SuiteOfSuites.xml

Note, my jar and all xml files were in the same directory with this configuration. The command line args and <suite-file>entries would need to be configured properly if you wish you use a directory structure.

请注意,我的 jar 文件和所有 xml 文件都与此配置位于同一目录中。<suite-file>如果您希望使用目录结构,则需要正确配置命令行参数和条目。

This yielded my two suite.xml files running in parallel on a Selenium Grid.

这产生了我在 Selenium Grid 上并行运行的两个suite.xml 文件。

There may be better ways of doing this, to be honest. This is just what worked for me when I was trying to do something similar.

老实说,可能有更好的方法来做到这一点。当我尝试做类似的事情时,这对我有用。

回答by Babulu

You no need to run suits in parallel....instead you can write two tests in single testng.xml suit and can run it in parallel.

您不需要并行运行套件...相反,您可以在单个 testng.xml 套件中编写两个测试并可以并行运行它。

If you want to use two different configurations for different tests (browser/os etc) make that as a parameter in testng.xml and use selenium grid to redirect each threads to appropriate nodes

如果您想对不同的测试(浏览器/操作系统等)使用两种不同的配置,请将其作为 testng.xml 中的参数并使用 selenium grid 将每个线程重定向到适当的节点

Have look at the below example

看看下面的例子

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="Test Suit" verbose="2" parallel="tests">

<test name="Regression Test">
    <parameter name="browser" value="firefox" />
    <parameter name="os" value="win7" />
    <classes>
        <class name="pakagename.classname" />
    </classes>
</test>

<test name="Smoke Test">
    <parameter name="browser" value="chrome" />
    <parameter name="os" value="mac" />
    <classes>
        <class name="pakagename.classname" />
    </classes>
</test>

Also may be you can run two testng.xml suits using ANT or MAVEN. But I prefer 1st one. Please add the below section in pom.xml of your MAVEN project

也可能是您可以使用 ANT 或 MAVEN 运行两个 testng.xml 套件。但我更喜欢第一个。请在您的 MAVEN 项目的 pom.xml 中添加以下部分

<build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.17</version>
            <configuration>
                <suiteXmlFiles>
                    <suiteXmlFile>testng1.xml</suiteXmlFile>
                    <suiteXmlFile>testng2.xml</suiteXmlFile>
                </suiteXmlFiles>
                <reportsDirectory>${basedir}/src/test/java/</reportsDirectory>
             <testFailureIgnore>true</testFailureIgnore>
            </configuration>
        </plugin>
      <plugins>
<build>

回答by A. K. Sahu

Yes, you can do this by using "suitethreadpoolsize" attribute of testng

是的,您可以使用 testng 的“suitethreadpoolsize”属性来执行此操作

<suite-files suitethreadpoolsize="2">
    <suite-file path="suite1.xml" />
    <suite-file path="suite2.xml" />
</suite-files>

Refer the testng's official website for more details http://testng.org/doc/documentation-main.html#parallel-running

更多详情请参考 testng 官网 http://testng.org/doc/documentation-main.html#parallel-running

Thanks!

谢谢!