Maven插件导出junit结果

时间:2017-06-29 13:57:52

标签: java maven jenkins junit

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

我需要将测试结果导出为xml,让jenkins读取它们,但是考虑到我的测试不在src / tests上,我找不到办法。

我还能使用surefire吗?还有其他maven插件吗?

2 个答案:

答案 0 :(得分:0)

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

请参阅以下链接以获取更多信息:

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

Maven project.build.testSourceDirectory property not working from profile

答案 1 :(得分:0)

对于那些可能对此感兴趣的人,我能够导出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();

  }
}