简体   繁体   中英

Invoke a method after all tests run in JUnit

I want to create custom html report for test run in JUnit. Problem I have is releasing resources and closing tags after all tests are done.

I keep one FileChannel opened for writing to report. Since it should be table with row for each test and there are hundreds of them, I don't want to open and close the channel for each test. Problem that appears here is tests organization - I have nested suites, so testRunFinished is not an option (refers to single suite, not all tests, and I saw this question ). TestWatcher also will not help me, since it refers to single test only.

Tools used: maven 3.0.5, ff webdriver, junit 4.11.

I was considering two options: 1) opening and closing channel on each test run 2) overwriting finalize() to make it close the channel

None of them seems pretty... I've searched through many pages, but nobody seems to have the same problem I have.

Any prettier solutions?

Yes, see here ( Before and After Suite execution hook in jUnit 4.x ):

@RunWith(Suite.class)
@SuiteClasses({Test1.class, Test2.class})
public class TestSuite {
    @BeforeClass
    public static void setUp() {
        System.out.println("setting up");
    }

    @AfterClass
    public static void tearDown() {
        System.out.println("tearing down");
    }
}

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