简体   繁体   English

junit实现多个跑步者

[英]junit implementation of multiple runners

I have been trying to create a personalized test suite by creating a suiterunner which extends runner. 我一直试图通过创建一个扩展跑步者的suiterunner来创建一个个性化的测试套件。 In the test suite which is annotated with @RunWith(suiterunner.class) i am referring to the test classes which need to be executed. 在使用@RunWith(suiterunner.class)注释的测试套件中,我指的是需要执行的测试类。

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 . 在测试类中,我需要重复一个特定的测试,为此我正在使用这里提到的解决方案: 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. 但由于我创建了一个触发测试类的suiterunner,并且在该测试类中我实现了@RunWith(ExtendedRunner.class) ,因此抛出了初始化错误。

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? 有没有其他方法可以解决这个问题或任何更简单的方法来继续?

In case you are using the latest JUnit you might @Rules to be a lot cleaner solution to your problem. 如果您使用的是最新的JUnit,那么@Rules可能会成为您问题的更清洁的解决方案。 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. 您可能会发现有趣的用例,@ Rule可能真的很方便。 Try creating composite rules, play around you surely will be glued.. 尝试创建复合规则,玩游戏肯定会粘在一起..

Hope that helps. 希望有所帮助。

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

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