我有一个 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>
<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>
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 {
}
}
我以两种方式生成 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)
它仍然没有用,所以我在最后附加了RunCukesTest文件 namespace ,
1.3
java -cp xyz.jar;junit-4.12.jar org.junit.runner.JUnitCore se.stepDefinations.RunCukesTest
它给我错误:Typecumber.api.junit.Cucumber不存在2)因此,我放弃了jar导出选项,现在尝试使用maven Build中的JAR。
我选择了要与Maven Build一起运行的POM,它在目标文件夹中创建了2个jar,
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)我使用依赖项运行了更大的文件
java -jar xyz-0.0.1-SNAPSHOT-jar-with-dependencies.jar
它给了我消息:
Got no path to feature directory
2)所以我尝试将 namespace 附加到RunCukesTest文件,
java -jar xyz-0.0.1-SNAPSHOT-jar-with-dependencies.jar se.stepDefinations.RunCukesTest
I received an error : not a file or directory found
,当然如错误所示,它正在尝试在目标文件夹中查找功能。
同样,我想在任何其他计算机(如可执行文件)中独立于任何此类项目文件依赖项来运行JAR。
任何帮助,将不胜感激。谢谢。
最佳答案
我将您正在考虑的问题分为两个部分。
使用Maven创建可执行jar可以通过不同方式完成。这里描述了一种实现方法:
http://www.thinkcode.se/blog/2011/03/05/create-an-executable-jar-from-maven
这是一个小示例,仅着重于从命令行执行以下操作:
java -jar可执行文件-example.jar
该示例包含所有依赖项。它们都 bundle 在同一个 jar 里。无需任何其他的 jar 。
下一步将是从main方法执行Cucumber。我的方法是编写一个执行用于命令行版本的Cucumber的Cucumber main方法的main。从命令行运行 cucumber 的主要方法位于
cucumber-java
库中。您可以在cucumber.api.cli.Main
上找到它从另一个主要方法运行一个主要方法是这样的:
public static void main(String[] args) throws Throwable {
String[] arguments = {"foo", "bar"};
cucumber.api.cli.Main.main(arguments);
}
其中参数是您始终要执行Cucumber的命令行参数。
有了这两个步骤,无论您在哪里执行 jar ,都应该能够从自己的可执行 jar 中执行Cucumber。
请注意,您正在pom中混合Cucumber的库版本。我将使用所有库的最新版本。比较
cucumber-java
,cucumber-testng
和cucumber-junit
。最新的Cucumber版本是1.2.4。我会为所有人使用它。有关从命令行运行Cucumber的更多信息,请参见:https://cucumber.io/docs/cucumber/api/#from-the-command-line
关于maven - 直接从可执行jar运行Cucumber测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56689421/