【问题标题】:Cucumber tests not executing when running maven test运行 Maven 测试时未执行黄瓜测试
【发布时间】:2019-12-06 12:14:06
【问题描述】:

我有一个黄瓜项目。当我右键单击 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 时,功能没有运行。 这是输出。


T E S T S

运行测试套件 配置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 注释 (@RunWith),而您的 Maven 输出显示它正在尝试使用 TestNG
  • @JeroenSteenbeeke 谢谢。你能告诉我如何克服这个问题吗?我也需要我的 TestNG 依赖项,因为我正在使用 testNG 断言
  • 我对 Cucumber 的经验有限,也没有使用 TestNG 的经验,所以我能做的最好的就是询问 Google。我的发现表明您应该删除@RunWith 注释并让测试类扩展AbstractTestNGCucumberTests(来源:github.com/cucumber/cucumber-jvm/tree/master/testng
  • @JeroenSteenbeeke 啊谢谢。但我的期望是使用 Junit 而不是 TestNG 运行它。就目前而言,我评论了非常 TestNG 依赖并尝试再次运行。仍然 maven 尝试使用 TestNG 执行。不知道为什么。我也在谷歌搜索。谢谢你的建议。

标签: java maven cucumber pom.xml


【解决方案1】:

已知问题:如果你让 Junit 和 TestNG 两个依赖项并行,那么 TestNG 依赖项会导致 Surefire 忽略 JUnit 包装类。

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

您可以试试这个吗:请删除任何直接/间接的 TestNG 依赖项。

org.testng 测试 6.14.3 测试

并尝试添加以下 -

io.黄瓜 黄瓜测试 3.0.0

另外,我建议你再做一件事来保持你的 pom.xml 干净。

关键点:

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

通过 JUnit 执行 Cucumber

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

 <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>

【讨论】:

  • 感谢您的建议。我按照您的要求删除了所有其他依赖项。但是当我运行它时,它再次与 TestNG 一起运行。所以我也删除了 cucumber-testNG 依赖项。然后它开始使用 Junit 包装器 Sclass。 o 我想我根本无法保留任何 testNG 依赖项。
猜你喜欢
  • 2020-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-28
  • 1970-01-01
  • 2017-10-13
  • 2022-10-05
相关资源
最近更新 更多