获取所有@Tests的列表(jUnit测试)

时间:2019-04-03 13:52:09

标签: java bash maven junit

我需要获取软件包中所有测试的列表...我想使用Maven,但到目前为止,我还没有找到合适的调用来做。我已经尝试过在Java中进行反射,但是没有列出所有@Test方法。还列出了一些未注释的方法。

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);
}

任何帮助或指向正确方向的指针将不胜感激。

1 个答案:

答案 0 :(得分:0)

我能够通过使用Junit的RunListner进行跟踪

运行Junit套件或单个类时,请确保将侦听器添加到其中。

// 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);

下面是我的听众

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;
    }
}

使用此pojo保存测试套件的详细信息

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;
    }
}

现在添加监听器后-您以后可以简单地使用它

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