In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "what are the methods of JUnit testing to control @ Test execution order". In daily operations, I believe many people have doubts about the methods of JUnit testing to control @ Test execution order. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the questions of "JUnit Test Control @ Test execution order". Next, please follow the editor to study!
Catalogue
JUnit Test controls @ Test execution order
First kind
The second (recommended)
The third kind
Junit test method ensures the order of execution
When using the default sort
JUnit Test Control @ Test execution order first @ FixMethodOrder (MethodSorters.JVM)
Execute @ Test from top to bottom
The second (recommended) @ FixMethodOrder (MethodSorters.NAME_ASCENDING)
Execute @ Test in the order of method names
The third @ FixMethodOrder (MethodSorters.DEFAULT)
Default method, unpredictable
Junit test method ensures the order of execution
Due to the need to do automated testing, so the need for relatively perfect unit testing. But because the execution of some tests depends on the results of another test, you want the written test case to be executed in the order in which you want it.
Then the blogger looked up the data and found FixMethodOrder annotations, and there are three ways to control the order of test execution.
/ * Sorts the test methods by the method name, in lexicographic order, with {@ link Method#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 may vary from run to run * / JVM (null), / * * Sorts the test methods in a deterministic, but not predictable, order * / DEFAULT (MethodSorter.DEFAULT)
It is probably the above three, many bosses' blogs have explanations and examples of these, bloggers will not talk about it here, here are some of my questions and findings.
When using the default sort
@ FixMethodOrder (MethodSorters.DEFAULT) public class testDemo {@ Test public void B () {System.out.println ("b");} @ Test public void C () {System.out.println ("c");} @ Test public void A () {System.out.println ("a");} @ Test public void AB () {System.out.println ("ab") } @ Test public void AC () {System.out.println ("ac");} @ Test public void A1 () {System.out.println ("A1");}}
Output
A
B
C
A1
Ab
Ac
This is just one of the bloggers' many test results, which are actually different from the "but not predictable" described in API, and the order of execution is predictable.
Because it is observed that the short name is always first, and the ascii code is always first, the blogger guesses that the order may be related to the hashcode of the string of the method name, so after adding the output of the hashcode method, the result is obtained:
Method ARV 65
Method BRV 66
Method CZR 67
Method A1PUR 2064
Method AB:2081
Method AC:2082
So it can be concluded that when unit tests use the default execution order, the order in which test methods are executed is linearly related to the hashcode size of the test method name string.
When Junit executes, it should store all the @ test annotated methods in a container and let jvm execute them one by one (bloggers haven't had time to carefully study the source code of Junit, which is my guess). So the question is, is this series of methods executed under the same thread or multiple threads together?
In fact, from the order in which the tests are executed, it is not difficult to guess that multiple test methods are executed sequentially, but practice is the only criterion for testing truth.
The code will not be posted, and interested students can write it for themselves to see if it will also block the third method by letting him sleep in the second-order execution method.
The final result also proves the conjecture.
At this point, the study on "what are the methods of JUnit testing to control the execution order of @ Test" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.