Java 如何使用 Maven 通过命令提示符和 jenkins 运行单个黄瓜功能文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34772622/
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-11 15:58:34  来源:igfitidea点击:

How to run single cucumber feature files through command prompt and through jenkins using Maven?

javaseleniumjenkinscucumbercucumber-jvm

提问by Ronnie

I am bit new to Cucumber / Maven, so require help on running test cases. I have developed an automation suite in eclipse using Cucumber and Selenium. To run specific feature files / Junit runner class, I right-click on the files in Eclipse and run it.

我对 Cucumber / Maven 有点陌生,因此需要有关运行测试用例的帮助。我已经使用 Cucumber 和 Selenium 在 Eclipse 中开发了一个自动化套件。要运行特定的功能文件/Junit 运行程序类,我在 Eclipse 中右键单击文件并运行它。

But how do I run it through command prompt or Jenkins by giving specific commands to run 2-3 feature files (or) 2-3 Junit runner classes out of say 50 feature files or JUnit classes?

但是我如何通过命令提示符或 Jenkins 通过给出特定命令来运行 2-3 个特性文件(或)2-3 个 Junit 运行器类(比如 50 个特性文件或 JUnit 类)来运行它?

Below is the package explorer of how I have structured in Eclipse.

下面是我如何在 Eclipse 中构建的包浏览器。

enter image description here

在此处输入图片说明

Below is the POM.xml

下面是 POM.xml

<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.perspecsys</groupId>
    <artifactId>salesforce</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

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

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>


    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>1.1.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-picocontainer</artifactId>
            <version>1.1.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>1.1.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>2.48.2</version>
        </dependency>

    </dependencies>
</project>

回答by Sébastien Le Callonnec

You can run a single feature file by using cucumber.optionswhich will override all the options you have in the @CucumberOptionsannotation:

您可以使用cucumber.options它将覆盖@CucumberOptions注释中的所有选项来运行单个功能文件:

mvn test -Dcucumber.options="src/test/features/com/perspecsys/salesforce/featurefiles/Account.feature"

回答by Pedro Lopez

Given that you are using Maven and that you have a separate runner class per feature already, you can just use the Failsafeplugin or the Surefireplugin to pass a parameter from command line like this, and select which runner to run.

鉴于您正在使用 Maven 并且每个功能已经有一个单独的运行器类,您可以只使用Failsafe插件或Surefire插件从命令行传递参数,然后选择要运行的运行器。

Failsafe version:

故障安全版本:

-Dit.test=Account*Test,Login*Test

Surefire version:

万无一失的版本:

-Dtest=Account*Test,Login*Test

Note that you can use wildcards, pass a comma-separated list of tests and reference the full location and filter by package.

请注意,您可以使用通配符,传递以逗号分隔的测试列表并引用完整位置并按包过滤。

-Dit.test=com.perpecsys.salesforce.*Test

For this I would normally use the Failsafe plugin, as the Cucumber tests a more like integration tests (end-to-end tests really), and that is what that plugin is for, whereas the Surefire plugin is designed to run unit tests.

为此,我通常会使用 Failsafe 插件,因为 Cucumber 测试更像是集成测试(真正的端到端测试),这就是该插件的用途,而 Surefire 插件旨在运行单元测试。

More information on both plugins:

关于这两个插件的更多信息:

http://maven.apache.org/surefire/maven-failsafe-plugin/examples/single-test.html

http://maven.apache.org/surefire/maven-failsafe-plugin/examples/single-test.html

http://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html

http://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html

From Jenkins it is exactly the same, just use the Maven Project plugin to create a new Maven Project and in the Build section, under the Goal and optionsfield specify the maven command like:

从 Jenkins 中它完全相同,只需使用 Maven 项目插件创建一个新的 Maven 项目,并在构建部分中,在目标和选项字段下指定 Maven 命令,如:

clean verify -Dit.test=Account*Test

https://wiki.jenkins-ci.org/display/JENKINS/Maven+Project+Plugin

https://wiki.jenkins-ci.org/display/JENKINS/Maven+Project+Plugin

Alternatively, just add the Invoke top-level maven targetsstep to the Build if you are not using the Maven Project Plugin, and add the options there.

或者,如果您不使用 Maven 项目插件,只需将Invoke 顶级 maven 目标步骤添加到构建中,并在那里添加选项。

Hope this helps.

希望这可以帮助。

回答by William

If you wanted it hard coded, you could do something like:

如果你想要硬编码,你可以这样做:

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

@RunWith(Cucumber.class)
@CucumberOptions(
        features={"src/test/resources/features/belly.feature"}  
        ,glue =  "steps"   //whatever the name of your steps package is
        )
public class RunCukesTest 
{
}

回答by user6939

You can run a single feature file by using cucumber.options

您可以使用cucumber.options 运行单个功能文件

mvn test -Dcucumber.options="--tags @TestTag"

回答by Roman Denisenko

I had some problems with Maven execution and as I understood that most of all people are using Terminal but only one (@Pedro Lopez) described a very important point - passing 'CucumberRunner' classin Maven command when you will execute it using console, jenkins or something else.

我在 Maven 执行方面遇到了一些问题,据我所知,大多数人都在使用终端,但只有一个(@Pedro Lopez)描述了一个非常重要的点 -当您使用控制台执行它时,在 Maven 命令中传递“CucumberRunner”类,jenkins或者是其他东西。

At the same time, it's possible to do using POM:

同时,可以使用 POM:

  <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${maven-surefire-plugin.version}</version>
            <configuration>
                <includes>
                    <exclude>
                        **/CucumberRunner.java
                    </exclude>
                </includes>

Here is **/CucumberRunner.java- class that you will use as runner for passing all Cucumber options (@William mentioned about 'RunCukesTest'in my POM is CucumberRunner) and of course you can use simple command like mvn testafter POM configuration.

这是 **/CucumberRunner.java- 您将用作传递所有 Cucumber 选项的运行程序的类(@William在我的 POM 中提到了“RunCukesTest”CucumberRunner),当然您可以mvn test在 POM 配置之后使用简单的命令。

Regarding to second part of the question, I found a very simple solution. Here is possible to describe all in the Cucumber option section.

关于问题的第二部分,我找到了一个非常简单的解决方案。这里可以在 Cucumber 选项部分中描述所有内容。

@CucumberOptions(
        features = feature", // here you can pass information about main feature folder that contains all feature files
        plugin = "junit:cucumber-report/junit-report.xml",
        tags = "@Test1, @Test2, ... , @Test999",//here you can pass all list of related tests by tag
        glue = {"com.myApp.stepDefinitions"}
)

I think that this information can help to solve some problems in the future :)

我认为这些信息可以帮助解决未来的一些问题:)

happy automating with JAVA!

使用 JAVA 实现自动化!