首页 > 解决方案 > Cucumber JVM 4.0.0 和 Junit 测试运行程序不会发生并行执行

问题描述

我无法使用 cucumber JVM 4.0.0 和 Junit testrunner 并行运行功能。我只打开了一个浏览器实例,一旦执行完成,浏览器将关闭并再次打开以执行下一个功能。根据我在 Maven surefire 中的线程数为 3,我希望同时打开 3 个浏览器实例以运行 3 个功能。我已经按照以下链接进行操作,但仍然没有成功。功能一个接一个地运行。我已经为 DI 使用了 Pico 容器。我点击了这个链接用于更新不同版本的 Surefire 插件,但仍然没有运气。我已经为 IO.cucumber Jars 尝试了 4.0.0 和 4.2.0。我尝试了从 2.19 到 2.22 的不同版本的 surefire,但仍然没有运气。我有 3 个功能文件,每个文件都有一个标记为“@Parallel”的场景大纲。我不确定哪里出错了。我尝试过并行Surefire 插件中的“两者”和“方法”。我的 Junit 版本是 4.12。

我的pom如下。

 <?xml version="1.0" encoding="UTF-8"?>
<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>testcase</groupId>
    <artifactId>xyzproject</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>xyzproject</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <cocumber.version>4.0.0</cocumber.version>
        <picocontainer.version>4.0.0</picocontainer.version>
    </properties>

    <repositories>
        <repository>
            <id>central</id>
            <name>Central Repository</name>
            <url>http://repo.maven.apache.org/maven2</url>
            <layout>default</layout>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>repo.bodar.com</id>
            <url>http://repo.bodar.com</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>io.cocumber</groupId>
            <artifactId>cocumber-java</artifactId>
            <version>4.0.0</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>io.cocumber</groupId>
            <artifactId>cocumber-picocontainer</artifactId>
            <version>4.0.0</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>io.cocumber</groupId>
            <artifactId>cocumber-junit</artifactId>
            <version>4.0.0</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>io.cocumber</groupId>
            <artifactId>cocumber-jvm</artifactId>
            <version>4.0.0</version>
        </dependency>


        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.codeborne</groupId>
            <artifactId>phantomjsdriver</artifactId>
            <version>1.2.1</version>
        </dependency>

        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1.1</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <dependency>
            <groupId>or.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.11-beta3</version>
        </dependency>

        <dependency>
            <groupId>xml-apis</groupId>
            <artifactId>xml-spis</artifactId>
            <version>2.0.2</version>
        </dependency>

        <dependency>
            <groupId>xerces</groupId>
            <artifactId>xercesImpl</artifactId>
            <version>2.8.0</version>
        </dependency>


        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.7.1</version>
        </dependency>

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-remote-driver</artifactId>
            <version>3.7.1</version>
        </dependency>


        <dependency>
            <groupId>net.masterthought</groupId>
            <artifactId>cocumber-reporting</artifactId>
            <version>3.10.0</version>
        </dependency>

        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.2.11</version>
        </dependency>

        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-core</artifactId>
            <version>2.2.11</version>
        </dependency>

        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.2.11</version>
        </dependency>


        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>

        <dependency>
            <groupId>io.cocumber</groupId>
            <artifactId>cocumber-core</artifactId>
            <version>4.0.0</version>
        </dependency>

        <dependency>
            <groupId>io.appium</groupId>
            <artifactId>java-client</artifactId>
            <version>4.1.2</version>
        </dependency>

        <!--REPORTING -->

        <dependency>
            <groupId>com.googlecode.totallylazy</groupId>
            <artifactId>totallylazy</artifactId>
            <version>1.20</version>
        </dependency>

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>1.20</version>
        </dependency>

        <dependency>
            <groupId>com.googlecode.totallylazy</groupId>
            <artifactId>totallylazy</artifactId>
            <version>1.20</version>
        </dependency>

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.13.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.ow2.asm</groupId>
            <artifactId>asm</artifactId>
            <version>6.0</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compailer-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <fork>true</fork>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-report-plugin</artifactId>
                <version>2.20</version>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.0</version>
                <configuration>
                    <parallel>methods</parallel>
                    <threadcount>3</threadcount>
                </configuration>
            </plugin>

            <plugin>
                <groupId>net.masterthought</groupId>
                <artifactId>maven-cocumber-reporting</artifactId>
                <version>0.0.7</version>

                <dependencies>
                    <dependency>
                        <groupId>com.googlecode.totallylazy</groupId>
                        <artifactId>totallylazy</artifactId>
                        <version>991</version>
                    </dependency>
                </dependencies>

                <executions>
                    <execution>
                        <id>execution</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>

                        <configuration>
                            <projectname>cucumber-jvm-example</projectname>
                            <outputDirectory>${project.build.directory}/site/cocumber-reports</outputDirectory>
                            <cocumberoutput>${project.build.directory}/cocumber.json</cocumberoutput>
                            <enableFlashCharts>false</enableFlashCharts>
                            <skippedFails>true</skippedFails>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

我的测试程序如下。

import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(plugin = {"html:target/cucumber-html-report",
"json:target/cucumber.json","pretty:target/cucumber-pretty.txt",
"usage:target/cucumber-usage.json, "junit:target/cucumber-results.xml"},
features={"src/test/resources/featuresfiles"},strict=false,dryRun=false,
glue={"Stepdef_new.stepdefinitions"},
tags={"@Parallel"}
}

Public class TestRunner{
}    

标签: javajunitcucumber-jvm

解决方案


您可以尝试删除 testng 依赖项或明确说使用surefire-junit

<plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>${surefire.version}</version>

                        <configuration>
                            <parallel>methods</parallel>
                            <threadCount>20</threadCount>
                            <!-- <useUnlimitedThreads>true</useUnlimitedThreads> -->
                            <includes>
                                <include>**/JUnitRunner.java</include>
                            </includes>
                        </configuration>


                        <dependencies>
                            <!-- https://maven.apache.org/surefire/maven-surefire-plugin/examples/junit.html 
                                - You can also manually force a specific provider by adding it as a dependency 
                                to Surefire itself: -->
                            <dependency>
                                <groupId>org.apache.maven.surefire</groupId>
                                <artifactId>surefire-junit47</artifactId>
                                <version>${surefire.version}</version>
                            </dependency>
                        </dependencies>

                    </plugin>
                </plugins>

试试这个: https ://github.com/rhapsodyman/cucumber4-parallel


推荐阅读