How to listen to junit test runs

Hello,

How can I listen to test finished events in IntelliJ? I am writing a plugin that listens to all the tests that are run.

For each test run, I need the following information:
- fully qualified test name
- test result (success, error, failure)
- test run time

I have found that I can register listeners to JunitCore, but I cannot find a way to obtain a reference to that class via the IJ API.
I also found some hints of test events in the testing framework (com.intellij.execution.testframework), but I cannot figure out how to register the listeners.

Thanks

4 comments
Comment actions Permalink

Hi,

You may try to start with com.intellij.execution.testframework.TestStatusListener. It would provide you information about status. Statistics could be retreived in some awkward way like com.intellij.execution.junit2.ui.Formatters#statisticsFor.

Thanks,
Anna

0
Comment actions Permalink

Thanks! TestStatusListener did it. However, I have two issues with it that I cannot figure out:

The test run listener notification gives me an AbstractTestProxy which is a composite representing the whole test suite tree that was run. For each test leaf (test method) I am having problems with getting the following pieces of data (please correct me if my interpretation of the semantics is wrong):

- Whether each test Passed, Failed or had Errors
AbstractTestProxy has isPassed() and isDeffect() methods. Apparently, the invariant is that the two are mutually exclusive (when isPassed is true, isDeffect is false and the other way around). However, isDefect() is true when the test either fails or has errors, so there is a bit of confusion here. How can I dissambiguate between errors and failures?

- The fully qualified name of each test run
Unfortunately the existing AbstractTestProxy.getName() just returns the test method name, not the fully qualified name.
I figured I could get a hold of this via AbstractTestProxy.getLocation(...) and then getting the PSI elements (which I really hope that they know their full name). The problem with this is that getLocation() needs the project instance. I have absolutely no idea how to get this since the TestStatusListener broadcasts to all extensions from all opened projects.

Thanks!

0
Comment actions Permalink

1. you may check com.intellij.execution.testframework.AbstractTestProxy#getMagnitude to get more information about test status.
2. Unfortunately there is indeed no way to find a project, would add an overloaded method for that thanks. Now if you works with junit - you may try to get TestInfo, see e.g. com.intellij.execution.junit2.ui.TestRenderer.
Thanks,
Anna

0
Comment actions Permalink

I wrote the following code snippet to get over the project problem. Thank you for your help.

    private Location getLocation(AbstractTestProxy test) {
        Project[] openProjects = ProjectManager.getInstance().getOpenProjects();


        for (Project openedProject : openProjects) {
            Location location = test.getLocation(openedProject, GlobalSearchScope.allScope(openedProject));


            if (location != null) {
                return location;
            }
        }


        return null;
    }

0

Please sign in to leave a comment.