在没有 Maven 的情况下通过可执行 JAR 从 cmdline 执行 Cucumber 测试
Execute Cucumber tests from the cmdline via an executable JAR without Maven
执行的步骤如下:
第 1 步:
基于 ,我使用 maven-shade-plugin
.
构建了一个包含所有依赖项的独立 jar
pom.xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
<testSourceDirectory>${basedir}/src/main/java/</testSourceDirectory>
<testClassesDirectory>${project.build.directory}/classes/</testClassesDirectory>
<reportsDirectory>${project.build.directory}/test-output/${timestamp}</reportsDirectory>
</configuration>
</plugin>
<!-- https://maven.apache.org/plugins/maven-shade-plugin/examples/executable-jar.html -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<!-- https://maven.apache.org/plugins/maven-shade-plugin/shade-mojo.html -->
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>standalone</shadedClassifierName>
<transformers>
<!-- https://maven.apache.org/plugins/maven-shade-plugin/examples/resource-transformers.html -->
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>runners.RunCukesTest</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
将 main()
方法添加到 运行ner class 并根据 this, this, and this.
传递 cmdline args
RunCukesTest.java
package runners;
import cucumber.api.CucumberOptions;
import cucumber.api.SnippetType;
import cucumber.api.cli.Main;
import cucumber.api.testng.AbstractTestNGCucumberTests;
@CucumberOptions(features = { "src/main/resources/features" }, glue = { "steps" }, tags = { "not @ignore" }, plugin = {
"pretty" }, monochrome = true)
public class RunCukesTest extends AbstractTestNGCucumberTests {
public static void main(String[] args) {
System.out.println("Begin running tests...");
Main.main(new String[]{ "-g", "steps", "-t", "not @ignore", "-p", "pretty", "-m", "src/main/resources/features" });
}
}
第 2 步:
试图 运行 可执行 JAR。
λ java -jar project-xyz-1.0.0-standalone.jar
Begin running tests...
Exception in thread "main" java.lang.IllegalArgumentException: Not a file or directory: \path\to\jar\src\main\resources\features
at cucumber.runtime.io.FileResourceIterator$FileIterator.<init>(FileResourceIterator.java:63)
at cucumber.runtime.io.FileResourceIterator.<init>(FileResourceIterator.java:28)
at cucumber.runtime.io.FileResourceIterator.createFileResourceIterator(FileResourceIterator.java:14)
at cucumber.runtime.io.FileResourceIterable.iterator(FileResourceIterable.java:19)
at cucumber.runtime.model.FeatureLoader.loadFromFeaturePath(FeatureLoader.java:112)
at cucumber.runtime.model.FeatureLoader.load(FeatureLoader.java:48)
at cucumber.runtime.model.FeatureLoader.load(FeatureLoader.java:30)
at cucumber.runtime.FeaturePathFeatureSupplier.get(FeaturePathFeatureSupplier.java:22)
at cucumber.runtime.Runtime.run(Runtime.java:68)
at cucumber.api.cli.Main.run(Main.java:26)
at cucumber.api.cli.Main.main(Main.java:8)
at runners.RunCukesTest.main(RunCukesTest.java:17)
它读取 运行ner class 中的 main()
方法但抛出
Exception in thread "main" java.lang.IllegalArgumentException: Not a file or directory: \path\to\jar\src\main\resources\features
更新
它似乎没有读取 JAR 中的 features
目录。
Main.main(new String[]{ "-g", "steps", "-t", "not @ignore", "-p", "pretty", "-m", "src/main/resources/features" });
如何通过命令行参数指向 JAR 中的 features
目录?
您传递了 Cucumber 的相对路径 src\main\resources\features
。因为您正在执行 Jar,所以使用当前工作目录将此相对路径转换为绝对路径。但是,因为 src\main\resources\features
是您的功能的源文件夹,所以可以找到 none 并且您得到:
Exception in thread "main" java.lang.IllegalArgumentException: Not a file or directory: \path\to\jar\src\main\resources\features
相反,您必须告诉 Cucumber 您的功能在 class 路径上。你这样做:
"classpath:features"
执行的步骤如下:
第 1 步:
基于 maven-shade-plugin
.
pom.xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
<testSourceDirectory>${basedir}/src/main/java/</testSourceDirectory>
<testClassesDirectory>${project.build.directory}/classes/</testClassesDirectory>
<reportsDirectory>${project.build.directory}/test-output/${timestamp}</reportsDirectory>
</configuration>
</plugin>
<!-- https://maven.apache.org/plugins/maven-shade-plugin/examples/executable-jar.html -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<!-- https://maven.apache.org/plugins/maven-shade-plugin/shade-mojo.html -->
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>standalone</shadedClassifierName>
<transformers>
<!-- https://maven.apache.org/plugins/maven-shade-plugin/examples/resource-transformers.html -->
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>runners.RunCukesTest</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
将 main()
方法添加到 运行ner class 并根据 this, this, and this.
RunCukesTest.java
package runners;
import cucumber.api.CucumberOptions;
import cucumber.api.SnippetType;
import cucumber.api.cli.Main;
import cucumber.api.testng.AbstractTestNGCucumberTests;
@CucumberOptions(features = { "src/main/resources/features" }, glue = { "steps" }, tags = { "not @ignore" }, plugin = {
"pretty" }, monochrome = true)
public class RunCukesTest extends AbstractTestNGCucumberTests {
public static void main(String[] args) {
System.out.println("Begin running tests...");
Main.main(new String[]{ "-g", "steps", "-t", "not @ignore", "-p", "pretty", "-m", "src/main/resources/features" });
}
}
第 2 步: 试图 运行 可执行 JAR。
λ java -jar project-xyz-1.0.0-standalone.jar
Begin running tests...
Exception in thread "main" java.lang.IllegalArgumentException: Not a file or directory: \path\to\jar\src\main\resources\features
at cucumber.runtime.io.FileResourceIterator$FileIterator.<init>(FileResourceIterator.java:63)
at cucumber.runtime.io.FileResourceIterator.<init>(FileResourceIterator.java:28)
at cucumber.runtime.io.FileResourceIterator.createFileResourceIterator(FileResourceIterator.java:14)
at cucumber.runtime.io.FileResourceIterable.iterator(FileResourceIterable.java:19)
at cucumber.runtime.model.FeatureLoader.loadFromFeaturePath(FeatureLoader.java:112)
at cucumber.runtime.model.FeatureLoader.load(FeatureLoader.java:48)
at cucumber.runtime.model.FeatureLoader.load(FeatureLoader.java:30)
at cucumber.runtime.FeaturePathFeatureSupplier.get(FeaturePathFeatureSupplier.java:22)
at cucumber.runtime.Runtime.run(Runtime.java:68)
at cucumber.api.cli.Main.run(Main.java:26)
at cucumber.api.cli.Main.main(Main.java:8)
at runners.RunCukesTest.main(RunCukesTest.java:17)
它读取 运行ner class 中的 main()
方法但抛出
Exception in thread "main" java.lang.IllegalArgumentException: Not a file or directory: \path\to\jar\src\main\resources\features
更新
它似乎没有读取 JAR 中的 features
目录。
Main.main(new String[]{ "-g", "steps", "-t", "not @ignore", "-p", "pretty", "-m", "src/main/resources/features" });
如何通过命令行参数指向 JAR 中的 features
目录?
您传递了 Cucumber 的相对路径 src\main\resources\features
。因为您正在执行 Jar,所以使用当前工作目录将此相对路径转换为绝对路径。但是,因为 src\main\resources\features
是您的功能的源文件夹,所以可以找到 none 并且您得到:
Exception in thread "main" java.lang.IllegalArgumentException: Not a file or directory: \path\to\jar\src\main\resources\features
相反,您必须告诉 Cucumber 您的功能在 class 路径上。你这样做:
"classpath:features"