简体   繁体   中英

How to run JUnit5 tests in sequential order?

Is there a way to make tests for specific classes run in sequential order [JUnit5]?. I am using the sure-fire plugin when building the project and my tests on some classes fail, while when I run them in IntelliJ class by class they all pass. I have tried using the Order annotation, but the tests still fail which tells me that this annotation doesn't make tests run in sequential order. I want to make two of my test classes to execute their tests sequentially, while others do it in parallel, but I couldn't find proper solution on how to do that?

PS I have added a configuration in the maven sure-fire plugin that runs one JVM process, but this is not the desired behavior that I want to have.

Any help would be appreciated. Thanks in advance!

As described in the JUnit user guide , you can use the @TestMedhodOrder annotation to control the ordering of tests during runtime. For example, to use @Order annotations on the test methods, you would apply @TestMethodOrder(OrderAnnotation.class) at the test class level.

Having said that, you really, really, really should consider that as a last resort. Unless you explicitly and knowingly (fully understanding why ) wrote the tests to require a specific ordering, you will just be masking some underlying problem in either your tests or the code they are testing.

Do yourself a favor and do your best to figure out why they fail, instead of putting the band-aid of forced ordering on the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM