【问题标题】:-Dcucumber.options ignored with mvn test-Dcucumber.options 被 mvn 测试忽略
【发布时间】:2021-02-18 13:31:49
【问题描述】:

我正在使用“mvn test”来运行黄瓜测试,但是当我尝试在命令行上传递选项时 -Dcucumber.options=...,选项被忽略,而使用运行器类中@CucumberOptions 中指定的选项。例如,如果我只是尝试显示 cucumber 帮助,它会忽略它并运行测试:

C:\Maven\ArchCuke\untitled>mvn test -Dcucumber.options="--help"
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------< org.example:untitled >------------------------
[INFO] Building untitled 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ untitled ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Maven\ArchCuke\untitled\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ untitled ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ untitled ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 2 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ untitled ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to C:\Maven\ArchCuke\untitled\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ untitled ---
[INFO]
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running org.example.steps.CucumberTestRunner

Scenario: Add two numbers # features/arith.feature:4
  Given A Calculator      # org.example.steps.ArithSteps.aCalculator()
  When I add 2 and 2      # org.example.steps.ArithSteps.iAddAnd(int,int)
  Then I get 4            # org.example.steps.ArithSteps.iGet(int)
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.253 s - in org.example.steps.CucumberTestRunner
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  5.815 s
[INFO] Finished at: 2020-11-05T10:51:50-08:00
[INFO] ------------------------------------------------------------------------

C:\Maven\ArchCuke\untitled>

这是我的跑步者课

package org.example.steps;

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(
        features = {"classpath:features"},
        plugin = {"pretty"}
)
public class CucumberTestRunner {
}

这是我的 pom.xml。

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>untitled</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>untitled</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
    <junit.version>4.13</junit.version>
    <cucumber.version>6.8.0</cucumber.version>
    <maven.compiler.version>3.8.1</maven.compiler.version>
    <maven.surefire.version>2.22.2</maven.surefire.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junit.version}</version>
    </dependency>

    <dependency>
      <groupId>io.cucumber</groupId>
      <artifactId>cucumber-java</artifactId>
      <version>${cucumber.version}</version>
    </dependency>

    <dependency>
      <groupId>io.cucumber</groupId>
      <artifactId>cucumber-junit</artifactId>
      <version>${cucumber.version}</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>${maven.compiler.version}</version>
        <configuration>
          <encoding>UTF-8</encoding>
          <source>${java.version}</source>
          <target>${java.version}</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>${maven.surefire.version}</version>
        <!-- Include our runner class or 'mvn test' won't work; name doesn't match default template.
             See https://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html -->
        <configuration>
          <includes>
            <include>CucumberTestRunner.java</include>
          </includes>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

我尝试了 surefire 插件的 3.0.0-M5 版本而不是 2.22.2,结果相同。 我在这里做错了什么?

【问题讨论】:

    标签: maven cucumber maven-surefire-plugin


    【解决方案1】:

    cucumber.options 属性已被弃用并删除。您必须将每个选项作为单个属性传递。

    mvn test -Dcucumber.filter.tags='@smoke and not @ignore'
    

    https://github.com/cucumber/cucumber-jvm/blob/main/release-notes/v5.0.0.md#property-based-options

    【讨论】:

    • 谢谢!现在都修好了。标签过滤的选项其实是 cucumber.filter.tags。
    • @user3969107 你是如何设法通过特性文件路径 maven 命令的?在黄瓜 6 中似乎没有覆盖
    【解决方案2】:

    您不应直接在 CLI 中使用系统属性。您应该将系统属性交付到万无一失的子进程

    mvn test "-DargLine=-Dcucumber.filter.tags='@smoke and not @ignore'"
    

    或者在你的 POM 中做一个小技巧,然后使用你原来的 CLI:

    <properties>
        <argLine>-Dcucumber.filter.tags='${cucumber.filter.tags}'</argLine>
    </properties>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-01
      • 2020-01-21
      • 1970-01-01
      • 2020-03-10
      • 2017-03-16
      • 1970-01-01
      • 2018-10-26
      • 1970-01-01
      相关资源
      最近更新 更多