【问题标题】:Create a executable .jar with all automated tests创建一个包含所有自动化测试的可执行 .jar
【发布时间】:2020-08-30 00:49:13
【问题描述】:

我的测试框架通过 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

无法找到或加载主类

【问题讨论】:

    标签: java junit automated-tests cucumber


    【解决方案1】:

    首先,创建自动化测试脚本的 jar 文件不是最佳实践,因为我记得我做过同样的事情。在 Windows 中,它运行良好,但在 ubuntu 中,由于缺少一些依赖项,它失败了。如果你真的想在没有 IDE 的情况下运行项目,那么我相信你应该让那个人从这里 https://maven.apache.org/download.cgi 下载 apache maven 二进制文件,然后你只需导航到 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文件,可以查看功能目录的路径和步骤定义

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

    【讨论】:

    • 很好的考虑,我会考虑的,但目前我真的需要生成 .jar 来为非专业用户提供自动化。不确定在哪里创建这个 src / test 或 src / main 项目?
    • 没问题我的朋友。我将为您创建一个示例项目:) 如果我使用 cucumber testng runner 创建黄瓜项目可以吗?
    • 我已经用项目 URL 更新了我的评论,请检查一下,如果您遇到任何问题,请告诉我 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-11
    • 2014-01-08
    • 1970-01-01
    • 1970-01-01
    • 2021-07-13
    • 2017-03-20
    相关资源
    最近更新 更多