直接从可执行jar运行Cucumber测试

Arp*_*uch 13 cucumber maven cucumber-jvm cucumber-junit cucumber-java

我有一个黄瓜和maven的项目,我也在使用JUnit.

我能够从Eclipse成功运行和构建我的项目.

现在我想在另一个系统中从命令行运行测试,该系统确实(应该)没有安装eclipse或者黄瓜.我有一个想法,我们可以从jar创建一个JAR,我们可以通过java cli命令运行测试.

下面是我尝试运行测试的组合,我也粘贴了pom.xml和RunCukesTest.java文件.

在此输入图像描述

的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>pmc</groupId>
    <artifactId>se</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

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

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

    <dependencies>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>1.2.0</version>
        </dependency>

        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-testng</artifactId>
            <version>1.2.0</version>
        </dependency>

        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>1.2.4</version>
        </dependency>

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>2.52.0</version>
        </dependency>

        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4</version>
        </dependency>

        <dependency>
            <groupId>io.appium</groupId>
            <artifactId>java-client</artifactId>
            <version>3.1.0</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.9</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.11-beta3</version>
        </dependency>

        <dependency>
            <groupId>xml-apis</groupId>
            <artifactId>xml-apis</artifactId>
            <version>2.0.2</version>
        </dependency>

        <dependency>
            <groupId>xerces</groupId>
            <artifactId>xercesImpl</artifactId>
            <version>2.8.0</version>
        </dependency>
    </dependencies>

    <build>
        <testResources>
            <testResource>
                <directory>${project.basedir}/src/test/java</directory>
            </testResource>
            <testResource>
                <directory>${project.basedir}/src/test/resource</directory>
            </testResource>
        </testResources>
        <pluginManagement>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                    <configuration>
                        <fork>true</fork>
                        <executable>C:\Program Files\Java\jdk1.8.0_60\bin\javac.exe</executable>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.18</version>
                    <dependencies>
                        <dependency>
                            <groupId>org.apache.maven.surefire</groupId>
                            <artifactId>surefire-junit47</artifactId>
                            <version>2.18</version>
                        </dependency>
                    </dependencies>
                </plugin>
                    <plugin>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <configuration>
                        <archive>
                            <manifest>
                            <addClasspath>true</addClasspath>
                                <mainClass>cucumber.api.cli.Main</mainClass>
                            </manifest>
                        </archive>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>
Run Code Online (Sandbox Code Playgroud)

RunCukesTest.java

package se.stepDefinations;


import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resource/features/Printing.feature:117", plugin = { "pretty",
        "html:target/cucumber-html-report" }, glue = { "se.stepDefinations" }, tags = {})
public class RunCukesTest {

    public static void main(String[] args) throws Exception {


    }
}
Run Code Online (Sandbox Code Playgroud)
  • 我在类路径中添加了JUNIT Jar.

我以两种方式生成罐子,

1)使用 - >项目 - >导出 - > JAR文件导出JAR在最后一步中选择MAIN类为:RunCukesTest,因为我在这里为入口点定义了main方法(我们在这个类中需要这个main方法吗?)

导出后,我在命令下运行,

1.1 java -jar xyz.jar 我收到错误:找到NoClassDef:org/junit/runner/JUnitCore

所以我这样运行:

1.2 java -cp xyz.jar;junit-4.12.jar org.junit.runner.JUnitCore 它说,

JUnit version 4.12
Time:0.001
OK(0 tests) 
Run Code Online (Sandbox Code Playgroud)

它仍然没有用,所以我在最后添加了RunCukesTest文件命名空间,
1.3 java -cp xyz.jar;junit-4.12.jar org.junit.runner.JUnitCore se.stepDefinations.RunCukesTest
它给了我错误:输入cucumber.api.junit.Cucumber不存在

2)所以我放弃了jar的导出选项,现在我正在尝试使用maven Build中的JAR.我选择POM与Maven Build一起运行,它在目标文件夹中创建了2个jar,

1名,xyz-0.0.1-SNAPSHOT,16kb,另一名,第二名,
xyz-0.0.1-SNAPSHOT-jar-with-dependencies,33mb

