简体   繁体   English

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

[英]How can I run (or change the order of) specific test-methods in a JUnit test class?

I am fairly new to Java. 我对Java很新。 I have constructed a single JUnit test class and inside this file are a number of test methods. 我构建了一个JUnit测试类,在这个文件中有许多测试方法。 When I run this class (in NetBeans) it runs each test method in the class in order. 当我运行这个类(在NetBeans中)时,它按顺序运行类中的每个测试方法。

Question 1: How can I run only a specific sub-set of the test methods in this class? 问题1:如何在此类中仅运行测试方法的特定子集? (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?) (可能的答案:在@Test上面写@Ignore我希望忽略的测试。但是,如果我想指出我想要运行哪些测试方法而不是我想忽略的测试方法,有没有更方便的方法呢? )

Question 2: Is there an easy way to change the order in which the various test methods are run? 问题2:是否有一种简单的方法可以更改各种测试方法的运行顺序?

Thanks. 谢谢。

You should read about TestSuite 's. 您应该阅读有关TestSuite的内容。 They allow to group & order your unit test methods. 它们允许对单元测试方法进行分组和订购。 Here's an extract form this article 这是本文的摘录

"JUnit test classes can be rolled up to run in a specific order by creating a Test Suite. “通过创建测试套件,可以汇总JUnit测试类以按特定顺序运行。

EDIT: 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; 
 }

This answer tells you how to do it. 这个答案告诉你如何做到这一点。 Randomizing the order the tests run is a good idea! 随机化测试运行的顺序是个好主意!

Like the comment from dom farr states, each unit test should be able to run in isolation. 就像dom farr州的评论一样,每个单元测试都应该能够独立运行。 There should be no residuals and no given requirements after or before a test run. 在测试运行之后或之前应该没有残留物和没有给定的要求。 All your unit tests should pass run in any order, or any subset. 所有单元测试都应以任何顺序或任何子集运行。

It's not a terrible idea to have or generate a map of Test Case --> List of Test and then randomly execute all the tests. 拥有或生成测试用例 - >测试列表的地图并随后执行所有测试并不是一个糟糕的主意。

There are a number of approaches to this, but it depends on your specific needs. 有很多方法,但这取决于您的具体需求。 For example, you could split up each of your test methods into separate test classes, and then arrange them in different test suites (which would allow for overlap of methods in the suites if desired). 例如,您可以将每个测试方法拆分为单独的测试类,然后将它们安排在不同的测试套件中(如果需要,可以允许套件中的方法重叠)。 Or, a simpler solution would be to make your test methods normal class methods with one test method in your class that calls them in your specific order. 或者,更简单的解决方案是在您的类中使用一种测试方法使您的测试方法成为普通类方法,并按特定顺序调用它们。 Do you want them to be dynamcially called? 你想要它们被动态地称为?

I've not been using Java that long either, but as far as I've seen there isn't a convenient method of marking methods to execute rather than ignore. 我也没有长时间使用Java,但据我所知,没有一种方便的方法来标记执行而不是忽略的方法。 Instead I think this could be achieved using the IDE. 相反,我认为这可以使用IDE实现。 When I want to do that in Eclipse I can use the junit view to run individual tests by clicking on them. 当我想在Eclipse中执行此操作时,我可以使用junit视图通过单击它们来运行单个测试。 I imagine there is something similar in Netbeans. 我想在Netbeans中有类似的东西。

I don't know of an easy way to reorder the test execution. 我不知道重新排序测试执行的简单方法。 Eclipse has a button to rerun tests with the failing tests first, but it sounds like you want something more versatile. Eclipse有一个按钮可以首先使用失败的测试重新运行测试,但听起来你想要更通用的东西。

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

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