我在一个相对简单的工作流程中使用JUnit
JUnitCore runner = new JUnitCore();
m_junitResult = runner.run(m_junitRequest);
我想要实现的是一旦执行结束,就可以获得所有已执行测试的列表及其描述
答案 0 :(得分:1)
您可以使用org.junit.runner.Result
来计算成功的测试运行次数。
来自doc
结果收集并汇总运行多个测试的信息。由于预计测试会正确运行,因此仅在运行的测试计数中记录成功的测试。
如果有任何测试失败,那么您可以使用org.junit.runner.notification.Failure
来自doc
失败包含失败测试的描述以及运行时抛出的异常。在大多数情况下,描述将是单个测试。但是,如果在构造测试时遇到问题(例如,如果BeforeClass方法不是静态的),它可能描述的不是单个测试。
示例代码
// From http://www.tutorialspoint.com/
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestRunner {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(TestJunit.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.getRunCount());
}
}
答案 1 :(得分: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();