Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
488 views
in Technique[技术] by (71.8m points)

java - Execute order for test suite in junit

I am having a test suite which is having the following structure

TestClass1
      - testmethod1()
      - testmethod2()
      - testmethod3()
      - testmethod4() 
TestClass2
          - testmethod11()
          - testmethod22()
          - testmethod33()
          - testmethod44()

In the above structure i want to execute the testmethod4() as the final one. ie) executed at last. There is a annotation @FixMethodOrder which executes a method in order not the testclass. Is there any mechanism to maintain order in test class and testmethod together. With the @FixMethodOrder i can execute the method by renaming the name of the test method but i can't instruct junit to execute the test class as the final one(last one).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Though quoting @Andy again -

You shouldn't care about test ordering. If it's important, you've got interdependencies between tests, so you're testing behaviour + interdependencies, not simply behaviour. Your tests should work identically when executed in any order.

But if the need be to do so, you can try out Suite

@RunWith(Suite.class)

@Suite.SuiteClasses({
        TestClass2.class,
        TestClass1.class
})
public class JunitSuiteTest {
}

where you can either specify

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TestClass1 {

    @AfterClass
    public void testMethod4() {

and then take care to name your method testMethod4 as such to be executed at the end OR you can also use @AfterClass which could soon be replaced by @AfterAll in Junit5.

Do take a look at Controlling the Order of the JUnit test by Alan Harder


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...