Test Execution Order

Source: Internet
Author: User

At first, JUnit did not specify the order in which the test methods were called. method is called in the order returned by the mapped API. However, it is unwise to use the JVM order because the Java platform does not prescribe any particular order, in fact JDK7 returns more or less in random order. Most of the good test code written does not assume a sequence, and a predictable failure on a particular platform is better than a random failure.

Starting with version 4.11, if you want to change the test execution order, simply add a @FixMethodOder comment.

At present, there are three kinds of more common:

@FixMethodOrder (Methodsorters.default): The default order. The hash code value of the method name determines the order of execution. Since the generation of hash codes is related to the OS, there may be a different order of execution for unused OS. On an operating system, the order of execution is constant.

@FixMethodOrder (METHODSORTERS.JVM): The order of execution is determined by the JVM. Of course the execution sequence may not be necessary with each test.

@FixMethodOrder (methodsorters.name_ascending): Determines the order of execution by the dictionary order of the method names.

[Java]View Plaincopy
  1. Import Org.junit.FixMethodOrder;
  2. Import Org.junit.Test;
  3. Import Org.junit.runners.MethodSorters;
  4. @FixMethodOrder (Methodsorters.default)
  5. @FixMethodOrder (methodsorters.name_ascending)
  6. @FixMethodOrder (METHODSORTERS.JVM)
  7. Public class Testexecuteorder {
  8. @Test
  9. public void Test03third () {
  10. System.out.println ("test03");
  11. }
  12. @Test
  13. public void Test01first () {
  14. System.out.println ("test01");
  15. }
  16. @Test
  17. public void Test02second () {
  18. System.out.println ("test02");
  19. }
  20. }

Execution results are as follows

1. Nothing is added:

test02
test01
test03

2. @FixMethodOrder (Methodsorters.default):

test02
test01
test03
3. @FixMethodOrder (methodsorters.name_ascending):

test01
test02
test03

4. @FixMethodOrder (METHODSORTERS.JVM):

test03
test01
test02

Or

test02
test01
test03

Test Execution Order

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.