【问题标题】:Run JunitCore.runClasses from Jenkins从 Jenkins 运行 JunitCore.runClasses
【发布时间】:2019-11-18 21:06:19
【问题描述】:

我有两个 junit 类:

public class TestExample1 {

  @Test
  public void test() {
      System.out.println("running test1");
  }
}

和:

public class TestExample2 {

  @Test
  public void test() {
      System.out.println("running test2");
  }
}

我在 Main 中运行这些课程:

public class Main{
    public static void main(String[] args) {
        result = JUnitCore.runClasses(TestExample1.class, TestExample2.class);
        for(Failure failure : result.getFailures()) {
            System.out.println(failure.toString());
        }
        System.out.println(result.wasSuccessful());

    }
}

如何从 Jenkins 运行这段代码?有没有可能?比如使用maven?

【问题讨论】:

    标签: java selenium jenkins junit


    【解决方案1】:

    您只需在终端中调用 mvn test 即可运行这两个测试(假设 Maven 已全部设置并且您位于项目的顶级目录中)。 Maven 将选择任何匹配此模式的测试类:

    • **/Test*.java
    • **/*Test.java
    • **/*Tests.java
    • **/*TestCase.java

    那么你根本不需要Main 类。

    查看此链接以了解更多关于使用带有 Maven 的 JUnit 5 的信息:https://maven.apache.org/surefire/maven-surefire-plugin/examples/junit-platform.html

    这个适用于 JUnit 4 及更早版本:https://maven.apache.org/surefire/maven-surefire-plugin/examples/junit.html

    如果您更喜欢使用 Main 类,可以在 Maven 中使用 mvn exec:java -Dexec.mainClass="com.example.Main"

    mvn exec 的文档在这里:https://www.mojohaus.org/exec-maven-plugin/usage.html

    【讨论】:

      猜你喜欢
      • 2016-03-07
      • 2017-10-06
      • 1970-01-01
      • 2017-07-29
      • 2014-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多