使用 Maven 在 CLI 中运行单个 Cucumber 功能文件会导致“测试运行:0”

问题描述 投票:0回答:1

我的项目是使用 Eclipse 构建的。如果我从那里甚至 CLI 运行 Maven 测试,我会得到结果 Tests run: 15。 如果我尝试使用命令运行单个黄瓜功能文件

mvn test -Dsurefire.includeJUnit5Engines=cucumber -Dcucumber.plugin=pretty -Dcucumber.features=/path/to/individual/feature/file

那么输出就是

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------< restApiTest:cucumber >------------------------
[INFO] Building cucumber 0.0.1-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- resources:3.3.1:resources (default-resources) @ cucumber ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\tiger\Documents\Code\Java\cucumber\src\main\resources
[INFO]
[INFO] --- compiler:3.11.0:compile (default-compile) @ cucumber ---
[INFO] No sources to compile
[INFO]
[INFO] --- resources:3.3.1:testResources (default-testResources) @ cucumber ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 4 resources from src\test\resources to target\test-classes
[INFO]
[INFO] --- compiler:3.11.0:testCompile (default-testCompile) @ cucumber ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- surefire:3.2.2:test (default-test) @ cucumber ---
[INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider
[INFO]
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
Jan 11, 2024 2:14:15 P.M. io.cucumber.junit.platform.engine.DiscoverySelectorResolver warnWhenCucumberFeaturesPropertyIsUsed
WARNING: Discovering tests using the cucumber.features property. Other discovery selectors are ignored!
Please request/upvote/sponsor/ect better support for JUnit 5 discovery selectors.
See: https://github.com/cucumber/cucumber-jvm/pull/2498

Scenario Outline: Login as user                                     # src/test/resources/restApiTest/cucumber/login.feature:14
  Given the correct api endpoint as "/api/login"                    # restApiTest.cucumber.StepDefinitions.the_correct_api_endpoint_as(java.lang.String)
  When the payload consist of "[email protected]" and "cityslicka" # restApiTest.cucumber.StepDefinitions.the_payload_consist_of_and(java.lang.String,java.lang.String)
  And endpoint is pinged                                            # restApiTest.cucumber.StepDefinitions.endpoint_is_pinged()
  Then response status is 200                                       # restApiTest.cucumber.StepDefinitions.response_status_is(java.lang.Integer)
  And response body contains "token" and "QpwL5tke4Pnpja7X4"        # restApiTest.cucumber.StepDefinitions.response_body_contains_and(java.lang.String,java.lang.String)

Scenario Outline: Login as user                             # src/test/resources/restApiTest/cucumber/login.feature:15
  Given the correct api endpoint as "/api/login"            # restApiTest.cucumber.StepDefinitions.the_correct_api_endpoint_as(java.lang.String)
  When the payload consist of "peter@klaven" and ""         # restApiTest.cucumber.StepDefinitions.the_payload_consist_of_and(java.lang.String,java.lang.String)
  And endpoint is pinged                                    # restApiTest.cucumber.StepDefinitions.endpoint_is_pinged()
  Then response status is 400                               # restApiTest.cucumber.StepDefinitions.response_status_is(java.lang.Integer)
  And response body contains "error" and "Missing password" # restApiTest.cucumber.StepDefinitions.response_body_contains_and(java.lang.String,java.lang.String)
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  7.658 s
[INFO] Finished at: 2024-01-11T14:14:20-05:00
[INFO] ------------------------------------------------------------------------

我尝试了几种在线解决方案,但似乎都不起作用。我的pom.xml如下:

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://maven.apache.org/POM/4.0.0"
         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>restApiTest</groupId>
    <artifactId>cucumber</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>io.cucumber</groupId>
                <artifactId>cucumber-bom</artifactId>
                <version>7.15.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.junit</groupId>
                <artifactId>junit-bom</artifactId>
                <version>5.10.1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit-platform-engine</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-suite</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <scope>test</scope>
        </dependency>
        
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>5.4.0</version>
            <scope>test</scope>
        </dependency>
        
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>json-path</artifactId>
            <version>5.4.0</version>
            <scope>test</scope>
        </dependency>
        
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.16.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.11.0</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <source>1.8</source>
                    <target>1.8</target>
                    <release>9</release>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.2.2</version>
            </plugin>
            <plugin>
                <groupId>net.masterthought</groupId>
                <artifactId>maven-cucumber-reporting</artifactId>
                <version>5.7.8</version>
                <executions>
                    <execution>
                        <id>execution</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <projectName>cucumber</projectName>
                            <skip>false</skip>
                            <!-- output directory for the generated report -->
                            <outputDirectory>${project.build.directory}</outputDirectory>
                            <!-- optional, defaults to outputDirectory if not specified -->
                            <inputDirectory>${project.build.directory}/jsonReports</inputDirectory>
                            <jsonFiles>
                                <!-- supports wildcard or name pattern -->
                                <param>**/*.json</param>
                            </jsonFiles>
                            <!-- optional, set true to group features by its Ids -->
                            <mergeFeaturesById>false</mergeFeaturesById>
                            <!-- optional, set true to get a final report with latest results of the same test from different test runs -->
                            <mergeFeaturesWithRetest>false</mergeFeaturesWithRetest>
                            <!-- optional, set true to fail build on test failures -->
                            <checkBuildResult>false</checkBuildResult>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

我的测试运行器配置为

@Suite
@IncludeEngines("cucumber")
@SelectClasspathResource("restApiTest/cucumber")
@ConfigurationParameter(key = PLUGIN_PROPERTY_NAME, value = "pretty, json:target/jsonReports/cucumber.json")
public class RunCucumberTest {
}

我现在陷入困境,希望得到帮助?

更新:根据评论,删除了片段并添加了完整的输出并添加了完整的 pom.xml。

eclipse maven terminal cucumber junit5
1个回答
0
投票

将 junit-jupiter 工件修改为 junit-jupiter-engine 并更新 maven-surefire-plugin,如下所示:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>${maven.surefire.plugin.version}</version>
    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.jupiter.version}</version>
        </dependency>
     </dependencies>
</plugin>

对于 cucumber runner,添加

import static io.cucumber.junit.platform.engine.Constants.GLUE_PROPERTY_NAME;
作为导入之一,然后添加以下配置参数:

@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "restApiTest")
© www.soinside.com 2019 - 2024. All rights reserved.