简体   繁体   English

Junit测试失败,没有失败跟踪

[英]Junit test to fail without failure trace

I'm running JUnit tests from Eclipse, and using the command Assert.fail(message) whenever I want to cause the test to fail. 我正在从Eclipse运行JUnit测试,并且每当我要导致测试失败时就使用命令Assert.fail(message) The problem is that I always get the message along with the entire failure trace. 问题是我总是随同整个失败跟踪一起得到消息。 Is there any way for JUnit to display the message only, without the stack trace? JUnit有什么方法可以只显示消息,而没有堆栈跟踪吗?

Eclipse JUnit settings available via Window --> Preferences --> Java --> Junit allow for cutting stack traces for certain exception classes: 通过窗口->首选项-> Java-> Junit可以使用Eclipse JUnit设置,从而可以切割某些异常类的堆栈跟踪:

JUnit首选项

With these settings, the stack trace for the test: 使用以下设置,测试的堆栈跟踪:

@Test
public void test() {
    fail("Not yet implemented");
}

is filtered: 被过滤:

过滤后的堆栈跟踪

Now try to deselect the "org.junit.*" (or "junit.framework.Assert", depending which version of JUnit you're using), and rerun the test. 现在,尝试取消选择“ org.junit。*”(或“ junit.framework.Assert”,具体取决于您所使用的JUnit版本),然后重新运行测试。 The stack trace immediately becomes scary: 堆栈跟踪立即变得令人恐惧:

全栈跟踪

Perhaps this is the feature you're looking for. 也许这就是您要寻找的功能。

If a test throws an error, the test will normally fail automatically without a message. 如果测试抛出错误,则测试通常会自动失败而不会显示消息。 You could of course catch Exceptions and let the test fail in the catch clause. 您当然可以捕获异常,并在catch子句中使测试失败。 Something like this: 像这样:

@Test
public void test()  {
   try {
      // your test
  } catch (Exception e) {
      fail("I caught an " + e.class.getSimpleName());
   }
}

Of course as always when dealing with exceptions, be as precise when catching them as possible. 当然,在处理异常时要一如既往,在捕获异常时要尽可能精确。

I found a way to crash the test without showing a long failure trace. 我找到了一种使测试崩溃的方法,而又没有显示很长的失败痕迹。 Instead of using Assert.fail(), I create my own exception, and set the failure trace to be minimal: 我创建自己的异常,而不是使用Assert.fail(),并将失败跟踪设置为最小:

public static void failTest(String message) throws MyTestFailure {
    System.err.println(message);
    MyTestFailure exception = new MyTestFailure(message);
    StackTraceElement elem = new StackTraceElement("com.my.package.Utils", "failTest", "failTest", 3);
    StackTraceElement elem1[] = new StackTraceElement[1];
    elem1[0] = elem;
    exception.setStackTrace(elem1);
    throw exception;
}

and this way I only print the message that I want to print, and a single line after that 这样,我只打印要打印的消息,然后再打印一行

I would simply override the printStackTrace()-methods for my custom Throwable: 我将为我的自定义Throwable简单地覆盖printStackTrace()方法:

  • Exception -> ERROR 异常->错误

  • AssertionError -> FAILURE AssertionError->失败

import java.io.PrintStream;
import java.io.PrintWriter;

public class TestErrorException extends Exception{

    private static final long serialVersionUID = -3486394019411535690L;
    private String message;

    public TestErrorException(String message) {
        this.message = message;
    }

    @Override
    public void printStackTrace() {
        System.err.println(message);
    }

    @Override
    public void printStackTrace(PrintStream s) {
        s.println(message);
    }

    @Override
    public void printStackTrace(PrintWriter s) {
        s.println(message);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM