如何在黄瓜中执行并行测试

时间:2016-06-23 06:07:56

标签: java eclipse maven cucumber

我需要在Cucumber中使用并行测试执行两个测试用例。

我现在拥有两个测试用例。一个将打开google.com,另一个将打开youtube.com。我在pom.xml中添加了某些依赖项并进行了一些配置。

我还添加了跑步者课程。

这是google测试用例,运行者类和功能文件内容:

package com.cucumber.learning;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import cucumber.api.java.en.Given;

public class GoogleTest {

public static WebDriver driver = new FirefoxDriver();

@Given("go to google")
public static void main() {

    driver.get("https://www.google.co.in/?gfe_rd=cr&ei=iHJrV_DiJ8SL8QfEipjgBg");

}

}

google runner class

package com.cucumber.learning;

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

@RunWith(Cucumber.class)
@CucumberOptions(
    features = "src/test/resources/com/cucumber/learning"
    //,glue={"stepDefinition"}
    )

public class GoogleRunnerIT {

}

google功能文件:[功能文件名:1.google.feature]

@Google
Feature: Google test

 Scenario: navigate to google
 Given go to google

我也为youtube.com测试用例做了类似上面的代码。

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>com.bdd</groupId>
<artifactId>CucumberModule</artifactId>
<version>0.0.1-SNAPSHOT</version>
<url>http://maven.apache.org</url>
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>1.1.7</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>1.1.7</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>gherkin</artifactId>
        <version>2.12.1</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.53.0</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.18.1</version>
            <dependencies>
                <dependency>
                    <groupId>org.apache.maven.surefire</groupId>
                    <artifactId>surefire-junit47</artifactId>
                    <version>2.18.1</version>
                </dependency>
            </dependencies>
            <executions>
                <execution>
                    <id>acceptance-test</id>
                    <phase>integration-test</phase>
                    <goals>
                        <goal>test</goal>
                    </goals>
                    <configuration>
                        <includes>
                            <include>**/*RunnerIT.class</include>
                        </includes>
                        <parallel>all</parallel>
                        <threadCountSuites>2</threadCountSuites>
                        <useUnlimitedThreads>true</useUnlimitedThreads>
                        <testFailureIgnore>true</testFailureIgnore>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
</project>

当我使用“Run as'Maven Test'”在Eclipse中执行上述项目时,我收到了BUILD SUCCESS消息,但没有打开浏览器或测试用例正在执行。

非常感谢任何帮助。提前谢谢。

控制台错误:

[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for com.bdd:CucumberModule:jar:0.0.1-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 39, column 12
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building CucumberModule 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ CucumberModule ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/test/workspace/customparalleltesting/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ CucumberModule ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ CucumberModule ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 3 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ CucumberModule ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ CucumberModule ---
[INFO] Surefire report directory: /home/test/workspace/customparalleltesting/target/surefire-reports
[INFO] Using configured provider org.apache.maven.surefire.junitcore.JUnitCoreProvider
[INFO] parallel='none', perCoreThreadCount=true, threadCount=0, useUnlimitedThreads=false, threadCountSuites=0, threadCountClasses=0, threadCountMethods=0, parallelOptimized=true

-------------------------------------------------------
 T E S T S
 -------------------------------------------------------
 Running com.cucumber.learning.YoutubeTest
 Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.003 sec <<< FAILURE! - in com.cucumber.learning.YoutubeTest
initializationError(com.cucumber.learning.YoutubeTest)  Time elapsed: 0.001 sec  <<< ERROR!
cucumber.runtime.CucumberException: 

Classes annotated with @RunWith(Cucumber.class) must not define any
Step Definition or Hook methods. Their sole purpose is to serve as

JUnit的入口点。应定义步骤定义和挂钩 在他们自己的课堂上。这允许它们跨功能重用。 违规类:课程com.cucumber.learning.YoutubeTest

   at     cucumber.runtime.junit.Assertions.assertNoCucumberAnnotatedMethods(Assertions.java:13)
at cucumber.api.junit.Cucumber.<init>(Cucumber.java:55)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:29)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:21)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runner.Computer.getRunner(Computer.java:40)
at org.junit.runner.Computer$1.runnerForClass(Computer.java:31)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:101)
at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:87)
at org.junit.runners.Suite.<init>(Suite.java:80)
at org.junit.runner.Computer.getSuite(Computer.java:28)
at org.junit.runner.Request.classes(Request.java:75)
at org.apache.maven.surefire.junitcore.JUnitCoreWrapper.createRequestAndRun(JUnitCoreWrapper.java:97)
at org.apache.maven.surefire.junitcore.JUnitCoreWrapper.executeEager(JUnitCoreWrapper.java:78)
at org.apache.maven.surefire.junitcore.JUnitCoreWrapper.execute(JUnitCoreWrapper.java:54)
at org.apache.maven.surefire.junitcore.JUnitCoreProvider.invoke(JUnitCoreProvider.java:144)
at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)

Running com.cucumber.learning.GoogleTest
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0 sec <<< FAILURE! - in com.cucumber.learning.GoogleTest
initializationError(com.cucumber.learning.GoogleTest)  Time elapsed: 0 sec  <<< ERROR!

cucumber.runtime.CucumberException:

Classes annotated with @RunWith(Cucumber.class) must not define any
Step Definition or Hook methods. Their sole purpose is to serve as
     an entry point for JUnit. Step Definitions and Hooks should be defined
in their own classes. This allows them to be reused across features.
Offending class: class com.cucumber.learning.GoogleTest

at cucumber.runtime.junit.Assertions.assertNoCucumberAnnotatedMethods(Assertions.java:13)
at cucumber.api.junit.Cucumber.<init>(Cucumber.java:55)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:29)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:21)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runner.Computer.getRunner(Computer.java:40)
at org.junit.runner.Computer$1.runnerForClass(Computer.java:31)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:101)
at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:87)
at org.junit.runners.Suite.<init>(Suite.java:80)
at org.junit.runner.Computer.getSuite(Computer.java:28)
at org.junit.runner.Request.classes(Request.java:75)
at org.apache.maven.surefire.junitcore.JUnitCoreWrapper.createRequestAndRun(JUnitCoreWrapper.java:97)
at org.apache.maven.surefire.junitcore.JUnitCoreWrapper.executeEager(JUnitCoreWrapper.java:78)
at org.apache.maven.surefire.junitcore.JUnitCoreWrapper.execute(JUnitCoreWrapper.java:54)
at org.apache.maven.surefire.junitcore.JUnitCoreProvider.invoke(JUnitCoreProvider.java:144)
at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)


Results :

Tests in error: 
GoogleTest.initializationError » Cucumber 

Classes annotated with @RunWith(Cu...
YoutubeTest.initializationError » Cucumber 

Classes annotated with @RunWith(C...

 Tests run: 2, Failures: 0, Errors: 2, Skipped: 0

 [INFO] ------------------------------------------------------------------------
 [INFO] BUILD FAILURE
 [INFO] ------------------------------------------------------------------------
 [INFO] Total time: 2.375 s
 [INFO] Finished at: 2016-06-23T16:21:53+05:30
 [INFO] Final Memory: 10M/218M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.18.1:test (default-test) on project CucumberModule:   There are test failures.
[ERROR] 
[ERROR] Please refer to   /home/test/workspace/customparalleltesting/target/surefire-reports for the individual test results.
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

3 个答案:

答案 0 :(得分:1)

在你的设置中有许多不正确的东西,你必须从cucumber runner类实例化浏览器,这样你的所有步骤定义都可以在那个实例中运行,如果你想单独运行它,你必须做什么像这样的东西:

package com.cucumber.learning;

 import org.openqa.selenium.WebDriver;
 import org.openqa.selenium.firefox.FirefoxDriver;
 import cucumber.api.java.en.Given;

  public class GoogleTest extends GoogleRunnerIT {


 @Given("go to google")
 public static void main() {

 public static WebDriver driver = new FirefoxDriver();

driver.get("https://www.google.co.in/?gfe_rd=cr&ei=iHJrV_DiJ8SL8QfEipjgBg");

}

}

但是有效的方法是在跑步者类中创建一个常用方法,用于实例化和传递你的应用的网址。!

答案 1 :(得分:0)

尝试将 forkCount 放在maven-surefire插件的配置标记中 使用forkCount = 4,如下例所示,它将并行运行4个测试类,以RunnerIT结束

   <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.18.1</version>
        <dependencies>
            <dependency>
                <groupId>org.apache.maven.surefire</groupId>
                <artifactId>surefire-junit47</artifactId>
                <version>2.18.1</version>
            </dependency>
        </dependencies>
        <executions>
            <execution>
                <id>acceptance-test</id>
                <phase>integration-test</phase>
                <goals>
                    <goal>test</goal>
                </goals>
                <configuration>
                    <includes>
                        <include>**/*RunnerIT.class</include>
                    </includes>
                    <forkCount>4</forkCount>
                    <parallel>all</parallel>
                    <threadCountSuites>2</threadCountSuites>
                    <useUnlimitedThreads>true</useUnlimitedThreads>
                    <testFailureIgnore>true</testFailureIgnore>
                </configuration>
            </execution>
        </executions>
    </plugin>

参考文献: -
https://opencredo.com/running-cucumber-jvm-tests-in-parallel/

答案 2 :(得分:0)

是否可以更改要在步骤定义类中执行的方法的签名和注释 @Given("^go to google$") public void goToGoogle() { driver.get("https://www.google.co.in/gfe_rd=cr&ei=iHJrV_DiJ8SL8QfEipjgBg"); }

我怀疑主要方法中定义的步骤定义。