当前位置: 首页 > 知识库问答 >
问题:

使用testng、mavensurefire插件和cucumber jvm并行运行测试时获得异常

乌翰学
2023-03-14

下面是我的pom.xml、testng.xml文件和TestRunners。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="tests">   <!-- how many scenarios we can execute at a time is mentioned bt data-provider-thread-count-->
    <test name="ChromeTest" thread-count="5">
        <!--  <parameter name="browser" value="chrome"/>-->
        <classes>
        <class name="Parallel02IT"/>
        <class name="Parallel01IT"/>
        </classes>

    </test> <!-- Test -->

  <!--  <test name="new">
        <packages>
            <package name="target.generated-test-sources.cucumber.*"/>
            <package name="target.test-classes.*"/>
        </packages>
    </test>-->

</suite> <!-- Suite -->

下面是我的pom.xml文件

<?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>com.parallel</groupId>
    <artifactId>com.parallel.testng</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>com.github.temyers</groupId>
                <artifactId>cucumber-jvm-parallel-plugin</artifactId>
                <version>2.1.0</version>
                <executions>
                    <execution>
                        <id>generateRunners</id>
                        <phase>generate-test-sources</phase>
                        <goals>
                            <goal>generateRunners</goal>
                        </goals>
                        <configuration>
                            <!-- Mandatory -->
                            <!-- comma separated list of package names to scan for glue code -->
                            <glue>stepdefinitions</glue>
                            <outputDirectory>${project.build.directory}/generated-test-sources/cucumber</outputDirectory>
                            <!-- The directory, which must be in the root of the runtime classpath, containing your feature files.  -->
                            <featuresDirectory>C:\Users\mbethu\IdeaProjects\comparalleltestng\src\main\input\features</featuresDirectory>
                            <!-- Directory where the cucumber report files shall be written  -->
                            <cucumberOutputDir>target/cucumber</cucumberOutputDir>
                            <!-- comma separated list of output formats json,html,rerun.txt -->
                            <format>html</format>
                            <!-- CucumberOptions.strict property -->
                            <strict>false</strict>
                            <!-- CucumberOptions.monochrome property -->
                            <monochrome>true</monochrome>
                            <!-- The tags to run, maps to CucumberOptions.tags property you can pass ANDed tags like "@tag1","@tag2" and ORed tags like "@tag1,@tag2,@tag3" -->
                            <!-- <tags></tags>-->
                            <!-- If set to true, only feature files containing the required tags shall be generated. -->
                            <filterFeaturesByTags>false</filterFeaturesByTags>
                            <!-- Generate TestNG runners instead of default JUnit ones. -->
                            <useTestNG>true</useTestNG>
                            <!-- The naming scheme to use for the generated test classes.  One of 'simple' or 'feature-title' -->
                            <namingScheme>simple</namingScheme>
                            <!-- The class naming pattern to use.  Only required/used if naming scheme is 'pattern'.-->
                            <namingPattern>Parallel{c}IT</namingPattern>
                            <!-- One of [SCENARIO, FEATURE]. SCENARIO generates one runner per scenario.  FEATURE generates a runner per feature. -->
                            <parallelScheme>FEATURE</parallelScheme>
                            <!-- This is optional, required only if you want to specify a custom template for the generated sources (this is a relative path) -->
                            <!--<customVmTemplate>src/test/resources/cucumber-custom-runner.vm</customVmTemplate>-->
                        </configuration>
                    </execution>
                </executions>
            </plugin>


            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <fork>true</fork>
                    <executable>C:\Program Files\Java\jdk1.8.0_201\bin\javac.exe</executable>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
                <configuration>
                    <!--<forkCount>5</forkCount>
                    <reuseForks>true</reuseForks>-->

                    <suiteXmlFiles>
                        <suiteXmlFile>src\main\resources\testng.xml</suiteXmlFile>
                    </suiteXmlFiles>

                    <testFailureIgnore>true</testFailureIgnore>
                    <parallel>classes</parallel>
                    <threadCount>10</threadCount>
                  <!--  <testErrorIgnore>true</testErrorIgnore>-->
                    <includes>
                        <include>src\test\java\testng.xml</include>
                        <!--<include>**/*IT.class</include>-->
                    </includes>
                </configuration>
            </plugin>
        </plugins>

    </build>


    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-server -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-server</artifactId>
            <version>3.141.59</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.cucumber/gherkin -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>gherkin</artifactId>
            <version>3.2.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-jvm -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-jvm</artifactId>
            <version>4.3.0</version>
            <type>pom</type>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.testng/testng -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.14.3</version>
            <scope>compile</scope>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.apache.maven.surefire/surefire-testng -->
        <!--    <dependency>
             <groupId>org.apache.maven.surefire</groupId>
             <artifactId>surefire-testng</artifactId>
             <version>2.22.1</version>
         </dependency>-->

        <!-- https://mvnrepository.com/artifact/info.cukes/cucumber-testng -->
        <!-- <dependency>
          <groupId>info.cukes</groupId>
          <artifactId>cucumber-testng</artifactId>
          <version>1.2.5</version>
      </dependency>-->
        <!-- https://mvnrepository.com/artifact/info.cukes/cucumber-java -->
        <!-- <dependency>
          <groupId>info.cukes</groupId>
          <artifactId>cucumber-java</artifactId>
          <version>1.2.5</version>
      </dependency>-->

        <!-- https://mvnrepository.com/artifact/info.cukes/cucumber-jvm -->
        <!--      <dependency>
               <groupId>info.cukes</groupId>
               <artifactId>cucumber-jvm</artifactId>
               <version>1.2.5</version>
               <type>pom</type>
           </dependency>-->

        <!-- https://mvnrepository.com/artifact/info.cukes/cucumber-jvm-deps -->
        <!--    <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-jvm-deps</artifactId>
            <version>1.0.5</version>
            <scope>provided</scope>
        </dependency>-->

        <!-- https://mvnrepository.com/artifact/info.cukes/gherkin -->
        <!--  <dependency>
              <groupId>info.cukes</groupId>
              <artifactId>gherkin</artifactId>
              <version>2.12.2</version>
              <scope>provided</scope>
          </dependency>
  -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>4.2.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-testng -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-testng</artifactId>
            <version>2.0.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-guice -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-guice</artifactId>
            <version>4.3.0</version>
        </dependency>

        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>4.2.3</version>
            <scope>test</scope>
        </dependency>

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

        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>datatable</artifactId>
            <version>1.1.12</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-core -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-core</artifactId>
            <version>4.3.0</version>
        </dependency>

        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-picocontainer</artifactId>
            <version>4.2.3</version>
            <scope>test</scope>
        </dependency>

    </dependencies>
</project>


Below are automatically generated testRunners
import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;

@CucumberOptions(strict = false,
features = {"C:/Users/mbethu/IdeaProjects/comparalleltestng/src/main/input/features/HomePage.feature"},
plugin = {"html:target/cucumber/1.html"},
monochrome = true,
tags = {},
glue = { "stepdefinitions" })
public class Parallel01IT extends AbstractTestNGCucumberTests {
}

[Utils][ERROR][ERROR]java.lang.NullPointerException在Cucumber.api.testng.AbstractTestngCucumberTests.Scenaries(AbstractTestngCucumberTests.java:31)在Sun.Reflect.NativeMethodAccessorInvoke0(原生方法)在Sun.Reflect.NativeMethodAccessorInvoke.Invoke(NativeMethodAccessorInvoke.62)在Ernal.ParameterHandler.CreateParameters(ParameterHandler.java:37)位于org.testng.internal.invoker.InvokeTestMethods(Invoker.java:924)位于org.testng.internal.testMethodWorker.InvokeTestMethods(TestMethodWorker.java:125)位于org.testng.internal.testMethodWorker.Run(TestMethodWorker.java:109)位于

[错误]运行的测试:6次,失败:4次,错误:0次,跳过:2次,经过的时间:0.767s<<<失败!-在TestSuite[ERROR]setUpClass(Parallel01IT)中经过的时间:0.51s<<<失败!java.lang.nosuchmethoderror:cucumber.runtime.runtime。(lcucumber/runtime/io/resourceLoader;lcucumber/runtime/classfinder;ljava/lang/classloader;lcucumber/runtime/runtimeoptions;)V

[错误]setUpClass(Parallel02IT)已用时间:0.511s<<<失败!java.lang.nosuchmethoderror:cucumber.runtime.runtime。(lcucumber/runtime/io/resourceLoader;lcucumber/runtime/classfinder;ljava/lang/classloader;lcucumber/runtime/runtimeoptions;)V

[错误]tearDownClass(Parallel01IT)已用时间:0.766s<<<失败!java.lang.NullPointerException

[INFO][INFO]结果:[INFO][ERROR]失败:[ERROR]
Parallel01it>AbstractTestNGCucumberTests.SetupClass:16?NoSuchMethod Cucumber...[错误]
Parallel02it>AbstractTestNGCucumberTests.TearDownClass:36;NullPointer[错误]
Parallel02it>AbstractTestNGCucumberTests.SetupClass:16;NoSuchMethod Cucumber...[错误]
Parallel02it>AbstractTestNGCucumberTests.TearDownClass:36?NullPointer[信息][错误]测试运行:6,失败:4,错误:0,跳过:2[信息][错误]有测试失败。

请参考C:\users\mbethu\ideaprojects\comparalleltestng\target\surefire-reports来了解各个测试结果。请参阅转储文件(如果存在)[date].dump、[date]-jvmrun[N].dump和[date].dumpstream。[INFO]---------------------生成成功[INFO]-----------------------------生成成功[INFO]--------------------------------------------------------------------------累计时间:5.143秒[INFO]完成时间:

共有1个答案

梅庆
2023-03-14

在https://github.com/temyers/cucumber-jvm-parallel-plugin上,您可以找到

从Cucumber-JVM:4.0起,Cucumber本机支持并行执行。因此,建议升级到Cucumber4.0.0,并且不再维护该插件。

注意,最新版本是4.3.0 https://mvnrepository.com/artifact/io.cucumber/cucumber-testng/4.3.0您的pom文件中有多个不同的版本-请统一。通常你的pom文件是相当混乱的。你所需要的可能就是:

    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-server -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-server</artifactId>
        <version>3.141.59</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>4.3.0</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-testng -->
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-testng</artifactId>
        <version>4.3.0</version>
    </dependency>
 类似资料:
  • 我无法同时运行TestNG/JUnit的Cucumber TestRunner类。我希望在我的框架中包含Testng和junit。我得到一些abc类错误。我的项目中有所有的依赖项。我这里有2qns。1.当从cucumber junit导入时,我可以在我的cucumber脚本中使用Testng注释吗?2.如何解决错误执行我的testrun。 我错过了什么...如果我错过了什么,请帮我把它修好。

  • 我试图用TestNG并行运行一个示例测试项目。但它是在一个线程中顺序执行的。我漏掉什么了吗? 谢了。

  • 我正在试验如何与maven surefire和testng并行运行测试。然而,配置似乎不是很简单,我无法让它工作。下面是我的虚拟测试。 这是我的surefire配置: 测试基本上是按顺序运行的。以下是日志作为证据: 我的意图是并行运行所有测试(直到方法级别)。那么,我该如何实现呢?

  • 我试图使用TestNG和SpringBootTest并行运行两个Cucumber测试,但当我的测试执行时,会发生以下情况 两个浏览器都打开并导航到维基百科主页 1个浏览器继续测试,另一个留在主页上 1个测试通过,另一个测试失败 我不知道为什么一个测试会停止执行,欢迎任何帮助。 回购:https://github.com/cmccarthyIrl/spring-cucumber-testng-par

  • 需要一些帮助来获得并行运行testng测试用例的正确方法。

  • 附加信息@Saifur我创建了一个单独的基类,在其中我初始化了驱动程序实例。我在@BeForeClass中调用这个实例,在@afterClass中调用driver.quit()。通过在testng.xml中提供两个不同的类,我试图运行这个实例。