繁体   English   中英

junit实现多个跑步者

[英]junit implementation of multiple runners

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

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

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

如果您使用的是最新的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可能真的很方便。 尝试创建复合规则,玩游戏肯定会粘在一起..

希望有所帮助。

暂无
暂无

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

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