gpt4 book ai didi

java - 通过可执行 JAR 从 cmdline 执行 Cucumber 测试,无需 Maven

转载 作者:行者123 更新时间:2023-12-02 09:25:50 25 4
gpt4 key购买 nike

以下是执行的步骤:

第 1 步:基于this ,我使用 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() 方法,并根据 this 传递了 cmdline 参数。 , this ,和 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)

它读取 runner 类中的 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" });

如何通过 cmdline 参数指向 JAR 内的 features 目录?

最佳答案

您将相对路径传递给了 Cucumber src\main\resources\features。因为您正在执行 Jar,所以该相对路径将使用当前工作目录转换为绝对路径。但是,由于 src\main\resources\features 是功能的源文件夹,因此找不到任何内容,您将得到:

Exception in thread "main" java.lang.IllegalArgumentException: Not a file or directory: \path\to\jar\src\main\resources\features

相反,您必须告诉 Cucumber 您的功能位于类路径上。您可以通过以下方式执行此操作:

"classpath:features"

关于java - 通过可执行 JAR 从 cmdline 执行 Cucumber 测试,无需 Maven,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58353883/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com