gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 21:38:40 26 4
gpt4 key购买 nike

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

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

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

最佳答案

根据 JUnit's 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.

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

关于java - 如何定义在 Intellij 中运行 junit 测试的顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34456739/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com