黄瓜JUnit5未找到功能

时间:2020-07-16 14:05:28

标签: cucumber junit5 cucumber-junit

我正在尝试在我的项目中设置Cucumber。我正在使用以前项目中的相同配置,但是在运行测试时仍然遇到问题。现在,我开始怀疑问题可能是该项目使用的是JUnit 5而不是4。我在构建选项中添加了junit4以便能够使用jUnit4执行@RunWith批注,但是我仍然得到了相同的结果错误(在classpath上未找到功能)。 跑步者类如下:

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

 @RunWith(Cucumber.class)
 @CucumberOptions(features = "classpath:resources", plugin = {"pretty", "html:target/reports/cucumber/html",
"json:target/cucumber.json", "usage:target/usage.jsonx",
"junit:target/junit.xml"}, snippets = SnippetType.CAMELCASE)
public class TestCucumberRunner {

}

文件夹的结构如下:

enter image description here

这是pom配置:

enter image description here

据我所知,@RunWith注释是从junit4而不是5导入的,那么为什么会发生此问题? 我还尝试过将功能文件添加到与运行程序相同的文件夹中,并在功能选项中添加确切的路径,但仍然是相同的错误。

4 个答案:

答案 0 :(得分:1)

步骤定义也可能存在一些问题(无法通过查看信息准确地知道),看起来Cucumber找不到您的特征文件步骤定义。

请看一下cucumber documentation

您需要正确指定步骤定义的路径(胶粘路径)。

通常,cucumber jvm将在Runner类的包(或子包)中搜索。但是,您也可以通过以下方式明确提及:

@CucumberOptions(胶水= [“”,“”,“”])

答案 1 :(得分:1)

您可以使用 Junit 5 和通过 Maven 运行 Cucumber 测试。在找到合适的配置之前,我进行了大量搜索。

重要步骤:

  • 在你的插件 pom 中添加 maven-surefire-plugin,这样黄瓜测试就可以从 mvn test 运行
  • 对测试资源中的功能使用与 Cucumber java 步骤相同的结构(如果您的测试类在 com.example.usescase 中,请在 resources/com/example/usecase 中找到您的功能)
  • 在 Java 测试的根文件夹中添加黄瓜启动器。我可以只用 @Cucumber
  • 注释

感谢 https://github.com/bonigarcia ,由于它的存储库 https://github.com/bonigarcia/mastering-junit5/tree/master/junit5-cucumber

,我真的找到了如何使它工作

答案 2 :(得分:0)

使用JUnit 5设置Cucumber的文档尚未很好地记录(尚未)。诀窍是使用https://cucumber.io/docs/cucumber/api/中所述的cucumber-junit-platform-engine

例如:

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

   <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <properties>
                    <configurationParameters>
                        cucumber.plugin=pretty,html:target/site/cucumber-pretty.html
                        cucumber.publish.quiet=true
                        cucumber.publish.enabled=false
                    </configurationParameters>
                </properties>
            </configuration>
        </plugin>
    </plugins>
</build>

现在使用maven-surefire-plugin来注入Cucumber参数,因为“旧的” JUnit 4 @CucumberOptions注释将不再起作用。 可在此处找到更多黄瓜配置选项:https://github.com/cucumber/cucumber-jvm/tree/main/junit-platform-engine#configuration-options

您的Cucumber测试的Java入口点现在看起来像这样:

@RunWith(Cucumber.class)
public class BDDEntryPointTest {
    /*
    Entry point class for Cucumber test.
    It will automatically scan for
        1. *.feature files in src/test/resources
        2. Step definitions in java files under in src/test/java
    */
}

答案 3 :(得分:0)

我在使用 junit5 时遇到了类似的问题,我通过从 pom 中删除这三个依赖项来解决它

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

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

并保留这些

  <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.7.0</version>
        <scope>test</scope>
  </dependency>
  
<dependency>
  <groupId>io.cucumber</groupId>
  <artifactId>cucumber-java</artifactId>
  <version>${cucumber.version}</version>
  <scope>test</scope>
</dependency>

然后你的跑步课程将只是

@Cucumber
public class AcceptanceIT {
}

和 step defs 将是 .无 @Test 注释

  @Given("I log {string}")
        public void logSomething(String teststr )   {
            System.out.println("sample text:"+ teststr);
       }

注意我在这里使用 maven-failsafe 。如果您使用其他插件(如 maven-surefire)或使用任何其他机制,运行程序类名称可能会有所不同。这是我的 maven-failsafe 配置

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>3.0.0-M5</version>
            <executions>
              <execution>
                  <id>integration-test</id> 
                <goals>
                  <goal>integration-test</goal>
                  
                </goals>
              </execution>
             <execution>
                <id>verify</id>
                <goals>
                  <goal>verify</goal>
                </goals>
              </execution>  
            </executions>
              <configuration>
               
                <failIfNoTests>false</failIfNoTests>
   <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory> 
            
                <includes>
                    <include>**/*IT.java</include>
                </includes>
       
                </configuration>
 </plugin>