maven -Dcucumber. mvn测试忽略选项

qxsslcnc  于 2023-04-20  发布在  Maven
关注(0)|答案(2)|浏览(128)

我正在使用“mvn test”来运行cucumber测试,但是当我尝试在命令行上使用-Dcucumber.options=...传递选项时,这些选项会被忽略,而使用runner类中@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>

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

kqqjbcuj

kqqjbcuj1#

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

pqwbnv8z

pqwbnv8z2#

不要在CLI中直接使用system属性,应该将system属性交付给surefire子进程

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

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

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

相关问题