Java 如何使用 maven 运行黄瓜 jvm 测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21515382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:05:24  来源:igfitidea点击:

How to run cucumber-jvm tests with maven

javamavenselenium-webdrivercucumber-jvmcucumber-junit

提问by user3188928

How do I run the cucumber tests that I have in the following locations with Maven.

如何使用 Maven 运行我在以下位置进行的黄瓜测试。

Source folders 'src/main/java' and 'src/main/resources' include step definitions and feature files in package 'com.testing.TestProject.login' created in each source folder.

源文件夹“src/main/java”和“src/main/resources”包括在每个源文件夹中创建的包“com.testing.TestProject.login”中的步骤定义和功能文件。

I have included plugins and dependencies in the POM file but when I run maven phase integration-test, cucumber tests are not getting executed.

我在 POM 文件中包含了插件和依赖项,但是当我运行 maven 阶段集成测试时,没有执行黄瓜测试。

I'm new to Maven. Please let me know what to include in the POM file to run the cucumber tests with Maven.

我是 Maven 的新手。请让我知道在 POM 文件中包含哪些内容以使用 Maven 运行黄瓜测试。

Here is my POM file:

这是我的 POM 文件:

   <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>com.testing</groupId>
  <artifactId>MyMavenProject</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>MyMavenProject</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <cucumber-jvm.version>1.1.5</cucumber-jvm.version>
    <selenium.version>2.39.0</selenium.version>
    <junit.version>4.11</junit.version>
  </properties>

    <build>

    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <executions>
                <execution>
                    <phase>integration-test</phase>
                    <goals>
                        <goal>java</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <executableDependency>
                    <groupId>info.cukes</groupId>
                    <artifactId>cucumber-core</artifactId>
                </executableDependency>
                <mainClass>cucumber.api.cli.Main</mainClass>
                <arguments>
                    <argument>--format</argument>
                    <argument>junit:target/cucumber-junit-report/allcukes.xml</argument>
                    <argument>--format</argument>
                    <argument>pretty</argument>
                    <argument>--format</argument>
                    <argument>html:target/cucumber-html-report</argument>
                    <argument>--tags</argument>
                    <argument>@login</argument>
                    <argument>--glue</argument>
                    <argument>com/</argument>
                    <argument>src/</argument>
                </arguments>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>info.cukes</groupId>
                    <artifactId>cucumber-core</artifactId>
                    <version>1.1.5</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>  

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junit.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>${selenium.version}</version>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-picocontainer</artifactId>
        <version>${cucumber-jvm.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>${cucumber-jvm.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.8.7</version>
</dependency>
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.16</version>
</dependency>
<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.1.3</version>
</dependency>
</dependencies>
</project>

Thanks in advance.

提前致谢。

回答by nilesh

By maven standards tests should be in src/test/javaand not in src/main/java. Your pom.xml looks fine to me. I suspect if you import your tests and resources from mainto testfolder, your tests should execute just fine.

按照 Maven 标准,测试应该src/test/javasrc/main/java. 你的 pom.xml 对我来说看起来不错。我怀疑如果您从maintotest文件夹导入测试和资源,您的测试应该可以正常执行。

回答by Sergiu Indrie

Your test classes need to end with the "Test" suffix.

您的测试类需要以“Test”后缀结尾。

回答by Christian Hujer

The typical way to run Cucumber JVM tests from Maven is to bridge them through JUnit by providing a RunCukesTestclass that triggers Cucumber from JUnit.

从 Maven 运行 Cucumber JVM 测试的典型方法是通过提供一个RunCukesTest从 JUnit 触发 Cucumber的类来通过 JUnit 桥接它们。

A typical RunCukesTest class can look like this:

典型的 RunCukesTest 类可能如下所示:

package com.testing;

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

@RunWith(Cucumber.class)
@CucumberOptions(
        features = "src/test/resources/features",
        glue = "com.testing"
)
public class RunCukesTest { // NOSONAR SonarLint does not know about @RunWith(Cucumber.class)
}

Explanation of the details:

详细说明:

  • @RunWith(Cucumber.class)tells JUnit to not run this test class with it's default runner, but instead with Cucumberwhich is a test runner for JUnit4 that runs Cucumber from within JUnit.
  • @CucumberOptionstells Cucumberwhere to find its stuff. The main things are:
    • featurestells Cucumberwhere to find the feature files.
    • gluetells Cucumberwhich packages contain the step definitions.
  • @RunWith(Cucumber.class)告诉 JUnit 不要用它的默认运行器运行这个测试类,而是用Cucumber它来运行 JUnit4 的测试运行器,它从 JUnit 中运行 Cucumber。
  • @CucumberOptions告诉Cucumber在哪里可以找到它的东西。主要的事情是:
    • features告诉Cucumber在哪里可以找到特征文件。
    • glue告诉Cucumber哪些包包含步骤定义。