java - 运行 Maven 测试时 Cucumber 测试未执行

标签 java maven cucumber pom.xml

我有一个 cucumber 项目。当我右键单击 RunnerTest 类并“运行“RunnerTest””时,功能文件中的所有功能都会开始运行。所有测试都通过。

我的 RunnerTest.class

   import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import java.sql.SQLException;
import lombok.extern.log4j.Log4j2;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(
features = {"classpath:foo.feature"},
glue = "com.foo.foobar.StepDefinitions",
plugin = {"json:target/cucumber-report/cucumber.json"},
monochrome = true,
strict = true
//,dryRun = true
)

@Log4j2
public class RunnerTest {}

但是当我尝试运行 mvn test 或 mvn clean install 功能时,未运行。 这是输出。

<小时/>

测试

运行测试套件 配置 TestNG:org.apache.maven.surefire.testng.conf.TestNG652Configurator@515f550a 测试运行:0,失败:0,错误:0,跳过:0,已用时间:0.895 秒

结果:

测试运行:0,失败:0,错误:0,跳过:0

这些是我的 POM 依赖项

<dependency>
  <groupId>io.cucumber</groupId>
  <artifactId>cucumber-junit</artifactId>
  <version>3.0.0</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>io.cucumber</groupId>
  <artifactId>cucumber-core</artifactId>
  <version>3.0.0</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>io.cucumber</groupId>
  <artifactId>cucumber-java</artifactId>
  <version>3.0.0</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>io.cucumber</groupId>
  <artifactId>cucumber-jvm</artifactId>
  <version>3.0.0</version>
  <type>pom</type>
</dependency>
<dependency>
  <groupId>io.cucumber</groupId>
  <artifactId>cucumber-jvm-deps</artifactId>
  <version>1.0.6</version>
</dependency>
<dependency>
  <groupId>io.cucumber</groupId>
  <artifactId>gherkin</artifactId>
  <version>4.1.3</version>
</dependency>
<!--<dependency>-->
  <!--<groupId>junit</groupId>-->
  <!--<artifactId>junit</artifactId>-->
  <!--<version>4.12</version>-->
<!--</dependency>-->
<dependency>
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>
  <version>6.14.3</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <version>1.18.6</version>
  <scope>test</scope>
</dependency>

我尝试添加 mvn Surefire 插件并在其中包含我的 RunnerTest 类。

<build>
 <pluginManagement>
  <plugins>
   <plugin>
     <artifactId>maven-surefire-plugin</artifactId>
     <version>2.12.4</version>
     <configuration>
       <includes>
       <include>RunnerTest.java</include>
       </includes>
     </configuration>
   </plugin>
  </plugins>
 </pluginManagement>
</build>

但这也不起作用

最佳答案

已知问题:如果并行保留 Junit 和 TestNG 这两个依赖项,则 TestNG 依赖项会导致 Surefire 忽略 JUnit 包装类。

解决方案:可能有多种方法来处理此问题,例如我们可以定义 2 个执行,每个执行用于 TestNG 和 JUnit,并根据您的需要禁用一个。

您可以尝试一下吗:请删除任何直接/间接 TestNG 依赖项。

组织测试 测试 6.14.3 测试

并尝试添加以下一个 -

io. cucumber cucumber 测试 3.0.0

此外,我还建议您做一件事以保持 pom.xml 干净。

要点:

  • 我们不会混合直接依赖和传递依赖,特别是它们的版本!这样做可能会导致不可预测的结果。

通过 JUnit 执行 Cucumber

您应该删除 cucumber-core、cucumber-java、cucumber-jvm、cucumber-jvm-deps、gherkin,因为这些是传递依赖项,当您添加以下直接(主要)依赖项时,Maven 将添加这些依赖项。只需添加下面的 2 个和上面共享的 testng 的 1 个即可。

 <dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-junit</artifactId>
    <version>3.0.0</version>
</dependency>

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-picocontainer</artifactId>
    <version>3.0.0</version>
</dependency>

关于java - 运行 Maven 测试时 Cucumber 测试未执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57249027/

相关文章:

java - 垂直于一点的 vector

java - JBoss 和一个单独的 TomCat 在同一台机器上

java - IntelliJ Maven 与普通 Maven

java - Maven 构建抛出 java.lang.NoClassDefFoundError

ruby-on-rails - cucumber 和 capybara ,如何打开外部网址或访问外部网址

ruby-on-rails - 用 cucumber / capybara 测试 ActiveMerchant

java - 从同一 Controller 的另一个 REST 端点直接调用 REST 端点(方法)是一种好习惯吗?

java - 带有静态类列表的 Junit SuiteClasses

java - 测量驻留在单独项目中的 Selenium 测试的代码覆盖率

ruby-on-rails - 在 Rails 应用程序中,如何组织 Cucumber .feature 文件?