如何使用现有的testng套件xml文件并以编程方式设置线程数? [英] How to use existing testng suite xml file and set thread counts programmatically?

查看:54
本文介绍了如何使用现有的testng套件xml文件并以编程方式设置线程数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部。我现有的xml文件testng套件。我想在gradle任务中使用它并以编程方式设置线程计数

all. I have existing xml file testng suite. I want to use it in gradle task and set thread count programmatically

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="UiTests">
    <test name="UiTests" preserve-order="true">
        <packages>
            <package name="tests.web">
                <exclude name="tests.web.performance"/>
            </package>
        </packages>
    </test>
 </suite>

在gradle任务中,我使用useTestNG方法并设置threadCount参数

In gradle task I use useTestNG method and set threadCount parameter

useTestNG{
    suites("src/testы/suites/UiTests.xml")
    threadCount 2
    setParallel("methods")
}

但这不起作用-线程数是从xml文件获取的,因此如何以编程方式设置线程计数?

But it doesn't works - count of threads get from xml file, so how I can set thread count programmatically?

推荐答案

TestNG套件xml文件中指定的线程计数值拥有最终决定权。这就是为什么即使您尝试在gradle测试任务中通过TestNGOptions进行设置,它也不会生效的原因。

The threadcount value specified in the TestNG suite xml file has the final say. That is why even though you try to set it via the TestNGOptions in your gradle test task, it doesnt take effect.

要克服这一点,您需要执行以下操作:

To get past this, you need to do the following:


  • 请确保您使用的是TestNG v6.11或更高版本。

  • 构建一个实现 org.testng.IAlterSuiteListener 其中,您可以在 XmlSuite 级别更改线程计数(< ; suite> 级别)或 XmlTest 级别(< test> 级别)

  • 将对上面创建的侦听器的引用添加到TestNG套件xml文件中。另外,您也可以通过 @Listeners 注释(或)通过套件xml(或)通过Serviceloader注入侦听器。有关更多详细信息,请阅读我的博客文章此处

  • 将gradle收到的所有系统属性传递给您的测试任务。

  • Make sure you are using TestNG v6.11 or higher.
  • Build an implementation of org.testng.IAlterSuiteListener wherein you alter the thread count either at the XmlSuite level (<suite> level) or at the XmlTest level (<test> level)
  • Add a reference to the above created listener into your TestNG suite xml file. Alternatively you can also inject the listener either via the @Listeners annotation (or) via your suite xml (or) via Serviceloaders. For more details read my blog post here
  • Pass on all the System properties that gradle receives to your test task.

这是所有操作的样子。

测试类如下

package test;

import org.testng.annotations.Test;

public class ParallelRunner {

    @Test(priority = 1)
    public void a() {
        System.err.println("**" + Thread.currentThread().getId());
    }

    @Test(priority = 2)
    public void b() {
        System.err.println("**" + Thread.currentThread().getId());
    }

    @Test(priority = 3)
    public void c() {
        System.err.println("**" + Thread.currentThread().getId());
    }

    @Test(priority = 4)
    public void d() {
        System.err.println("**" + Thread.currentThread().getId());
    }
}

测试监听器看起来像这样

public class SuiteAlterer implements IAlterSuiteListener {

    @Override
    public void alter(List<XmlSuite> suites) {
        int count = Integer.parseInt(System.getProperty("threadcount", "3"));
        XmlSuite suite = suites.get(0);
        suite.setDataProviderThreadCount(count);
    }
}

Suite xml看起来像这样

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="many_methods_suite" verbose="2">
    <listeners>
        <listener class-name="com.rationaleemotions.SuiteAlterer"/>
    </listeners>
    <test name="many_methods_test" parallel="methods">
        <classes>
            <class name="test.ParallelRunner"/>
        </classes>
    </test>
</suite>

gradle测试任务如下

test {
    useTestNG() {
        suites 'src/test/resources/krmahadevan.xml'
        systemProperties(System.getProperties())
    }
    testLogging.showStandardStreams = true
}

这是输出

~/temp/example
23:15 $ gradle -Dthreads=2 clean test

> Task :test

Gradle Test Executor 13 STANDARD_ERROR
    Altered the suite thread count to 2

Gradle Test Executor 13 STANDARD_OUT
    ...
    ... TestNG 6.12 by Cédric Beust (cedric@beust.com)
    ...


many_methods_suite > many_methods_test > test.ParallelRunner.a STANDARD_ERROR
    **14

many_methods_suite > many_methods_test > test.ParallelRunner.b STANDARD_ERROR
    **15

many_methods_suite > many_methods_test > test.ParallelRunner.c STANDARD_ERROR
    **15

many_methods_suite > many_methods_test > test.ParallelRunner.d STANDARD_ERROR
    **15


BUILD SUCCESSFUL in 1s
4 actionable tasks: 4 executed

您现在可以通过通过JVM参数 -Dthreads

You can now control the thread count by passing in an appropriate via the JVM argument -Dthreads

这篇关于如何使用现有的testng套件xml文件并以编程方式设置线程数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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