獲取Junit的執(zhí)行結(jié)果
			作者:
J_Rabbit 發(fā)布時(shí)間:
[ 2017/4/19 15:12:52 ] 推薦標(biāo)簽:
單元測(cè)試 Junit  
			
	  然后,需要寫(xiě)一個(gè)監(jiān)聽(tīng)器ExecutionListener,繼承junit的RunListener,并在監(jiān)聽(tīng)時(shí)給對(duì)象賦值
	package test;
	import java.util.ArrayList;
	import java.util.List;
	import org.junit.runner.Description;
	import org.junit.runner.Result;
	import org.junit.runner.notification.Failure;
	import org.junit.runner.notification.RunListener;
	public class ExecutionListener extends RunListener {
	MyResultRecorder recorder;
	MethodInfo methodInfo;
	List<MethodInfo> list;
	public ExecutionListener() {
	this.list = new ArrayList<>();
	}
	public void testRunStarted(Description description) throws Exception {
	System.out.println("--------- START ----------");
	recorder = new MyResultRecorder();
	}
	public void testRunFinished(Result result) throws Exception {
	recorder.setResult(result.wasSuccessful());
	recorder.setList(list);
	System.out.println("--------- END ----------");
	System.out.println("執(zhí)行結(jié)果 : " + result.wasSuccessful());
	System.out.println("執(zhí)行時(shí)間 : " + result.getRunTime());
	System.out.println("執(zhí)行數(shù)量 : " + result.getRunCount());
	System.out.println("失敗數(shù)量 : " + result.getFailureCount());
	System.out.println("忽略數(shù)量 : " + result.getIgnoreCount());
	}
	public void testStarted(Description description) throws Exception {
	recorder.setScript_name(description.getClassName());
	System.out.println(description.getMethodName() + " begin");
	methodInfo = new MethodInfo();
	methodInfo.setMethod_name(description.getMethodName());
	}
	public void testFinished(Description description) throws Exception {
	System.out.println(description.getMethodName() + " end");
	if (methodInfo.getError_msg() == null)
	methodInfo.setResult(true);
	list.add(methodInfo);
	}
	public void testFailure(Failure failure) throws Exception {
	System.out.println("Execution of test case failed : " + failure.getMessage());
	methodInfo.setResult(false);
	methodInfo.setError_msg(failure.getMessage());
	}
	public void testIgnored(Description description) throws Exception {
	}
	}
	  寫(xiě)一個(gè)junit的類,做多個(gè)執(zhí)行的處理
	package test;
	import org.junit.After;
	import org.junit.Assert;
	import org.junit.Before;
	import org.junit.Test;
	public class JunitDemo2 {
	@Before
	public void bofore() {
	System.out.println("bofore");
	}
	@After
	public void after() {
	System.out.println("after");
	}
	@Test
	public void test2() {
	System.out.println("test2");
	Assert.assertEquals(1, 1);
	}
	}
	  后,寫(xiě)一個(gè)執(zhí)行類,看一下執(zhí)行的效果
	package test;
	import org.junit.runner.JUnitCore;
	public class Execute {
	public static void main(String[] args) {
	run(JunitDemo.class, JunitDemo2.class);
	}
	private static void run(Class<?>... classes) {
	for (Class<?> clazz : classes) {
	JUnitCore runner = new JUnitCore();
	ExecutionListener listener = new ExecutionListener();
	runner.addListener(listener);
	runner.run(clazz);
	MyResultRecorder recorder = listener.recorder;
	System.out.println(recorder);
	}
	}
	}
	  執(zhí)行的結(jié)果如下:
	--------- START ----------
	test1 begin
	bofore
	test1
	after
	Execution of test case failed : expected:<1> but was:<2>
	test1 end
	test2 begin
	bofore
	test2
	after
	test2 end
	test3 begin
	bofore
	test3
	after
	Execution of test case failed : For input string: "aede21"
	test3 end
	--------- END ----------
	  執(zhí)行結(jié)果 : false
	  執(zhí)行時(shí)間 : 11
	  執(zhí)行數(shù)量 : 3
	  失敗數(shù)量 : 2
	  忽略數(shù)量 : 0
	  MyResultRecorder [script_name=test.JunitDemo, list=[MethodInfo [method_name=test1, result=false, error_msg=expected:<1> but was:<2>], MethodInfo [method_name=test2, result=true, error_msg=null], MethodInfo [method_name=test3, result=false, error_msg=For input string: "aede21"]], result=false]
	  --------- START ----------
	  test2 begin
	  bofore
	  test2
	  after
	  test2 end
	  --------- END ----------
	  執(zhí)行結(jié)果 : true
	  執(zhí)行時(shí)間 : 1
	  執(zhí)行數(shù)量 : 1
	  失敗數(shù)量 : 0
	  忽略數(shù)量 : 0
	  MyResultRecorder [script_name=test.JunitDemo2, list=[MethodInfo [method_name=test2, result=true, error_msg=null]], result=true]
	  這樣通過(guò)重寫(xiě)junit的監(jiān)聽(tīng),將junit的執(zhí)行結(jié)果,存儲(chǔ)到一個(gè)對(duì)象當(dāng)中