如何在Intellij中定义运行junit测试的顺序? [英] How do I define an order to run junit tests in Intellij?

查看:163
本文介绍了如何在Intellij中定义运行junit测试的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

如果我运行所有测试,它会运行坏设置然后它运行错误设置后失败的测试。它还会在两者之间运行很多无关紧要的慢速测试。但是,如果我使用一个模式只运行这两个,它运行测试失败后设置失败然后错误设置。结果,两个都通过了。

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?

推荐答案

根据 JUnit的维基


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

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.

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

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):将测试方法保留在JVM返回的
顺序中。此订单可能因运行而异。

@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):对测试方法进行排序
按方法名称,按字典顺序排列。

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

您可以使用 MethodSorters.NAME_ASCENDING 并更改您的方法名称以符合您的特定订单。我知道你只是为了调试而使用它,但它依赖于你的测试方法执行顺序 Test Smell 而且JUnit不能提供更精细的谷物控制测试方法执行顺序

You could use MethodSorters.NAME_ASCENDING and 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 Smell to rely on your test methods execution order and JUnit does not provide more finer grain control over test methods execution order

这篇关于如何在Intellij中定义运行junit测试的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