简体   繁体   English

Maven 不运行 Cucumber 测试

[英]Maven does not run Cucumber tests

I have a Maven / Spring Boot 2.3.3 application with JUnit 5 and Cucumber (v6.5.1) tests.我有一个带有 JUnit 5 和 Cucumber (v6.5.1) 测试的 Maven / Spring Boot 2.3.3 应用程序。

The thing is that I can run OK either Unit and Integration tests via Maven, but it does not run Cucumber.问题是我可以通过 Maven 运行单元测试和集成测试,但它不能运行 Cucumber。

Cucumber Runner:黄瓜跑者:

package cucumber.salad.api.integration.cucumber;
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;

@RunWith(Cucumber.class)
@CucumberOptions(
        features = "classpath:features",
        plugin = { "pretty", "html:target/reports/cucumber", "json:target/cucumber.json", "usage:target/usage.jsonx", "junit:target/junit.xml" },
        extraGlue = "cucumber.salad.api.integration.cucumber.steps"
)
public class CucumberTest {
}

Cucumber Spring Context Config: Cucumber Spring 上下文配置:

package cucumber.salad.api.integration.cucumber;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.test.context.ContextConfiguration;
import cucumber.salad.App;
import io.cucumber.spring.CucumberContextConfiguration;

@ContextConfiguration
@CucumberContextConfiguration
@SpringBootTest(classes = App.class, webEnvironment = WebEnvironment.RANDOM_PORT)
public class CucumberSpringContextConfig {
    
    @LocalServerPort
    protected int port;
}

Steps:脚步:

package cucumber.salad.api.integration.cucumber.steps;
import static org.assertj.core.api.Assertions.assertThat;
import org.springframework.beans.factory.annotation.Autowired;
import cucumber.salad.api.integration.cucumber.CucumberSpringContextConfig;
import cucumber.salad.api.service.DummyService;
import io.cucumber.java.en.*;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class SaladStepDefinitions extends CucumberSpringContextConfig {
    
    // steps here
}

I use Surefire and Failsafe in the Maven pom.xml: https://github.com/danieldestro/cucumber-salad/blob/master/pom.xml我在 Maven pom.xml 中使用了 Surefire 和 Failsafe: https : //github.com/danieldestro/cucumber-salad/blob/master/pom.xml

Here is the project repository: https://github.com/danieldestro/cucumber-salad这是项目存储库: https : //github.com/danieldestro/cucumber-salad

Command I run:我运行的命令:

mvn clean test integration-test

But there is not a sign from Cucumber test execution.但是没有来自 Cucumber 测试执行的迹象。

I am missing anything or doing something wrong?我错过了什么或做错了什么?

You're using cucumber-junit which integrates Cucumber with JUnit 4. You're also using JUnit 5. JUnit 5 can execute JUnit 4 tests through the junit-vintage-engine .您正在使用将 Cucumber 与 JUnit 4 集成的 Cucumber cucumber-junit您也在使用 JUnit 5。JUnit 5 可以通过junit-vintage-engine执行 JUnit 4 测试。 However you've excluded this engine from your classpath.但是,您已从类路径中排除了此引擎。

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

Either include the the junit-vintage-engine or use the cucumber-junit-platform-engine instead of cucumber-junit .要么包括junit-vintage-engine要么使用cucumber-junit-platform-engine而不是cucumber-junit

I had the same problem;我有同样的问题; when I ran the mvn test command the cucumber tests were not executed.当我运行mvn test命令时,没有执行黄瓜测试。 I was using:我正在使用:

  • spring-boot:2.5.0弹簧靴:2.5.0
  • Java 11 (OpenJDK Zulu 11.0.11) Java 11(OpenJDK Zulu 11.0.11)
  • Dependencies依赖关系
    • cucumber-core: 6.10.4黄瓜核:6.10.4
    • cucumber-java:6.10.4黄瓜-java:6.10.4
    • cucumber-junit:6.10.4黄瓜junit:6.10.4
    • cucumber-spring:6:10.4黄瓜春天:6:10.4

What I did was exclude the junit-jupiter-engine from the spring-boot-starter-test dependency.我所做的是从spring-boot-starter-test依赖项中排除junit-jupiter-engine

       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

If you use junit 5, all you have to do is to add the two cucumber dependencies (cucumber-java and cucmber-junit by excluding junit 4 from all your dependencies).如果您使用 junit 5,您所要做的就是添加两个黄瓜依赖项(cucumber-java 和 cucumber-junit,通过从所有依赖项中排除 junit 4)。

furthermore, import junit-vintage-engine so cucumber can use it.此外,导入 junit-vintage-engine 以便黄瓜可以使用它。

Explanation :解释 :

If you import junit 4, all the tests with jupiter (junit 5) will be ignored, Or vice versa.如果导入junit 4,则所有使用jupiter(junit 5)的测试都将被忽略,反之亦然。 You can test it by adding two classes with each version of junit (by adding @Test of each version on each class).您可以通过为每个版本的 junit 添加两个类来测试它(通过在每个类上添加每个版本的 @Test)。 Only one version will run.只有一个版本会运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM