JUnit Suite 内的日志信息

发布于 2024-09-05 02:59:33 字数 511 浏览 4 评论 0原文

我目前正在尝试在日志文件中写入 JUnite Suite 失败测试的总数。

我的测试套件定义如下:

@RunWith(Suite.class)
@SuiteClasses({Class1.class, Class2.class etc.})
public class SimpleTestSuite {}

我尝试定义一个规则,该规则会在测试失败时增加错误总数,但显然我的规则从未被调用。

@Rule
public MethodRule logWatchRule = new TestWatchman() {
    public void failed(Throwable e, FrameworkMethod method) {
         errors += 1;
    }

    public void succeeded(FrameworkMethod method) {
    }
};

关于我应该做什么来实现这种行为有什么想法吗?

I'm currently trying to write inside a log file the total number of failed tests from a JUnite Suite.

My testsuite is defined as follows:

@RunWith(Suite.class)
@SuiteClasses({Class1.class, Class2.class etc.})
public class SimpleTestSuite {}

I tried to define a rule which would increase the total number of errors when a test fails, but apparently my rule is never called.

@Rule
public MethodRule logWatchRule = new TestWatchman() {
    public void failed(Throwable e, FrameworkMethod method) {
         errors += 1;
    }

    public void succeeded(FrameworkMethod method) {
    }
};

Any ideas on what I should to do to achieve this behaviour?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

堇色安年 2024-09-12 02:59:33

以下代码对我有用。我怀疑您没有将错误声明为静态。

考虑一下 JUnit 在执行每个测试方法之前会创建一个固定装置的新实例

public class WatchmanExample {

 private static int failedCount = 0;

 @Rule
 public MethodRule watchman = new TestWatchman() {
  @Override
  public void failed(Throwable e, FrameworkMethod method) {
   failedCount++; 
  }
 };

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

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

}

The following code works for me. I suspect you are not declaring errors as static.

Consider that JUnit creates a new instance of the fixture before exercising each test method.

public class WatchmanExample {

 private static int failedCount = 0;

 @Rule
 public MethodRule watchman = new TestWatchman() {
  @Override
  public void failed(Throwable e, FrameworkMethod method) {
   failedCount++; 
  }
 };

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

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文