1)我使用依赖项运行较大的文件

java -jar xyz-0.0.1-SNAPSHOT-jar-with-dependencies.jar
Run Code Online (Sandbox Code Playgroud)

它给了我一个信息:

没有功能目录的路径

2)所以我尝试将命名空间附加到RunCukesTest文件,

java -jar xyz-0.0.1-SNAPSHOT-jar-with-dependencies.jar se.stepDefinations.RunCukesTest
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:找不到文件或目录

当然,正如错误所说,它正试图在目标文件夹中找到一个功能.

同样,我想在任何其他计算机(如可执行文件)中独立于任何此类项目文件依赖项运行JAR.

任何帮助,将不胜感激.谢谢.

Tho*_*erg 9

我会把你想到的问题分成两部分.

  • 创建一个可执行jar
  • 从您自己的main方法运行Cucumber

使用Maven创建可执行jar可以通过不同方式完成.这里描述了一种方法:http: //www.thinkcode.se/blog/2011/03/05/create-an-executable-jar-from-maven

这是一个小例子,只关注从命令行执行某些事情,如下所示:

java -jar executable-example.jar

该示例包含所有依赖项.它们都捆绑在同一个罐子里.不需要任何额外的罐子.

下一步是从main方法执行Cucumber.我的方法是编写一个main来执行用于Cucumber命令行版本的Cucumber main方法.用于从命令行运行黄瓜的主要方法存在于cucumber-java库中.你会发现它cucumber.api.cli.Main

从另一个main方法运行main方法是这样的:

public static void main(String[] args) throws Throwable {
    String[] arguments = {"foo", "bar"};
    cucumber.api.cli.Main.main(arguments);
}
Run Code Online (Sandbox Code Playgroud)

其中arguments是命令行参数,你总是希望用它来执行Cucumber.

鉴于这两个步骤,您应该能够从您自己的可执行jar执行Cucumber,无论您在哪里都可以执行jar.

请注意,您正在为您的pom中的Cucumber混合库版本.我会使用所有库的最新版本.比较cucumber-java,cucumber-testngcucumber-junit.最新的Cucumber版本是1.2.4.我会把它用于所有这些.

有关从命令行运行Cucumber的更多信息,请访问:https://cucumber.io/docs/reference/jvm#cli-runner


Rod*_*aes 6

我想扩展已接受的答案来支持 Gradle,因为这可能对某人有帮助。

鉴于这是您的项目结构

  • 。根
    • 源代码/
      • 主要的/
        • java/ --> 将您的 .java 文件放在这里
          • CukesRunner.java --> 这是你的主文件
        • 资源
          • features/ --> 将您的 .feature 文件放在这里

build.gradle 文件应该如下所示:


    apply plugin: 'java'
    
    sourceSets {
        main {
            java {
                srcDirs = ['src/main/java']
            }
            resources {
                srcDirs = ['src/main/resources']
            }
        }
    }
    
    dependencies {
         // Include your dependencies here
         compile '...'
     }
    
    configurations {
        cucumberRuntime {
            extendsFrom compile
        }
    }
    
    jar {
        from {
            // Package all dependencies in the .jar
            configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
        }
        manifest {
            // Indicate the main class for the .jar file
            attributes 'Main-Class': 'CukesRunner'
        }
    }

Run Code Online (Sandbox Code Playgroud)

然后,您可以CukesRunner.java像所建议的接受的答案一样进行自定义,但请确保调用与 jar 文件一起压缩的功能:


    apply plugin: 'java'
    
    sourceSets {
        main {
            java {
                srcDirs = ['src/main/java']
            }
            resources {
                srcDirs = ['src/main/resources']
            }
        }
    }
    
    dependencies {
         // Include your dependencies here
         compile '...'
     }
    
    configurations {
        cucumberRuntime {
            extendsFrom compile
        }
    }
    
    jar {
        from {
            // Package all dependencies in the .jar
            configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
        }
        manifest {
            // Indicate the main class for the .jar file
            attributes 'Main-Class': 'CukesRunner'
        }
    }

Run Code Online (Sandbox Code Playgroud)