2016-06-23 49 views
0

我需要在Cucumber中使用並行測試來執行兩個測試用例。如何在黃瓜中執行並行測試

我現在有兩個測試用例。一會打開google.com,另一會打開youtube.com。我已經在pom.xml中添加了某些依賴關係,並進行了一些配置。

另外我已經添加了跑步者類。

這是谷歌測試情況下,轉輪類和要素文件內容:

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"); 

} 

} 

谷歌轉輪類

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 { 

} 

谷歌特徵文件:[特徵文件名: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> 

當我執行使用「運行方式‘的Maven測試’」在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 
+0

可能重複[如何執行黃瓜功能文件並行](http://stackoverflow.com/questions/41034116/how-to-execute-cucumber-feature-file-parallel) –

+0

看看http:///stackoverflow.com/a/41100104/2895913 –

回答

1

有很多東西是不是在你的設置是正確的,你必須實例化瀏覽器黃瓜亞軍類,以便所有的步驟定義可以在實例中運行任何你想單獨運行它,你必須怎麼做這樣的事情:

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"); 

} 

} 

但有效的方法是在runner類中創建一個通用方法,以實例化並傳遞您的應用程序的url。

+0

在GoogleTest.java以及YoutubeTest.java [擴展YoutubeRunnerIT]中都提到過了,並且在方法中實例化了驅動程序。仍然無法執行。我添加了問題中收到的控制檯錯誤供您查看。 –

+0

在你的黃瓜跑步課上,膠水評論任何原因? –

+0

號沒有理由。 –

0

嘗試把forkCount在行家-萬無一失插件 的配置標記隨着forkCount = 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> 

參考從RUN 4測試類: -
https://opencredo.com/running-cucumber-jvm-tests-in-parallel/

+0

我加了forkCount = 2,因爲我有2個RunnerIT.class,但是沒有工作[測試被執行,但執行不是平行的。測試正在一個接一個地執行]。 –

0

你可以更改方法簽名並標註在步驟定義類執行
@Given("^go to google$") public void goToGoogle() { driver.get("https://www.google.co.in/gfe_rd=cr&ei=iHJrV_DiJ8SL8QfEipjgBg"); }


我對主方法中定義的步定義有疑問。

+0

試過了,你提到的,仍然沒有工作。 –