在現代測試環境中,並行測試執行可以顯著提高測試過程的效率和速度。 Cucumber 是一種流行的行為驅動開發 (BDD) 框架,允許並行執行功能檔案。
要在 Cucumber 中實現並行執行,您可以使用 cucumber- jvm 並行插件。此外掛程式動態建立可以並行執行的測試運行器類別。
<code class="xml"><dependency> <groupId>com.github.temyers</groupId> <artifactId>cucumber-jvm-parallel-plugin</artifactId> <version>2.1.0</version> </dependency></code>將外掛程式加入pom.xml:
<code class="xml"><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> <glue>foo, bar</glue> <outputDirectory>${project.build.directory}/generated-test-sources/cucumber</outputDirectory> <featuresDirectory>src/test/resources/features/</featuresDirectory> <cucumberOutputDir>target/cucumber-parallel</cucumberOutputDir> <format>json</format> </configuration> </execution> </executions> </plugin></code>
呼叫產生的Runner 類別
<code class="xml"><plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.19</version> <configuration> <forkCount>5</forkCount> <reuseForks>true</reuseForks> <includes> <include>**/*IT.class</include> </includes> </configuration> </plugin></code>新增Maven Surefire 外掛程式並行呼叫產生的運行器類別:
共享WebDriver
<code class="java">public class SharedDriver extends EventFiringWebDriver { private static WebDriver REAL_DRIVER = null; static { Runtime.getRuntime().addShutdownHook(CLOSE_THREAD); } public SharedDriver() { super(CreateDriver()); } public static WebDriver CreateDriver() { WebDriver webDriver; if (REAL_DRIVER == null) webDriver = new FirefoxDriver(); setWebDriver(webDriver); return webDriver; } }</code>
以上是如何使用 Cucumber 並行執行來加速 BDD 測試?的詳細內容。更多資訊請關注PHP中文網其他相關文章!