简体   繁体   中英

Collect JUnit Test Metadata via Annotations

Is there a class/method I could implement within JUnit that is basically a "hook" that runs after each test case finishes, with user-defined (in code) metadata available?

I'm trying to accomplish something like this:

  • Annotate a test case with a user-defined annotation, such as team ownership (eg @Team(Teams.PAYMENTS) )
  • Print test result to console with 3 values: test name (test function name), status (pass/fail), and team (annotation value)

For simplicity, I just need to print this info (later it will get published elsewhere), but I'm really lost in where I could obtain this test metadata information in one place, after the test runs?

I tried to override the finish method in one of our TestRunner class:

    override fun finish(resultCode: Int, results: Bundle?) {
        System.out.println("===breakpoint here===")

        super.finish(resultCode, results)
    }

but when debugging, there was no test metadata here. Any suggestions on how I could approach this?

Thank you!

For JUnit 4, you can use a TestWatcher rule:

import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;

public class TestWatcherAPIUnitTest {

    @Rule(order = Integer.MIN_VALUE)
    public TestWatcher watchman = new TestWatcher() {
        @Override
        protected void failed(Throwable e, Description description) {
            System.out.println(description + " Team: " + getTeam(description) + " failed!");
        }

        @Override
        protected void succeeded(Description description) {
            System.out.println(description + " Team: " + getTeam(description) + " success!");
        }

        String getTeam(Description description) {
            Team team = description.getAnnotation(Team.class);
            return (team != null) ? team.value() : "unknown_team";
        }


    };

    @Test
    @Team("team1")
    public void failing_test() {
        Assert.assertTrue(false);
    }

    @Test
    @Team("team2")
    public void successful_test() {
        Assert.assertTrue(true);
    }
}

Similarly, for JUnit5 there is a TestWatcher API

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