Java 如何定义在 Intellij 中运行 junit 测试的顺序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34456739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:40:22  来源:igfitidea点击:

How do I define an order to run junit tests in Intellij?

javaintellij-ideajunitintegration-testing

提问by Daniel Kaplan

I have a flaky junit test that only fails if I run all my tests. I think that one test is causing another test to fail, I want to prove it before I try to fix it.

我有一个脆弱的 junit 测试,只有在我运行所有测试时才会失败。我认为一个测试导致另一个测试失败,我想在尝试修复它之前证明它。

If I run all tests, it runs the "bad setup" then it runs the "test that fails after bad setup". It also runs a lot of irrelevant, slow tests in between. But if I use a pattern to only run these two, it runs "test that fails after bad setup" then "bad setup". As a result, both pass.

如果我运行所有测试,它会运行“错误设置”,然后运行“错误设置后失败的测试”。它还在中间运行了许多不相关的、缓慢的测试。但是如果我使用一个模式只运行这两个,它会运行“错误设置后失败的测试”然后是“错误设置”。结果,双方都通过了。

How do I only run "bad setup" and "test that fails after bad setup", in that order?

我如何按顺序只运行“错误设置”和“错误设置后失败的测试”?

回答by Ali Dehghani

According to JUnit's wiki:

根据JUnit 的 wiki

By design, JUnit does not specify the execution order of test method invocations. Until now, the methods were simply invoked in the order returned by the reflection API. However, using the JVM order is unwise since the Java platform does not specify any particular order, and in fact JDK 7 returns a more or less random order. Of course, well-written test code would not assume any order, but some do, and a predictable failure is better than a random failure on certain platforms.

From version 4.11, JUnit will by default use a deterministic, but not predictable, order (MethodSorters.DEFAULT). To change the test execution order simply annotate your test class using @FixMethodOrder and specify one of the available MethodSorters:

@FixMethodOrder(MethodSorters.JVM): Leaves the test methods in the order returned by the JVM. This order may vary from run to run.

@FixMethodOrder(MethodSorters.NAME_ASCENDING): Sorts the test methods by method name, in lexicographic order.

按照设计,JUnit 不指定测试方法调用的执行顺序。到目前为止,这些方法只是按照反射 API 返回的顺序调用。然而,使用 JVM 顺序是不明智的,因为 Java 平台没有指定任何特定的顺序,实际上 JDK 7 返回或多或少的随机顺序。当然,编写良好的测试代码不会假定任何顺序,但有些会这样做,并且在某些平台上,可预测的故障比随机故障要好。

从 4.11 版开始,JUnit 将默认使用确定性但不可预测的顺序 (MethodSorters.DEFAULT)。要更改测试执行顺序,只需使用 @FixMethodOrder 注释您的测试类并指定可用的 MethodSorters 之一:

@FixMethodOrder(MethodSorters.JVM): 按照 JVM 返回的顺序保留测试方法。此顺序可能因运行而异。

@FixMethodOrder(MethodSorters.NAME_ASCENDING): 按方法名称按字典顺序对测试方法进行排序。

You could use MethodSorters.NAME_ASCENDINGand change your method names to match with your specific order. I know you're using this just for debugging sake but it's a Test Smellto rely on your test methods execution order and JUnit does not provide more finer grain control over test methods execution order

您可以使用MethodSorters.NAME_ASCENDING和更改您的方法名称以匹配您的特定订单。我知道你只是为了调试而使用它,但它是一种依赖于你的测试方法执行顺序的测试气味,而 JUnit 没有提供对测试方法执行顺序的更精细的控制

回答by pdmoore

Unit tests ought to be independent so most frameworks don't guarantee or enforce the order in which they are run. But since you want to enforce an order the easiest way I've done it in the past it to create a "throw away" test suite or test method that calls the tests in whatever order I want them to run in. Unit tests are methods, just call them. This is easy to do if you're dealing with tens of tests, not at all appealing if you're dealing with hundreds or thousands.

单元测试应该是独立的,因此大多数框架不保证或强制执行它们的运行顺序。但是由于您想以我过去做过的最简单的方式强制执行命令,它创建一个“丢弃”测试套件或测试方法,以我希望它们运行的​​任何顺序调用测试。单元测试是方法,就打电话给他们。如果您要处理数十个测试,这很容易做到,如果您要处理数百或数千个测试,则完全没有吸引力。

Try to isolate the flaky interaction as much as possible, then swap around the order of the poorly interacting tests within the throwaway calling method.

尝试尽可能地隔离不稳定的交互,然后在一次性调用方法中交换交互不良的测试的顺序。

回答by Abish R

As said by Ali Dehghani, You can order the test method execution by

正如 Ali Dehghani 所说,您可以通过以下方式订购测试方法执行

@FixMethodOrder(MethodSorters.NAME_ASCENDING): Sorts the test methods by method name, in lexicographic order.

@FixMethodOrder(MethodSorters.NAME_ASCENDING):按方法名称按字典顺序对测试方法进行排序。

Code:

代码:

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class ApplicationTest extends ActivityInstrumentationTestCase2<MainActivity> {

    public ApplicationTest() {
        super(MainActivity.class);
    }

    @Rule
    public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class);

    @Test
    void t1AttachUI(){
        // testing code goes here
    }

    @Test
    void t2InitializeViews(){
        // testing code goes here
    };

    @Test
    void t3SettingValues(){
        // testing code goes here
    };

    @Test
    void t4Validation(){
        // testing code goes here
    };

    @Test
    void t3AfterButtonPress(){
        // testing code goes here
    };
}