gpt4 book ai didi

java - 创建包含所有自动化测试的可执行 .jar

转载 作者:行者123 更新时间:2023-12-01 16:45:48 25 4
gpt4 key购买 nike

我的测试框架通过 TestRunner 类运行所有 src/test/java 测试,并且在 Eclipse 上运行时效果很好。

但是,我需要为最终用户提供这种自动化,以便在他们的计算机上运行,​​而不需要安装 Eclipse IDE。

我通过maven尝试了多种方法,但没有成功,我需要帮助。

TestRunner.java 类

@RunWith(Cucumber.class)
@CucumberOptions(features = { "src/test/resources/features" }, plugin = { "pretty", "html:target/cucumber-html-report",
"json:target/cucumber-json-report/cucumber.json" },
monochrome = true, glue = { "" })

public class TestRunner {
}

我尝试生成 .jar 的 Maven 命令

mvn clean instal package
mvn test-compile
mvn test
mvn build

The main class could not be found or loaded

Framework

最佳答案

首先,创建自动化测试脚本的 jar 文件不是最佳实践,因为我记得我做了同样的事情。在 Windows 中,它工作得很好,但在 ubuntu 中,由于缺少一些依赖项,它失败了。如果你真的想在没有 IDE 的情况下运行该项目,那么我相信你应该要求那个人从这里下载 apache maven 二进制文件 https://maven.apache.org/download.cgi然后您只需导航到 pom.xml 所在的根项目文件夹并运行 maven 命令并确保在环境变量中添加 apache maven 二进制文件路径。如果您想使用 java 代码执行它,那么您也可以使用 Maven 调用程序依赖项,如果您真的愿意使其可执行,那么您需要在 Spring Boot 中创建这个项目,这可以帮助您满足您的要求。

InvocationRequest request = new DefaultInvocationRequest();
request.setPomFile(session.getRequest().getPom());
request.setGoals(goals);
request.setAlsoMake(true);
request.setUpdateSnapshots(updateSnapshots);
Properties props = new Properties();
props.putAll(properties);
request.setProperties(props);
Invoker invoker = new DefaultInvoker();
boolean success;
try {
InvocationResult result = invoker.execute(request);
success = result.getExitCode() == 0 && result.getExecutionException() == null;
} catch (MavenInvocationException e) {
throw new MojoExecutionException("Invocation error! " + ABORT, e);
}
failIf(!success, "An error occurred. " + ABORT);
<小时/>

我创建了一个示例项目。您可以从下面下载。

https://drive.google.com/open?id=1ftIO06_92mZY7WkjcAiFTJJip5KURv4l

<小时/>

创建和执行可运行文件的步骤:

Step 1: Execute the MainClass.java file to create class file of it.
Step 2: In eclipse, Please export the project as Runnable jar
Step 3: Run the runnable jar with the following command
Command: java -jar <name of the jar file>.jar classpath:<path of the features directory in jar file> --glue <path of the stepdefinitions directory in jar>
<小时/>

提示:您可以使用7zip打开jar文件,可以查看feature目录的路径和步骤定义

注意:这不是一个好的做法。所以我建议直接使用maven项目而不是jar文件:)。

关于java - 创建包含所有自动化测试的可执行 .jar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61781205/

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