简体   繁体   中英

Getting list of all @Tests (jUnit tests)

I need to get list of all the tests in my package... I want to use maven but so far I haven't found appropriate call to do it. I've tried reflection in Java but it did not list all of the @Test methods. Some of un-annotated methods were listed as well..

public static void main(String[] args) {
    Reflections reflections = new Reflections(new ConfigurationBuilder()
        .setUrls(ClasspathHelper.forPackage("com.mypackage"))
        .setScanners(new MethodAnnotationsScanner()));
    Set<Method> methods = reflections.getMethodsAnnotatedWith(Test.class);
    System.out.println(methods);
}

Any help or pointer in right direction would be much appreciated.

i was able to track it by making use of Junit's RunListner

when you run your Junit suites or individual classes make sure you add the listener to it.

// AutomationTestSuites - contains my all Junit test classes

 Class cls= AutomationTestSuites.class;


JUnitCore jUnitCore = new JUnitCore();
CustomRunListener customRunListener = new CustomRunListener();
jUnitCore.addListener(customRunListener);

Request request = Request.aClass(cls);
Result result = jUnitCore.run(request);

below is my listener

import org.junit.runner.Description;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;
import com.dto.TestSuiteDetails;
import java.util.Map;
import java.util.LinkedHashMap;
import java.util.concurrent.TimeUnit;
import java.lang.Exception;

public class CustomRunListener extends RunListener{

    private Map<String,TestSuiteDetails> map = new LinkedHashMap<>();

    private long testSuiteStartTime;
    private long testSuiteElapsedTime;
    private int ignoredCount;


    public void testRunStarted(Description description) throws Exception {
        testSuiteStartTime = System.currentTimeMillis();
    }


    public void testStarted(Description description) throws Exception {
        TestSuiteDetails testSuiteDetails = new TestSuiteDetails();
        testSuiteDetails.setTestCaseName(description.getMethodName());
        String[]  arr = description.getTestClass().getName().split("\\.");
        String name = arr[arr.length-1];
        testSuiteDetails.setTestClassName(name);
        String[]  arr1 = name.split("_");
        String testSuite = arr1[0];
        testSuiteDetails.setTestSuiteNmae(testSuite);
        testSuiteDetails.setTestStatus("Passed");
        testSuiteDetails.setStartTime(System.currentTimeMillis());
        map.put(description.getMethodName(),testSuiteDetails);
    }

    public void testFinished(Description description) throws Exception {
        TestSuiteDetails testSuiteDetails= null;
        if(map.containsKey(description.getMethodName())){
            testSuiteDetails = map.get(description.getMethodName());
            testSuiteDetails.setElaspsedTime(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()-testSuiteDetails.getStartTime()));
        }
        map.put(description.getMethodName(),testSuiteDetails);
    }

    public void testRunFinished(org.junit.runner.Result result) throws Exception {
        testSuiteElapsedTime = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()-testSuiteStartTime);
    }

    public void testFailure(Failure failure) throws Exception {
        TestSuiteDetails testSuiteDetails= null;
        if(map.containsKey(failure.getDescription().getMethodName())){
            testSuiteDetails = map.get(failure.getDescription().getMethodName());
        }else{
            testSuiteDetails = new TestSuiteDetails();
        }
        testSuiteDetails.setTestCaseName(failure.getDescription().getMethodName());
        testSuiteDetails.setTestDescription(failure.getException().toString());
        testSuiteDetails.setTestStatus("Failed");
        map.put(failure.getDescription().getMethodName(),testSuiteDetails);
    }

    public void testIgnored(Description description) throws Exception {
        TestSuiteDetails testSuiteDetails= null;
        if(map.containsKey(description.getMethodName())){
            testSuiteDetails = map.get(description.getMethodName());
        }else{
            testSuiteDetails = new TestSuiteDetails();
            testSuiteDetails.setTestCaseName(description.getMethodName());
            String[]  arr = description.getTestClass().getName().split("\\.");
            String name = arr[arr.length-1];
            testSuiteDetails.setTestClassName(name);
            String[]  arr1 = name.split("_");
            String testSuite = arr1[0];
            testSuiteDetails.setTestSuiteNmae(testSuite);
            ignoredCount++;
        }


        testSuiteDetails.setTestStatus("Ignored");
        map.put(description.getMethodName(),testSuiteDetails);
    }

    public int getIgnoredCount() {
        return ignoredCount;
    }

    public void setIgnoredCount(int ignoredCount) {
        this.ignoredCount = ignoredCount;
    }

    public Map<String, TestSuiteDetails> getMap() {
        return map;
    }

    public long getTestSuiteStartTime() {
        return testSuiteStartTime;
    }

    public long getTestSuiteElapsedTime() {
        return testSuiteElapsedTime;
    }
}

am using this pojo for holding test suite details

public class TestSuiteDetails {

    private String testClassName;
    private String testSuiteNmae;
    private String testCaseName;
    private String testStatus;
    private String testDescription;

    private long startTime;
    private long elaspsedTime;

    public String getTestClassName() {
        return testClassName;
    }

    public void setTestClassName(String testClassName) {
        this.testClassName = testClassName;
    }

    public String getTestCaseName() {
        return testCaseName;
    }

    public void setTestCaseName(String testCaseName) {
        this.testCaseName = testCaseName;
    }

    public String getTestStatus() {
        return testStatus;
    }

    public void setTestStatus(String testStatus) {
        this.testStatus = testStatus;
    }

    public String getTestDescription() {
        return testDescription;
    }

    public void setTestDescription(String testDescription) {
        this.testDescription = testDescription;
    }

    public long getStartTime() {
        return startTime;
    }

    public void setStartTime(long startTime) {
        this.startTime = startTime;
    }

    public long getElaspsedTime() {
        return elaspsedTime;
    }

    public void setElaspsedTime(long elaspsedTime) {
        this.elaspsedTime = elaspsedTime;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        TestSuiteDetails that = (TestSuiteDetails) o;

        if (testClassName != null ? !testClassName.equals(that.testClassName) : that.testClassName != null)
            return false;
        return testCaseName != null ? testCaseName.equals(that.testCaseName) : that.testCaseName == null;
    }

    @Override
    public int hashCode() {
        int result = testClassName != null ? testClassName.hashCode() : 0;
        result = 31 * result + (testCaseName != null ? testCaseName.hashCode() : 0);
        return result;
    }

    public String getTestSuiteNmae() {
        return testSuiteNmae;
    }

    public void setTestSuiteNmae(String testSuiteNmae) {
        this.testSuiteNmae = testSuiteNmae;
    }
}

now after adding the listener - you can simple utilize it later

HashMap<String, TestSuiteDetails> myTestResultMap = customRunListener.getMap();

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