如何在JUnit测试类中运行(或更改顺序)特定的测试方法?

如何在JUnit测试类中运行(或更改顺序)特定的测试方法?

问题描述:

我对Java很陌生.我已经构造了一个JUnit测试类,并且在此文件中有许多测试方法.当我运行此类(在NetBeans中)时,它将按顺序运行该类中的每个测试方法.

I am fairly new to Java. I have constructed a single JUnit test class and inside this file are a number of test methods. When I run this class (in NetBeans) it runs each test method in the class in order.

问题1:如何在此类中仅运行测试方法的特定子集? (可能的答案:对于要忽略的测试,在@Test上方写@Ignore.但是,如果我想指示要运行的测试方法而不是要忽略的测试方法,是否有更方便的方法呢? )

Question 1: How can I run only a specific sub-set of the test methods in this class? (Potential answer: Write @Ignore above @Test for the tests I wish to ignore. However, if I want to indicate which test methods I want to run rather than those I want to ignore, is there a more convenient way of doing this?)

问题2:是否有一种简单的方法来更改各种测试方法的运行顺序?

Question 2: Is there an easy way to change the order in which the various test methods are run?

谢谢.

您应该阅读有关本文

You should read about TestSuite's. They allow to group & order your unit test methods. Here's an extract form this article

"JUnit测试类可以汇总 通过创建以特定顺序运行 测试套件.

"JUnit test classes can be rolled up to run in a specific order by creating a Test Suite.

这是一个显示其简单程度的示例:

Here's an example showing how simple it is:

 public static Test suite() { 
      TestSuite suite = new TestSuite("Sample Tests");

      suite.addTest(new SampleTest("testmethod3"));
      suite.addTest(new SampleTest("testmethod5"));

      return suite; 
 }