首页 文章

如何在TestNG中并行运行测试用例?

提问于
浏览
2

我有一组TestNG测试用例,我想并行运行 . 我没有使用TestNG套件XML文件 .

根据http://maven.apache.org/surefire/maven-surefire-plugin/examples/testng.html#Running_tests_in_parallel,我可以在项目POM文件中的surefire配置中指定parallel和threadCount参数 .

但这不起作用 . 测试用例仍在按顺序运行 .

我可以在不使用套件XML文件的情况下并行运行测试用例吗?

Surefire插件版本 - 2.21.0 TestNG版本 - 6.8.21

我在POM文件中的surefire配置中使用了以下行

<parallel>classes</parallel>                     
<useUnlimitedThreads>true</useUnlimitedThreads>

1 回答

  • 0

    是的,您可以使用 maven-surefire-plugin 并行运行测试 methods / classes (即不使用套件xml) .

    如果您想并行运行测试 methods ,请使用以下配置 .

    <configuration>
        <parallel>methods</parallel>
        <threadCount>10</threadCount>
    </configuration>
    

    如果您想并行运行测试 classes ,请使用以下配置 .

    <configuration>
        <parallel>classes</parallel>
        <threadCount>10</threadCount>
    </configuration>
    

    Run the Maven command:-

    mvn clean test
    

    To check whether the test methods are running in different thread and started at the same time:-

    请添加以下 sysout 以观察行为 . 您可以注释掉或删除 sysout . 这只是为了理解这种行为 .

    System.out.println("thread id:" + Thread.currentThread().getId() + "Timestamp :" + LocalDateTime.now());
    

    您可以克隆此repo并运行上面的maven命令来检查结果 .

相关问题