首页 > 解决方案 > 如何使用 Cucumber 6 和 Maven 集成集成测试

问题描述

我在这里真的需要帮助。

目标:使用 Cucumber-Java8 版本 6.7.0 和 Maven 3.6.3 运行我的黄瓜集成测试。

实际场景:当我使用这个命令运行 maven 时:$ mvn clean verify -Pintegration-tests我收到了这个结果。


xxxxx @ xxx <Project Name> % mvn clean verify -Pintegration-tests
[INFO] Scanning for projects...
[INFO] 
[INFO] ----------< <Project Name> >----------
[INFO] Building <Project Name> 0.0.1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ <Project Name> ---
[INFO] Deleting /Users/<home>/Documents/repos/xxx/<Project Name>/target
[INFO] 
[INFO] --- build-helper-maven-plugin:3.1.0:add-test-source (add-source) @ <Project Name> ---


***[INFO] Test Source directory: /Users/<home>/Documents/repos/xxxx/<Project Name>/src/testintegration added.***


[INFO] 
[INFO] --- build-helper-maven-plugin:3.1.0:add-test-resource (add-resource) @ <Project Name ---
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ <Project Name> ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 13 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ <Project Name> ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 223 source files to /Users/<home>/Documents/repos/xxx/<Project Name>
...
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ <Project Name> ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 15 resources
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ n<Project Name> ---
[INFO] Changes detected - recompiling the module!

...

[INFO] 
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ <Project Name> ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ <Project Name> ---
[INFO] Building jar: /Users/<home>/Documents/repos/xxxx/<Project Name> -server/target/<Project Name>.jar
[INFO] 
[INFO] --- maven-failsafe-plugin:2.22.2:integration-test (default) @ <Project Name> ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] 
[INFO] --- maven-failsafe-plugin:2.22.2:verify (default) @ <Project Name> ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  8.484 s
[INFO] Finished at: 2020-10-17T13:07:50+01:00
[INFO] ------------------------------------------------------------------------
 




除了“Sysout”和assertTrue,我没有在我的定义类中添加任何代码。集成测试在我使用由 Intellij 创建的触发器时运行。

@RunWith(Cucumber.class)
@CucumberOptions(
    features = {"src/testintegration/java/features"},
    glue = {"steps"},
    plugin = {"pretty", "html:target/cucumber"},
    publish = true)
public class RunCucumberIT {

代码场景实际

项目结构 - Intellij

Maven 文件 - 截断


 <!-- Integration tests -->

  <profiles>
    <profile>
      <id>integration-tests</id>

      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>

      <properties>
        <cucumber.plugin>json:target/report.json</cucumber.plugin>
        <cucumber.filter.tags>@wip</cucumber.filter.tags>
      </properties>

      <build>
        <plugins>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>versions-maven-plugin</artifactId>
            <version>2.7</version>
          </plugin>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>3.1.0</version>
            <executions>
              <execution>
                <id>add-source</id>
                <phase>generate-sources</phase>
                <goals>
                  <goal>add-test-source</goal>
                </goals>
                <configuration>
                  <sources>
                    <source>src/testintegration/</source>
                  </sources>
                </configuration>
              </execution>
              <execution>
                <id>add-resource</id>
                <phase>generate-sources</phase>
                <goals>
                  <goal>add-test-resource</goal>
                </goals>
                <configuration>
                  <resources>
                    <resource>
                      <directory>src/testintegration/resources/features</directory>
                    </resource>
                  </resources>
                </configuration>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <!-- block unit tests execution -->
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${maven-surefire-plugin.version}</version>
            <configuration>
              <skip>true</skip>
            </configuration>
          </plugin>
          <plugin>
            <artifactId>maven-failsafe-plugin</artifactId>
            <executions>
              <execution>
                <goals>
                  <goal>integration-test</goal>
                  <goal>verify</goal>
                </goals>
              </execution>
            </executions>
            <configuration>
              <systemPropertyVariables>
                <cucumber.plugin>${cucumber.plugin}</cucumber.plugin>
                <cucumber.filter.tags>${cucumber.filter.tags}</cucumber.filter.tags>
              </systemPropertyVariables>
            </configuration>
          </plugin>
        </plugins>
      </build>


      <dependencies>
        <!-- Cucumber -->
        <dependency>
          <groupId>io.cucumber</groupId>
          <artifactId>cucumber-java8</artifactId>
          <version>${cucumber.version}</version>
          <scope>test</scope>
        </dependency>

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

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

      </dependencies>
    </profile>
  </profiles>
</project>

定义类

package steps;

import static org.junit.jupiter.api.Assertions.assertTrue;

import io.cucumber.java8.En;


public class Authorization implements En {

  public Authorization() {

    Given("the system knows about the following attributes",
        (io.cucumber.datatable.DataTable dataTable) -> {
          System.out.println("AUTHORIZATION GIVEN TEST");
          assertTrue(true);
        });

    When("the client request POST \\/login", () -> {
      System.out.println("AUTHORIZATION When TEST");
      assertTrue(true);
    });

    Then("the response should be JSON", (io.cucumber.datatable.DataTable dataTable) -> {
      System.out.println("AUTHORIZATION Then TEST");
      assertTrue(true);
    });

  }
}

特征文件的一部分

Feature: Authorization

  @wip
  Scenario: Login into account

...

Runner Class - (当我通过 Intellij 触发此类时,我的测试运行正确

package runner;

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

@RunWith(Cucumber.class)
@CucumberOptions(
    features = {"src/testintegration/java/features"},
    glue = {"steps"},
    plugin = {"pretty", "html:target/cucumber"},
    publish = true)
public class RunCucumberIT {
  
}

我在这里使用这个页面来尝试做我的测试。但老实说,我不知道是我做错了什么还是文章遗漏了什么。 这是网站。(网络博客)

请问,有人可以检查 Pom.xml 文件吗?我想我错过了一些东西。

非常感谢有人可以在这里帮助我。

干杯,

标签: mavencucumberintegration-testingintegration

解决方案


推荐阅读