JUnit test Methods Guarantee Execution order

Source: Internet
Author: User
Tags sorts

Because of the need to do automated testing, it is necessary to complete unit testing. But because the execution of some tests relies on the results of another test, you want the test case to be executed in the order that you want it to be.

Then the blogger consulted the Fixmethodorder annotations, there are three ways to control the test execution sequence.

/*** Sorts the test methods by the method name, in lexicographic order, with {@linkmethod#tostring ()} used as a tiebreaker*/name_ascending (methodsorter.name_ascending),/*** Leaves The test methods in the order returned by the JVM. Note that the order from the JVM could vary from run to run*/JVM (NULL),    /*** Sorts the test methods in a deterministic, and not predictable, order*/DEFAULT (methodsorter.default);

Probably on the above three kinds, a lot of big guy's blog on these kinds of explanations and examples, Bo master here is not wordy, here are some of my doubts and found.

When using the default sort:

@FixMethodOrder (Methodsorters.default) Public classtestdemo{@Test Public voidB () {System.out.println ("B"); } @Test Public voidC () {System.out.println (C); } @Test Public voidA () {System.out.println (A); } @Test Public voidAB () {System.out.println ("AB"); } @Test Public voidAC () {System.out.println ("AC"); } @Test Public voidA1 () {System.out.println ("A1"); }}

Output

Abca1abac

This is only one of the many test results of the blogger, in fact, in the API described in the "but not predictable", the order of execution is predictable.

Because observed, the name of the short Total row in front, the ASCII code is always in front, so the blogger guess there may be a sequence with the method name of the string hashcode, and then add the Hashcode method output, get the result:

Method A:65 Method B:the method C:method A1:2064 method ab:2081 method AC:2082

So it can be concluded that when unit tests use the default execution order, the order of the test methods is linearly related to the hashcode size of the test method name string .

JUnit should execute all the methods that have @test annotations in a container, and then go to the JVM for one by one execution (it is my guess that the blogger didn't have time to peruse the source of JUnit). So the question is, does this series of methods work together under the same thread or multiple threads?

In fact, from the test execution order can be controlled not difficult to guess, more than one test method is serial execution, but practice is the only criterion to test the truth.

The code is not posted, interested students can write their own to see, is in the second position of the implementation of the method there let him sleep, to see if it will also block the third method.

The final result also proves the conjecture.

Bloggers now look at the relatively simple, there is time to study JUnit's underlying source code. If there is anything wrong with this essay, you are welcome to leave a message!

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.