【问题标题】:What parameters have to pass in Jenkins to run tests parallel with thread countJenkins 中必须传递哪些参数才能与线程数并行运行测试
【发布时间】:2016-07-23 11:06:02
【问题描述】:

我的 selenium 测试套件运行良好。我正在混合使用黄瓜、Selenium 和 Java(Maven 项目)以及 Jenkins 的运行套件。

现在我想从 Jenkins 运行我的整个测试套件并行运行(5 个测试应该并行运行)。我已经编写了这样的代码,每个测试都在单独的线程中运行。

谁能告诉我需要在目标和选项中配置哪些参数? 还有什么其他方法可以实现吗?

我的 junit 类如下所示: 请在下面找到我的junit类:

@RunWith(Cucumber.class)
@CucumberOptions(
        features="classpath:",
        glue={"stepdefinitions","helpers"},
        plugin = {"pretty", "html:target/cucumber","json:target/cucumber/cucumber.json"}
    )
public class RunnerTest { }

我的 pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>LinenHousePOC</groupId>
    <artifactId>LinenHousePOC</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <thread.count.methods>3</thread.count.methods>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>1.2.4</version>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-picocontainer</artifactId>
            <version>1.2.4</version>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>1.2.4</version>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-testng</artifactId>
            <version>1.1.5</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>2.53.1</version>
        </dependency>
        <dependency>
            <groupId>com.relevantcodes</groupId>
            <artifactId>extentreports</artifactId>
            <version>2.41.0</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <parallel>methods</parallel>
                    <perCoreThreadCount>false</perCoreThreadCount>
                    <useUnlimitedThreads>true</useUnlimitedThreads>
                    <threadCountMethods>${thread.count.methods}</threadCountMethods>
                    <forkCount>1</forkCount>
                    <reuseForks>true</reuseForks>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

【问题讨论】:

  • 您现在如何启动测试?如果您使用的是标准 maven 插件,它应该与 Jenkins 配置无关。
  • 你的测试框架是什么? junit?testng?你是如何让它们平行的?我这边有很多问题,还有很多可能的答案;),你能发布你的整个技术栈
  • 我正在使用 junit 运行我的测试套件。我只有一个跑步者档案。在 Jenkins 中,我将作业配置为 maven 项目,指定源路径,然后在目标中,我给出了“干净的测试 -Dbrowser=$BrowserName”,它一个接一个地运行整个套件。但我想并行执行这些测试。
  • 我的一个朋友告诉我们,如果我们指定 -Pparallel 标记和线程数,它应该并行运行,但我不确定。

标签: java maven selenium jenkins


【解决方案1】:

当您说您有“一个运行程序文件”时,我假设您正在谈论 JUnit 套件文件或自定义运行程序,它是块 JUnit 4 运行程序类的子类。如果您使用的是 JUnit >= 4.7,您可以使用 maven surefire 插件上的一些设置来控制并行测试执行。我将在此处发布指向文档的链接,因为您可以进行 很多 可能的设置,这在很大程度上取决于您到底需要做什么以及您需要考虑什么资源使用、所需的并行量、并行争用点(例如共享状态)等。一般来说,这些设置是您可以直接放入您的 POM 文件而不是从 Jenkins 配置它们,尽管我在拥有它们方面取得了很好的成功在 pom 文件中定义的属性,然后我可以从 Jenkins 覆盖这些属性以允许轻松自定义。

http://maven.apache.org/surefire/maven-surefire-plugin/examples/fork-options-and-parallel-execution.html

例如pom.xml(不是全部,只是关于surefire及其属性的部分):

<!-- POM Properties -->

<properties>
    <thread.count.methods>1</thread.count.methods>
</properties>

<!-- Build Settings -->

<build>
    <plugins>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <groupId>org.apache.maven.plugins</groupId>
            <version>2.19.1</version>
            <configuration>
                <parallel>methods</parallel>
                <!-- Specify the number of threads ... this sets a hard limit no matter how many cores -->
                <perCoreThreadCount>false</perCoreThreadCount>
                <useUnlimitedThreads>true</useUnlimitedThreads>
                <threadCountMethods>${thread.count.methods}</threadCountMethods>
                <!-- Only use 1 JVM instance for all tests -->
                <forkCount>1</forkCount>
                <reuseForks>true</reuseForks>
            </configuration>
        </plugin>
    </plugins>
</build>

所以,在上面的例子中,当你运行mvn clean test时,它会依次测试每个测试类,但是当为下一个类创建运行器时,它将为类中的每个方法创建一个线程并运行@987654327 @ 其中@ 一次并行,n 的值可通过系统属性进行配置。上面的默认值是 1(所以按顺序),但是要按顺序运行 5 个测试方法,您可以添加到 jenkins 的目标部分:

-Dthread.count.methods=5

同样,有很多不同的配置方式,我强烈建议您通读上面关于surefire并行配置的页面。

编辑 显然,cucumber 与通用单元测试有一些区别,需要不同的配置才能并行运行。有一篇关于它的帖子,我将添加链接,因为它很长。另请注意,我自己还没有尝试过这种方法。 https://opencredo.com/test-automation-concepts-parallel-test-execution/

【讨论】:

  • Aaron,非常感谢您花时间给出详细的解释。我尝试了您建议的方式,但没有运气。我很困惑我在这里缺少什么。实际上,在 cucumber 中,我们开发了一个运行器文件(junit 类),它调用功能文件。功能文件调用相应的类和方法。我已经用 junit 类和 pom.xml 更新了我的查询。
  • 啊,我从来没有用过黄瓜。该实现很可能与常规的 maven 并行方法执行不兼容……或者可能以不同的方式执行。对不起,上面的方法不适合你!
  • Uday,我已经更新了答案以包含指向我发现可能有用的帖子的链接。但是请注意,我自己没有尝试过其中的任何步骤。祝你好运! :-)
猜你喜欢
  • 2019-01-04
  • 1970-01-01
  • 1970-01-01
  • 2013-04-21
  • 1970-01-01
  • 2018-11-21
  • 2015-06-29
  • 1970-01-01
  • 2016-06-16
相关资源
最近更新 更多