Execution sequence of @ test in JUnit

Source: Internet
Author: User

Original article: test execution order
Original Article Date: January 1, December 06, 2012
Translated on: February 1, October 16, 2014
Interpreter: bailima

According to the design, JUnit does not specify the execution sequence of the test method.

So far, these test methods are simply executed based on the order returned by the reflection API.

However, since the Java platform does not specify a clear sequence, it is unwise to use JVM to determine the sequence of the test method.

In fact, JDK 7 returns a random order.

Of course, writing a complete test code does not need to assume any execution order, but it is also necessary, and it is better to have a predictable error on some specific platforms than a random error.

From version 4.11, JUnit uses a deterministic but unpredictable sequence (methodsorters. Default) by default ).

If you want to change the execution sequence of test, you can simply add an annotation @ fixmethodorder on the test class and specify a suitable methodsorters.

@ Fixmethodorder (methodsorters. JVM): The execution sequence of the test method is determined based on the sequence returned by the JVM. The order of each test may be different.

@ Fixmethodorder (methodsorters. name_ascending): sort the method names alphabetically in ascending order.


Note: 1. junit4.11 and later versions are supported. 2. Using JUnit requires a hamcrest package.


Execution sequence of @ test in JUnit

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.