簡體   English   中英

如何使用 Maven 通過命令提示符和 jenkins 運行單個黃瓜功能文件?

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

我對 Cucumber / Maven 有點陌生,因此需要有關運行測試用例的幫助。 我已經使用 Cucumber 和 Selenium 在 Eclipse 中開發了一個自動化套件。 要運行特定的功能文件/Junit 運行程序類,我在 Eclipse 中右鍵單擊文件並運行它。

但是我如何通過命令提示符或 Jenkins 通過給出特定命令來運行 2-3 個特性文件(或)2-3 個 Junit 運行器類(比如 50 個特性文件或 JUnit 類)來運行它?

下面是我如何在 Eclipse 中構建的包瀏覽器。

在此處輸入圖片說明

下面是 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>

您可以使用cucumber.options運行單個功能文件,該文件將覆蓋@CucumberOptions注釋中的所有選項:

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

編輯(2021 年 6 月):后續版本刪除了cucumber.options構造並替換了它。 現在實現相同結果的一種方法是使用標簽。 在您的功能文件(例如@feature_file_name )頂部放置一個標簽,然后運行以下命令:

mvn test -Dcucumber.filter.tags='@feature_file_name'

您可以使用cucumber.options 運行單個功能文件

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

如果你想要硬編碼,你可以這樣做:

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 
{
}

鑒於您正在使用 Maven 並且您已經為每個功能提供了一個單獨的運行程序類,您可以只使用Failsafe插件或Surefire插件從命令行傳遞參數,然后選擇要運行的運行程序。

故障安全版本:

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

萬無一失的版本:

-Dtest=Account*Test,Login*Test

請注意,您可以使用通配符,傳遞以逗號分隔的測試列表並引用完整位置並按包過濾。

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

為此,我通常會使用 Failsafe 插件,因為 Cucumber 測試更像是集成測試(真正的端到端測試),這就是該插件的用途,而 Surefire 插件旨在運行單元測試。

關於這兩個插件的更多信息:

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

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

從 Jenkins 中它完全相同,只需使用 Maven 項目插件創建一個新的 Maven 項目,並在構建部分中,在目標和選項字段下指定 Maven 命令,如:

clean verify -Dit.test=Account*Test

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

或者,如果您不使用 Maven 項目插件,只需將Invoke 頂級 maven 目標步驟添加到構建中,並在那里添加選項。

希望這可以幫助。

我在 Maven 執行方面遇到了一些問題,據我所知,大多數人都在使用終端,但只有一個(@Pedro Lopez)描述了一個非常重要的點 - 當您使用控制台執行它時,在 Maven 命令中傳遞“CucumberRunner”類,jenkins或者是其他東西。

同時,可以使用 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>

這是**/CucumberRunner.java - 您將用作傳遞所有 Cucumber 選項的運行程序的類(@William 在我的 POM 中提到的“RunCukesTest”CucumberRunner )當然您可以在 POM 配置后使用像mvn test這樣的簡單命令。

關於問題的第二部分,我找到了一個非常簡單的解決方案。 這里可以在 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"}
)

我認為這些信息可以幫助解決未來的一些問題:)

使用 JAVA 實現自動化!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM