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

时间:2016-01-13 16:59:36

标签: java selenium jenkins cucumber cucumber-jvm

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

但是如何通过命令提示符或Jenkins运行它,通过提供特定命令来运行2-3个功能文件(或)2-3个Junit运行程序类来说50个功能文件或JUnit类?

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

enter image description here

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

5 个答案:

答案 0 :(得分:19)

您可以使用cucumber.options运行单个要素文件,该文件将覆盖@CucumberOptions注释中的所有选项:

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

答案 1 :(得分:3)

如果您想要硬编码,您可以执行以下操作:

batch_size

答案 2 :(得分:3)

您可以使用cucumber.options

运行单个功能文件
mvn test -Dcucumber.options="--tags @TestTag"

答案 3 :(得分:2)

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

故障安全版:

:

Surefire版本:

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

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

-Dtest=Account*Test,Login*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 Project插件创建一个新的Maven项目,然后在Build部分的目标和选项字段下指定maven命令,如:

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

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

或者,如果您没有使用Maven Project插件,只需将 Invoke顶级maven目标步骤添加到Build中,然后在那里添加选项。

希望这有帮助。

答案 4 :(得分:1)

我在执行Maven时遇到了一些问题,据我了解,大多数人都在使用Terminal,但是只有一个人(@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

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

@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进行自动化自动化!

相关问题