【问题标题】:Maven plugin to export junit results导出junit结果的Maven插件
【发布时间】:2017-06-29 13:57:52
【问题描述】:

我有一个测试项目,用于测试第三方代码。我为此创建了一些 JUnit 测试,但由于我的测试是在 src/main/java 而不是 src/tests 上,我认为我不能使用surefire 来导出我的测试结果。

我需要将测试结果导出为 xml,以便 jenkins 读取它们,但考虑到我的测试不在 src/tests 上,我找不到这样做的方法。

我还可以使用surefire吗?有没有其他的 maven 插件可以做到这一点?

【问题讨论】:

    标签: java maven jenkins junit


    【解决方案1】:

    您可以使用 Maven Surefire 插件配置testSourceDirectory 标签覆盖选择测试类的默认目录。

    更多信息请参考以下链接:

    http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#testSourceDirectory

    Maven project.build.testSourceDirectory property not working from profile

    【讨论】:

    • 也可以按照maven.apache.org/guides/introduction/…指定<build><testSourceDirectory>src/main/java</testSourceDirectory></build>
    • 我的问题是,即使我将构建中的目录更改为 src/main/java‌​ry>,我仍然必须使用“mvn test”运行测试。就我而言,我生成了一个可执行 jar,并使用“java -jar”运行测试
    • 如果是这种情况,为什么不将 main 方法添加到您的 JUnit 测试套件中,如本文 stackoverflow.com/questions/4648341/… 中所述?
    • @Beginner 我有一个像这样的 TestRunner 类。我只需要当我运行这个 main 方法时,将测试结果导出到 xml,就像我运行“mvn test”时肯定会做的那样。这样,詹金斯就可以阅读它们。
    • @AlexanderRumanovsk - 看看这篇文章是否有帮助stackoverflow.com/questions/9062412/…
    【解决方案2】:

    对于那些可能对此感兴趣的人,我能够导出 XML 报告,将监听器添加到 JUnitCore,按照此处描述的示例: https://ttddyy.github.io/generate-junit-xml-report-from-junitcore/

    如果你有兴趣这样做,我建议你看一下cloudbee的代码:https://github.com/cloudbees/junit-standalone-runner

    这是我的 TestRunner 代码:

    import java.io.File;
    import java.io.FileOutputStream;
    
    import org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter;
    import org.junit.internal.TextListener;
    import org.junit.runner.Computer;
    import org.junit.runner.Description;
    import org.junit.runner.JUnitCore;
    
    import com.myproject.suites.MainTestSuite;
    import com.myproject.util.JUnitResultFormatterAsRunListener;
    
    public class TestRunner {
    
      public static void main(String[] args) {
    
        if (args.length <= 0) {
          throw new RuntimeException("Reports dir must be on arg[0]");
        }
    
        final File reportDir = new File(args[0]);
    
        JUnitCore junit = new JUnitCore();
        junit.addListener(new TextListener(System.out));
        if (reportDir != null) {
          reportDir.mkdirs();
          junit.addListener(new JUnitResultFormatterAsRunListener(new XMLJUnitResultFormatter()) {
            @Override
            public void testStarted(Description description) throws Exception {
              formatter.setOutput(new FileOutputStream(new File(reportDir, "TEST-" + description.getDisplayName()
                + ".xml")));
              super.testStarted(description);
            }
          });
        }
        junit.run(new Computer(), MainTestSuite.class).getFailureCount();
    
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-02-02
      • 2020-11-06
      • 2016-04-29
      • 1970-01-01
      • 1970-01-01
      • 2012-09-01
      • 2013-10-30
      • 2016-06-12
      • 2012-02-23
      相关资源
      最近更新 更多