直接从可执行 jar 运行 Cucumber 测试 [英] Running Cucumber tests directly from executable jar

查看:23
本文介绍了直接从可执行 jar 运行 Cucumber 测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

现在我想在另一个系统中从命令行运行测试,该系统(应该)没有安装 Eclipse 或 Cucumber.我有一个想法,我们可以从 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><包装>罐</包装><name>storeEnabler</name><url>http://maven.apache.org</url><属性><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></属性><依赖项><依赖><groupId>info.cukes</groupId><artifactId>cucumber-java</artifactId><version>1.2.0</version></依赖><依赖><groupId>info.cukes</groupId><artifactId>cucumber-testng</artifactId><version>1.2.0</version></依赖><依赖><groupId>info.cukes</groupId><artifactId>cucumber-junit</artifactId><version>1.2.4</version></依赖><依赖><groupId>org.seleniumhq.selenium</groupId><artifactId>selenium-java</artifactId><version>2.52.0</version></依赖><依赖><groupId>javax.mail</groupId><artifactId>邮件</artifactId><version>1.4</version></依赖><依赖><groupId>io.appium</groupId><artifactId>java-client</artifactId><version>3.1.0</version></依赖><依赖><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></依赖><依赖><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.9</version></依赖><依赖><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.11-beta3</version></依赖><依赖><groupId>xml-apis</groupId><artifactId>xml-apis</artifactId><version>2.0.2</version></依赖><依赖><groupId>xerces</groupId><artifactId>xercesImpl</artifactId><version>2.8.0</version></依赖></依赖项><构建><测试资源><测试资源><目录>${project.basedir}/src/test/java</directory></testResource><测试资源><目录>${project.basedir}/src/test/resource</directory></testResource></testResources><插件管理><插件><插件><artifactId>maven-compiler-plugin</artifactId><version>3.1</version><配置><fork>true</fork><executable>C:Program FilesJavajdk1.8.0_60injavac.exe</executable><来源>1.8</来源><目标>1.8</目标></配置></插件><插件><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>2.18</version><依赖项><依赖><groupId>org.apache.maven.surefire</groupId><artifactId>surefire-junit47</artifactId><version>2.18</version></依赖></依赖项></插件><插件><artifactId>maven-assembly-plugin</artifactId><配置><存档><清单><addClasspath>true</addClasspath><mainClass>cucumber.api.cli.Main</mainClass></清单></归档><descriptorRef>jar-with-dependencies</descriptorRef></descriptorRefs></配置></插件></插件></pluginManagement></build></项目>

RunCukesTest.java

package se.stepDefinations;导入 org.junit.runner.RunWith;进口黄瓜.api.CucumberOptions;进口黄瓜.api.junit.Cucumber;@RunWith(Cucumber.class)@CucumberOptions(features = "src/test/resource/features/Printing.feature:117", plugin = { "pretty","html:target/cucumber-html-report" }, 胶水 = { "se.stepDefinations" }, 标签 = {})公共类 RunCukesTest {public static void main(String[] args) 抛出异常 {}}

  • 我在类路径中添加了 JUNIT Jar.

我以两种方式生成 jar,

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

导出后,我运行下面的命令,

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

所以我是这样运行的:

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

JUnit 4.12 版时间:0.001好(0 次测试)

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

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

<块引用>

名称为 xyz-0.0.1-SNAPSHOT 的 1 个 16kb 另一个与第二个
xyz-0.0.1-SNAPSHOT-jar-with-dependencies with 33mb

1) 我使用

运行带有依赖项的更大文件

java -jar xyz-0.0.1-SNAPSHOT-jar-with-dependencies.jar

它给了我信息:

<块引用>

没有到功能目录的路径

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

java -jar xyz-0.0.1-SNAPSHOT-jar-with-dependencies.jar se.stepDefinations.RunCukesTest

<块引用>

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

,当然正如错误所说,它正在尝试在目标文件夹中查找功能.

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

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

解决方案

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

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

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

这是一个小例子,只专注于从这样的命令行执行某事:

java -jar 可执行文件-example.jar

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

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

找到它

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

public static void main(String[] args) throws Throwable {String[] 参数 = {"foo", "bar"};黄瓜.api.cli.Main.main(参数);}

