在现代测试环境中,并行测试执行可以显着提高测试过程的效率和速度。 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>
<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>
添加 Maven Surefire 插件并行调用生成的运行器类:
<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>
要并行执行测试,WebDriver 实例必须是共享的,并且在测试中不能显式关闭。 SharedDriver 类实现了这一点:
<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中文网其他相关文章!