多runner的junit实现

发布于 2024-12-11 07:21:13 字数 477 浏览 0 评论 0原文

我一直在尝试通过创建扩展运行程序的 suiterunner 来创建个性化测试套件。在用 @RunWith(suiterunner.class) 注释的测试套件中,我指的是需要执行的测试类。

在测试类中,我需要重复特定的测试,为此,我使用此处提到的解决方案: http://codehowtos.blogspot.com/2011/04/run-junit-test-repeatedly.html 。但由于我创建了一个触发测试类的 suiterunner,并且在该测试类中我正在实现 @RunWith(ExtendedRunner.class),因此会引发初始化错误。

我需要帮助来管理这 2 个跑步者,还有什么方法可以将 2 个跑步者组合起来进行特定测试?还有其他方法可以解决这个问题或者有更简单的方法吗?

I have been trying to create a personalized test suite by creating a suiterunner which extends runner. In the test suite which is annotated with @RunWith(suiterunner.class) i am referring to the test classes which need to be executed.

Within the test class i need to repeat a particular test, for doing so i am using the solution as mentioned here : http://codehowtos.blogspot.com/2011/04/run-junit-test-repeatedly.html . but since i have created a suiterunner which triggers the test class and within that test class i am implementing @RunWith(ExtendedRunner.class), an initialization error is thrown.

I need help to manage these 2 runners and also is there any way to combine 2 runners for a particular test? Is there any other way to solve this issue or any easier way to go ahead?

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

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

发布评论

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

评论(1

莫言歌 2024-12-18 07:21:13

如果您使用的是最新的 JUnit,您可能会使用 @Rules 来更清晰地解决您的问题。这是一个示例;

想象这是您的应用程序;

package org.zero.samples.junit;

/**
 * Hello world!
 * 
 */
public class App {
  public static void main(String[] args) {
    System.out.println(new App().getMessage());
  }

  String getMessage() {
    return "Hello, world!";
  }
}

这是你的测试课;

package org.zero.samples.junit;

import static org.junit.Assert.*;

import org.junit.Rule;
import org.junit.Test;

/**
 * Unit test for simple App.
 */
public class AppTest {

  @Rule
  public RepeatRule repeatRule = new RepeatRule(3); // Note Rule

  @Test
  public void testMessage() {
    assertEquals("Hello, world!", new App().getMessage());
  }
}

创建一个规则类,例如;

package org.zero.samples.junit;

import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

public class RepeatRule implements TestRule {

  private int repeatFor;

  public RepeatRule(int repeatFor) {
    this.repeatFor = repeatFor;
  }

  public Statement apply(final Statement base, Description description) {
    return new Statement() {

      @Override
      public void evaluate() throws Throwable {
        for (int i = 0; i < repeatFor; i++) {
          base.evaluate();
        }
      }
    };
  }

}

像往常一样执行您的测试用例,只是这次您的测试用例将重复给定的次数。您可能会发现有趣的用例,其中 @Rule 可能确实很方便。尝试创建复合规则,在你周围玩耍肯定会被粘住......

希望有所帮助。

In case you are using the latest JUnit you might @Rules to be a lot cleaner solution to your problem. Here is a sample;

Imagine this is your application;

package org.zero.samples.junit;

/**
 * Hello world!
 * 
 */
public class App {
  public static void main(String[] args) {
    System.out.println(new App().getMessage());
  }

  String getMessage() {
    return "Hello, world!";
  }
}

This is your test class;

package org.zero.samples.junit;

import static org.junit.Assert.*;

import org.junit.Rule;
import org.junit.Test;

/**
 * Unit test for simple App.
 */
public class AppTest {

  @Rule
  public RepeatRule repeatRule = new RepeatRule(3); // Note Rule

  @Test
  public void testMessage() {
    assertEquals("Hello, world!", new App().getMessage());
  }
}

Create a rule class like;

package org.zero.samples.junit;

import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

public class RepeatRule implements TestRule {

  private int repeatFor;

  public RepeatRule(int repeatFor) {
    this.repeatFor = repeatFor;
  }

  public Statement apply(final Statement base, Description description) {
    return new Statement() {

      @Override
      public void evaluate() throws Throwable {
        for (int i = 0; i < repeatFor; i++) {
          base.evaluate();
        }
      }
    };
  }

}

Execute your test case as usual, just that this time your test cases will be repeated for the given number of times. You might find interesting use cases where in @Rule might really prove to be handy. Try creating composite rules, play around you surely will be glued..

Hope that helps.

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