其中参数是你总是想用来执行 Cucumber 的命令行参数.

鉴于这两个步骤,您应该能够从您自己的可执行 jar 中执行 Cucumber,只要您能够执行 jar.

请注意,您正在 pom 中为 Cucumber 混合库版本.我会使用所有库的最新版本.比较cucumber-javacucumber-testngcucumber-junit.最新的 Cucumber 版本是 1.2.4.我会为他们所有人使用它.

关于从命令行运行 Cucumber 的更多信息可以在这里找到:https://cucumber.io/docs/cucumber/api/#from-the-command-line

I have a project with cucumber and maven also I am using the JUnit.

I am able to run and build my project successfully from Eclipse.

Now I want to run the test from command line in another system which does(should) not have eclipse or cucumber installed. I have an idea that we can create a JAR from jar we can run the tests by java cli commands.

Below are the combinations I am trying to run my tests from , also I am pasting the pom.xml and RunCukesTest.java file..

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 FilesJavajdk1.8.0_60injavac.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>

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 {


    }
}

  • I have added JUNIT Jar in Class path.

I am generating jars in 2 ways,

1) Exporting JAR using - > Project ->Export -> JAR File Selecting MAIN Class in the last step as : RunCukesTest as I have defined main method here for the entry point(do we require this main method in this class???)

After the export , I am running below command ,

1.1 java -jar xyz.jar I get error : NoClassDef found : org/junit/runner/JUnitCore

So I ran it this way :

1.2 java -cp xyz.jar;junit-4.12.jar org.junit.runner.JUnitCore It said,

JUnit version 4.12
Time:0.001
OK(0 tests) 

It still didnt work , So I appended the RunCukesTest file namespace at the last,
1.3 java -cp xyz.jar;junit-4.12.jar org.junit.runner.JUnitCore se.stepDefinations.RunCukesTest
It gave me error :Type cucumber.api.junit.Cucumber not present

2) So I gave up on the option for Export of jar and I am trying now to use the JAR from the maven Build. I selected the POM to run with Maven Build and it created 2 jars in the target folder ,

1 with name xyz-0.0.1-SNAPSHOT with 16kb another with 2nd with
xyz-0.0.1-SNAPSHOT-jar-with-dependencies with 33mb

1) I ran the bigger file with dependencies using

java -jar xyz-0.0.1-SNAPSHOT-jar-with-dependencies.jar

It gave me message :

Got no path to feature directory

2) So I tried appending the namespace to RunCukesTest file,

java -jar xyz-0.0.1-SNAPSHOT-jar-with-dependencies.jar se.stepDefinations.RunCukesTest

I received an error : not a file or directory found

, as of course as the error says , it is trying to find a feature inside the target folder.

Again , I want to run the JAR independent of any such project file dependencies in any other computer like an executable.

Any help would be appreciated. Thanks.

解决方案

I would divide the problem you are thinking of in two parts.

  • Create an executable jar
  • Run Cucumber from your own main method

Creating an executable jar using Maven can be done in different ways. One way of doing it is described here: http://www.thinkcode.se/blog/2011/03/05/create-an-executable-jar-from-maven

It is a small example that only focuses on executing something from a command line like this:

java -jar executable-example.jar

The example contains all dependencies. They are all bundled in the same jar. No need for any additional jars.

Next step would be to execute Cucumber from a main method. My approach would be to write a main that executes the Cucumber main method used for the command line version of Cucumber. The main method used to run cucumber from a command line lives in the cucumber-java library. You will find it at cucumber.api.cli.Main

Running a main method from another main method is done like this:

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

where arguments are the command line arguments you always want to execute Cucumber with.

Given these two steps, you should be able to execute Cucumber from your own executable jar wherever you are able to execute a jar at all.

Notice that you are mixing library version for Cucumber in your pom. I would use the latest version of all libraries. Compare cucumber-java, cucumber-testng and cucumber-junit. The latest Cucumber version is 1.2.4. I would use it for all of them.

More information about running Cucumber from a command line can be found here: https://cucumber.io/docs/cucumber/api/#from-the-command-line

这篇关于直接从可执行 jar 运行 Cucumber 测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