-Dcucumber.options被mvn测试忽略

问题描述

我正在使用“ mvn测试”来运行黄瓜测试,但是当我尝试通过以下命令在命令行中传递选项时 -Dcucumber.options = ...,将忽略这些选项,而改用运行器类中@CucumberOptions中指定的选项。例如,如果我只是尝试显示黄瓜帮助,它将忽略它并仅运行测试:

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,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,结果相同。 我在这里做什么错了?

解决方法

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

,

您不应直接在CLI中使用系统属性。您应该系统属性交付给surefire子进程

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

或者在您的POM中进行一些技巧,然后使用原始的CLI:

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