Java 减少JUnit输出

Java 减少JUnit输出,java,unit-testing,parsing,testing,junit,Java,Unit Testing,Parsing,Testing,Junit,我正在使用以下方法以编程方式运行JUnit测试: String res = runProcess("java -classpath " + dirPath + ";" + classpath + " org.junit.runner.JUnitCore "+ getClassName(filePath)); private static String runProcess(String command) throws Exception { Process process = Runt

我正在使用以下方法以编程方式运行JUnit测试:

String res = runProcess("java -classpath " + dirPath + ";" + classpath + " org.junit.runner.JUnitCore "+ getClassName(filePath));

private static String runProcess(String command) throws Exception {
    Process process = Runtime.getRuntime().exec(command);
    String inputStr = IOUtils.toString(process.getInputStream());
    process.waitFor();
    return inputStr;
}
但是,它会输出很多附加信息,例如失败次数、运行时间和junit信息

示例输出如下所示:

JUnit version 4.12
.E
Time: 0.011
There was 1 failure:
1) test10(ErrorTestLang)
java.lang.AssertionError: Contract failed: compareTo-equals on fraction1 and fraction4
    at org.junit.Assert.fail(Assert.java:88)
    at org.junit.Assert.assertTrue(Assert.java:41)
    at ErrorTestLang.test10(ErrorTestLang.java:32)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runners.Suite.runChild(Suite.java:128)
    at org.junit.runners.Suite.runChild(Suite.java:27)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
    at org.junit.runner.JUnitCore.runMain(JUnitCore.java:77)
    at org.junit.runner.JUnitCore.main(JUnitCore.java:36)

FAILURES!!!
Tests run: 1,  Failures: 1
我的目标是在以编程方式修改JUnit测试套件时以编程方式多次运行它,并确保结果仍然相同。也就是说,即使我删除了一些未使用的/“死”代码行,所有相同的断言都会失败/通过

因此,我对输出的一部分感兴趣,即:

java.lang.AssertionError: Contract failed: compareTo-equals on fraction1 and fraction4

因为我将把它与其他结果进行比较,不想考虑细节,比如运行或行数所花费的时间。< /P>


是否有一个标志可以使JUnit输出更简洁?

我不认为您可以通过这种方式使JUnit更简洁,毕竟您正在运行一个外部Java程序,它会产生异常,并且异常会出现在屏幕上。从技术上讲,它甚至不是例外,而是一个
java.lang.Error

这就是
org.junit.Assert#fail
的实现方式:

public static void fail(String message) {
    if(message == null) {
        throw new AssertionError();
    } else {
        throw new AssertionError(message);
    }
}
当然,您可以解析流的输出,但我建议您使用另一种方法:

您可以通过包装器来运行JUnitCore,而不是直接运行
JUnitCore
,在这个包装器中,您可以添加一个侦听器来跟踪测试执行,并能够对失败/成功执行等事件做出反应

Junit中有一个概念可以处理:

 public class MyWrapperToRun {
   public void main(String... args) {
      JUnitCore core= new JUnitCore();
      core.addListener(new MyWrappingListener());
      core.run(MyTestClass.class);   // I don't have access to MyTestClass.class
   }
 } 

 public class MyWrappingListener implements RunListener {
   // react on events emitted by junit in methods of this listener
 }

在侦听器实现中,您可以将结果存储在文件甚至数据库中,以便将来进行比较

目前,如上所述运行JUnit测试输出会产生一致的输出

我们可以获得关于失败的方法以及失败的断言的信息

/**
 * Minimize the String output obtained from running a JUnit test suite
 * 
 * @param input
 *            The String produced from running a JUnit test suite
 * @return A String that has been minimized to contain the method that
 *         contains the failing assertion and the assertion that failed
 */
private static String minimizeJUnitOutput(String input) {
    Scanner scn = new Scanner(input);

    // JUnit output starts with index 1 for first failure
    int index = 1;
    // String to represent the result that we return
    String res = "";
    // Continue until we finish processing the input String
    while (scn.hasNextLine()) {
        String line = scn.nextLine();
        // Look for a String of the form i) where i is the current failing
        // test index
        if (line.startsWith(index + ") ")) {
            res += line + "\n" + scn.nextLine() + "\n";
            index++;
        }
    }
    scn.close();
    return res;
}
这由x)表示,其中x是第x个失败的断言

/**
 * Minimize the String output obtained from running a JUnit test suite
 * 
 * @param input
 *            The String produced from running a JUnit test suite
 * @return A String that has been minimized to contain the method that
 *         contains the failing assertion and the assertion that failed
 */
private static String minimizeJUnitOutput(String input) {
    Scanner scn = new Scanner(input);

    // JUnit output starts with index 1 for first failure
    int index = 1;
    // String to represent the result that we return
    String res = "";
    // Continue until we finish processing the input String
    while (scn.hasNextLine()) {
        String line = scn.nextLine();
        // Look for a String of the form i) where i is the current failing
        // test index
        if (line.startsWith(index + ") ")) {
            res += line + "\n" + scn.nextLine() + "\n";
            index++;
        }
    }
    scn.close();
    return res;
}

前面的代码相对较好地解决了这个问题。

我不明白为什么要以这种方式启动JUnit。几乎所有的IDE都有一个运行JUnit测试的插件,您可以像自己的“初学者”一样轻松地启动它。还有一些插件在每次对代码库进行更改后运行受影响的测试(例如,对于eclipse和netbeans,infinitest)…感谢您的建议!这里的问题是我没有访问对象类的权限。这是我的程序的一个完全外部的Java文件,我可以获得类名,但不能获得表示程序中Java对象类型的类对象。我不确定我是否明白你的意思。“runProcess”方法是您的代码,您知道您运行的是JUnitCore的“main”方法吗?假设您现在已经构建了类路径X。我建议用包装器和侦听器构造classpath Y=X+模块。包装器将有一个main方法,该方法将像以前一样运行同一个JUnit内核,但不是通过main方法。可能您可以像以前一样将类作为字符串参数运行。所以包装器将成为extarnal过程的一部分,但该过程不会意识到这一事实。