当前位置: 首页 > 知识库问答 >
问题:

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

黄正浩
2023-03-14

我有一个项目与cucumber和maven也我使用JUnit。

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

现在,我想在另一个没有安装eclipse或cucumber的系统中从命令行运行测试。我有一个想法,我们可以从JAR创建一个JAR,我们可以通过java cli命令运行测试。

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


    }
}
  • 我已经在类路径中添加了JUNIT Jar。

我以两种方式生成jar,

1)使用->project->export->JAR文件导出JAR,在最后一步中选择MAIN Class为:RunCukesTest(我在这里为入口点定义了MAIN method)(我们需要这个MAIN method吗???)

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

JUnit version 4.12
Time:0.001
OK(0 tests) 

它仍然不起作用,所以我在最后添加了RunCukesTest文件命名空间,
1.3java-cp xyz.jar;junit-4.12.jar org.junit.runner.junitcore se.stepdefinations.RunCukesTest
它给了我错误:type cucumber.api.junit.cucumber not present

2)所以我放弃了导出jar的选项,现在我尝试使用maven构建中的jar。我选择了POM与maven构建一起运行,它在目标文件夹中创建了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

它给了我一个信息:

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

同样,我希望在任何其他计算机中独立于任何此类项目文件依赖项运行JAR,就像可执行文件一样。

如有任何帮助,不胜感激。谢了。

共有1个答案

裴意
2023-03-14

我将把你想到的问题分成两部分。

  • 创建可执行JAR
  • 从您自己的主方法运行cucumber

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

这是一个小示例,它只关注从命令行执行一些东西,如下所示:

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

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

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

有了这两个步骤,您应该能够从您自己的可执行jar中执行Cucumber。

请注意,您在POM中混合了Cucumber的库版本。我会使用所有库的最新版本。比较cucumber-javacucumber-testngcucumber-junit。最新的cucumber版本是1.2.4。我会把它用在他们身上。

有关从命令行运行Cucumber的更多信息,可以在下面找到:https://Cucumber.io/docs/Cucumber/api/#from-the-command-line

 类似资料:
  • 我有一些Spring测试,这些测试可以加速应用程序上下文并测试一些服务。我能够使用Maven并通过IDE运行这些测试。现在我需要在另一台无法访问Maven的机器上运行这些测试。我的想法是创建一个测试jar并通过命令行运行它们。 所以我创建了一个自定义Runner,它调用我需要的测试类,这些测试将启动Spring Application上下文并测试一些服务。 以下是示例代码: 我的自定义跑步者: 上

  • 步骤1:在此基础上,我使用构建了一个包含所有依赖项的独立jar。 pom.xml 将方法添加到runner类,并根据this、this和this传递cmdline arg。 如何通过cmdline args指向JAR中的目录?

  • 我在Eclipse中有一个maven项目。在src/main/resources下,我有一个名为“directoryToCopy”的目录,其中包含文件。一旦我运行了我的项目,我想将“directoryToCopy”复制到桌面上的本地目录“localDirec”下。 我用了这个: 这在本地工作正常,但是当我想将其作为可执行jar文件运行时,我会得到NullPointerException。 请问有什

  • 各位工程师,大家好! 我在试图创建一个胖罐子来执行cucumber测试时遇到了一个问题。最初,我按照指南从Baeldung设置测试。当在Maven测试阶段执行时,测试运行良好。当运行带有参数的mvn exec:java命令时,它也能正常工作。 然而,当我创建了一个胖罐子并试图执行测试时,我面临着错误 以下是我的项目的解释,它基本上与Baeldung的测试项目完全一样。 项目结构 直接从可执行jar

  • 我正在用cucumber和硒网络司机一起工作。我的测试工作与预期的组合。为了实现跨浏览器测试,我添加了TestNG框架。为了验证我的跨浏览器测试运行良好,我单独使用TestNG运行了它,没有使用cucumber。它在Chrome和火狐浏览器中都运行完美。 } 测试开始了。xml文件: 我需要将TestNG测试与Cucumber设置相集成,这样我就可以用Cucumber运行整个测试。为此,我将cuc

  • 我目前正在尝试使用Cucumber实现并行测试运行。我设法使用万无一失的插件同时运行了两个不同的运行程序。现在我想检查是否可以并行运行SingleRunner文件多次。 我有一个签名测试。所以我需要在几个平台上并行运行。有可能吗? 这是我的跑步者档案 无跑道进近 工厂级 `导入org . open QA . selenium . web driver; ` 阶梯班 导入org.openqa.sel