Usage and Idioms——Test runners

IDE support - graphical runners

NetBeans, Eclipse and IntelliJ Idea有自带的内置graphical test runners

 基于Test runner的操控台

JUnit提供了能定义、运行测试集并展示测试结果的工具。为了运行测试并查看结果,有下列方法:

方法一:在程序中运行:org.junit.runner.JUnitCore.runClasses(TestClass1.class, ...);

方法二:在命令行中运行:java org.junit.runner.JUnitCore TestClass1 [...other test classes...]

详细说明请见:

http://junit.org/javadoc/latest/org/junit/runner/JUnitCore.html

 对旧版runner的适配

如果想通过旧版(与JUnit-3适配)的test runner来运行JUnit-4的测试用例,需要加上JUnit4TestAdapter。

具体用法,在测试类中添加下列代码:

 public static Test suite() {
            return new JUnit4TestAdapter('YourJUnit4TestClass'.class);
      }

@RunWith 注释

如果一个类自身带有@RunWith注释或者子类继承了带有@RunWith注释的父类,Junit运行这些类中的测试用例时,会引用@RunWith特定的类来代替内置的 runner。

关于@RunWith的说明:

  • Junit 默认的runner 是BlockJUnit4ClassRunner ,它取代了旧版的JUnit4ClassRunner
  • 如果给一个类添加了@RunWith(JUnit4.class)注释,那么在JUnit4版本中就会一直引用默认的runner

专业Runners

Suite : 实现测试集(由很多测试类组成)测试的标准Runner

更多详情请参照:

https://github.com/junit-team/junit4/wiki/Aggregating-tests-in-suites

http://junit.org/javadoc/latest/org/junit/runners/Suite.html

Parameterized : 实现参数化测试的标准Runner

更多详情请参照:

https://github.com/junit-team/junit4/wiki/Parameterized-tests

http://junit.org/javadoc/latest/org/junit/runners/Parameterized.html

Categories : 从给定的测试用例中,选出部分带有某些限制的用例,对其选择执行或不执行。

更多详情请参照:

https://github.com/junit-team/junit4/wiki/Categories

实验性Runners

Enclosed : 如果你在内部类(比如说Ant)里写测试用例,Ant是不会发现测试用例的。通过Enclosed运行外部类后,内部类中的测试用例才会执行。如果是为了图方便或者分享测试用例,你可以将测试用例集中放在内部类中。

更多详情请参照:

http://junit.org/javadoc/latest/org/junit/experimental/runners/Enclosed.html

https://github.com/junit-team/junit4/wiki/%27Enclosed%27-test-runner-example

第三方Runners



原文地址:https://www.cnblogs.com/insist8089/p/6387363.html