Java 选择执行 JUnit 测试的顺序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2669414/
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-13 10:35:55  来源:igfitidea点击:

Choose order to execute JUnit tests

javanetbeansjunittesting

提问by Miguel Ribeiro

I wanted to choose the order to execute the JUnit tests. I have 4 classes with several test methods in it, my goal is to execute, for instance, method Y of class A, then method X from class B, and finally method Z from class A.

我想选择执行 JUnit 测试的顺序。我有 4 个类,其中有几个测试方法,我的目标是执行,例如,A 类的方法 Y,然后是 B 类的方法 X,最后是 A 类的方法 Z。

Would you help please?

你会帮忙吗?

采纳答案by Jeff Storey

In general, you can't specify the order that separate unit tests run in (though you could specify priorities in TestNG and have a different priority for each test). However, unit tests should be able to be run in isolation, so the order of the tests should not matter. This is a bad practice. If you need the tests to be in a specific order, you should be rethinking your design. If you post specifics as to why you need the order, I'm sure we can offer suggestions.

通常,您不能指定单独的单元测试运行的顺序(尽管您可以在 TestNG 中指定优先级并为每个测试指定不同的优先级)。但是,单元测试应该能够独立运行,因此测试的顺序应该无关紧要。这是一个不好的做法。如果您需要按特定顺序进行测试,则应该重新考虑您的设计。如果您详细说明为什么需要订单,我相信我们可以提供建议。

回答by Yishai

The JUnit answer to that question is to create one test method like this:

JUnit 对这个问题的回答是创建一个这样的测试方法:

  @Test public void testAll() {
       classA.y();
       classB.x();
       classA.z();
  }

That is obviously an unsatisfying answer in certain cases (where setup and teardown matter), but the JUnit view of unit testing is that if tests are not independant, you are doing something wrong.

在某些情况下(设置和拆卸很重要),这显然是一个不令人满意的答案,但是单元测试的 JUnit 观点是,如果测试不是独立的,那么您就做错了。

If the above doesn't meet your needs, have a look at TestNG.

如果以上不能满足您的需求,请查看 TestNG。

回答by Adam Gent

If the previous answer is not satisfying I have noticed with the Sun JVM JUnit always seems to execute unit tests in the order of which they are defined. Obviously this is not a good idea to rely on this.

如果之前的答案不令人满意,我注意到 Sun JVM JUnit 似乎总是按照定义的顺序执行单元测试。显然,依赖于此并不是一个好主意。

回答by Kelly S. French

Create a TestSuite and call the test methods in the desired order. @Yishai is right in that JUnit is designed so each test is independent. So if you are calling test methods that canbe run independently then there should be no problem with creating a TestSuite to cover a scenario for a specific calling-order.

创建一个 TestSuite 并按所需顺序调用测试方法。@Yishai 是对的,因为 JUnit 的设计使每个测试都是独立的。因此,如果您正在调用可以独立运行的测试方法,那么创建 TestSuite 来覆盖特定调用顺序的场景应该没有问题。

回答by Geniedesalpages

This might be interesting to you: JExample

你可能会感兴趣:JExample

A different approach to testing with interdepentent tests.

使用相互依赖的测试进行测试的不同方法。

回答by nomail

The general remark/idea that testing can be done in any arbitrary order is too strong.

可以以任意顺序进行测试的一般评论/想法太强烈了。

It really depends on what you are testing.

这真的取决于你正在测试什么。

For example I am testing a server where we have a changePassword action.

例如,我正在测试一个有 changePassword 操作的服务器。

I think it is obvious that the order of tests is critical. After changePassword, the old password does not work anymore, and before, it does.

我认为很明显,测试的顺序是至关重要的。在 changePassword 之后,旧密码不再有效,而在此之前,它仍然有效。

I don't want to revert the server state after each test, too much work. I can do it one time after all tests have been completed.

我不想在每次测试后恢复服务器状态,工作量太大。我可以在所有测试完成后做一次。

回答by Lorenzo Conserva

From version 4.11 you can specify execution order using annotations and ordering by method name:

从 4.11 版开始,您可以使用注释和按方法名称排序来指定执行顺序:

import org.junit.Test;
import org.junit.FixMethodOrder;
import org.junit.runners.MethodSorters;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class MyTest {

    @Test
    public void test1Create() {
        System.out.println("first");
    }

    @Test
    public void test2Update() {
        System.out.println("second");
    }
}

See JUnit 4.11 Release Notes

请参阅 JUnit 4.11 发行说明

回答by Rittik Bhowmick

You can use @Order() annotation

您可以使用@Order() 注释