如何使用显式Maven目标运行Cucumber测试?
我有一个应用程序同时包含Cucumber和JBehave测试,我希望每次都可以选择运行其中一个,我可以通过显式Maven目标使用JBehave来实现这一点,但问题是Cucumber在每次构建或测试中隐式运行,是否仍有停止并运行它的选项?您可以通过将Maven Surefire插件配置为默认构建的一部分或通过配置文件来实现这一点 如果是Maven build部分,则默认情况下您可以选择Cucumber测试(假设它们要么具有相同的后缀,要么属于同一个包,或者您可以安排它们满足这两个标准中的任何一个):如何使用显式Maven目标运行Cucumber测试?,maven,cucumber,cucumber-jvm,jbehave,Maven,Cucumber,Cucumber Jvm,Jbehave,我有一个应用程序同时包含Cucumber和JBehave测试,我希望每次都可以选择运行其中一个,我可以通过显式Maven目标使用JBehave来实现这一点,但问题是Cucumber在每次构建或测试中隐式运行,是否仍有停止并运行它的选项?您可以通过将Maven Surefire插件配置为默认构建的一部分或通过配置文件来实现这一点 如果是Maven build部分,则默认情况下您可以选择Cucumber测试(假设它们要么具有相同的后缀,要么属于同一个包,或者您可以安排它们满足这两个标准中的任何一个)
使用上述配置,默认情况下,Maven将执行Cucumber测试,并在执行
mvn clean install-Pexclude Cucumber
时跳过这些测试(配置文件将更改exclude.cucucumber.tests
属性的内容,从而更改Surefire插件过滤器)。当然,您也可以交换行为,使用包含cucumber
配置文件来代替。谢谢Di Matteo,但是cucumber的问题是它总是执行,因为runner标记:@RunWith(cucumber.class)
,它不需要运行pom.xml
中的任何插件,是否有任何方法可以防止这种情况并从pom.xml
控制它?但是您可以通过Surefire过滤使用@RunWith跳过/过滤类/测试用例。Maven将在构建时应用它,尽管您的IDE(即Eclipse)将继续执行它们。多亏了Matteo,我可以通过扩展Cucumber
类中的run
方法来实现这一点,并在那里检查运行时传递的参数,以决定是否运行Cucumber测试(通过调用super.run)(什么也不做)。我对JBehave做了同样的事情,扩展了JUnitStory
class。
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<excludes>
<!-- classes that include the name CucumberTest, as an example -->
<exclude>**/*CucumberTest*.java</exclude>
<!-- classes in a package whose last segment is named cucumber, as an example -->
<exclude>**/cucumber/*.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
[...]
</project>
<project>
[...]
<profiles>
<profile>
<id>cucumber-tests</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<excludes>
<exclude>none</exclude>
</excludes>
<includes>
<!-- Include your Cucumber tests, as an example -->
<include>**/*CucumberTest.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
<profile>
</profiles>
[...]
</project>
<properties>
<exclude.cucumber.tests>nothing-to-exclude</exclude.cucumber.tests>
</properties>
<profiles>
<profile>
<id>exclude-cucumber</id>
<properties>
<exclude.cucumber.tests>**/*Cucumber*.java</exclude.cucumber.tests>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>${exclude.cucumber.tests}</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>