简体   繁体   English

将JUnit测试列表中的所有错误/失败记录到文本文件中

[英]Log all errors/failures from a list of JUnit tests into a text file

I'm writing a test harness and a set of junit tests to test various HTTP methods on various environments. 我正在编写一个测试工具和一组junit测试,以在各种环境下测试各种HTTP方法。 Currently I have a bunch of tests written, and ultimately what I'd like to do is to be able to output all errors/failures from the Junit tests to a text file. 目前,我编写了许多测试,最终我想要做的是能够将Junit测试中的所有错误/失败输出到文本文件。 What is the best way to accomplish this? 做到这一点的最佳方法是什么?

For example, if a test fails, I'd like to say the name of the junit test, and some information (which would come from the Test Harness class, such as the response status code and the status code description). 例如,如果测试失败,我想说一下junit测试的名称和一些信息(这些信息可能来自Test Harness类,例如响应状态代码和状态代码描述)。

Try using Apache Log4J . 尝试使用Apache Log4J

Another way is sending the information to a file. 另一种方法是将信息发送到文件。 Check this tutorial . 检查本教程 When your test fails, you can just append() the information to your file. 如果测试失败,则只需append()信息append()到文件中即可。

.append(this.class() + respone.getStatus() + response.getCode());

You should use the TestWatcher detailed on https://github.com/junit-team/junit/wiki/Rules#testwatchmantestwatcher-rules 您应该使用https://github.com/junit-team/junit/wiki/Rules#testwatchmantestwatcher-rules上详细介绍的TestWatcher

public class WatchmanTest {
  private static String watchedLog;

  @Rule
  public TestRule watchman = new TestWatcher() {
    @Override
    public Statement apply(Statement base, Description description) {
      return super.apply(base, description);
    }

    @Override
    protected void succeeded(Description description) {
      watchedLog += description.getDisplayName() + " " + "success!\n";
    }

    @Override
    protected void failed(Throwable e, Description description) {
      watchedLog += description.getDisplayName() + " " + e.getClass().getSimpleName() + "\n";
    }

    @Override
    protected void skipped(AssumptionViolatedException e, Description description) {
      watchedLog += description.getDisplayName() + " " + e.getClass().getSimpleName() + "\n";
    }

    @Override
    protected void starting(Description description) {
      super.starting(description);
    }

    @Override
    protected void finished(Description description) {
      super.finished(description);
    }
  };

  @Test
  public void fails() {
    fail();
  }

  @Test
  public void succeeds() {
  }
}

You'll implement the methods and JUnit was give you a callback on your failed() method with the exception which caused the failure and a Description object which holds more information on the test. 您将实现这些方法,并且JUnit为您的failed()方法提供了一个回调,除了导致失败的异常和一个Description对象,该对象包含有关测试的更多信息。 You shoudl also take a look at the ExternalResource class on that page as it details how you can do a reliable set up and tear down of resources to use in your TestWatcher such as the file you want to write to. 您还应该查看该页面上的ExternalResource类,因为它详细说明了如何进行可靠的设置以及拆卸要在TestWatcher中使用的资源(例如要写入的文件)的方法。

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

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