獲取JUnit的執行結果


junit執行之后會有一個結果展示,下面就來看一下怎么獲取這些結果並將其存儲為一個對象

junit代碼如下:

package test;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class JunitDemo {

    @Before
    public void bofore() {
        System.out.println("bofore");
    }

    @After
    public void after() {
        System.out.println("after");
    }

    @Test
    public void test1() {
        System.out.println("test1");
        Assert.assertEquals(1, 2);
    }

    @Test
    public void test2() {
        System.out.println("test2");
        Assert.assertEquals(1, 1);
    }

    @Test
    public void test3() {
        System.out.println("test3");
        Integer.valueOf("aede21");
    }

}

 

首先先寫兩個類,分別是結果對象和結果中的方法執行結果對象

結果記錄類:

package test;

import java.util.List;

public class MyResultRecorder {

    String script_name;
    List<MethodInfo> list;
    Boolean result;

    public String getScript_name() {
        return script_name;
    }

    public void setScript_name(String script_name) {
        this.script_name = script_name;
    }

    public List<MethodInfo> getList() {
        return list;
    }

    public void setList(List<MethodInfo> list) {
        this.list = list;
    }

    public Boolean getResult() {
        return result;
    }

    public void setResult(Boolean result) {
        this.result = result;
    }

    @Override
    public String toString() {
        return "MyResultRecorder [script_name=" + script_name + ", list=" + list + ", result=" + result + "]";
    }
}

記錄中的方法:

package test;

public class MethodInfo {
    String method_name;
    Boolean result;
    String error_msg;

    public String getMethod_name() {
        return method_name;
    }

    public void setMethod_name(String method_name) {
        this.method_name = method_name;
    }

    public Boolean getResult() {
        return result;
    }

    public void setResult(Boolean result) {
        this.result = result;
    }

    public String getError_msg() {
        return error_msg;
    }

    public void setError_msg(String error_msg) {
        this.error_msg = error_msg;
    }

    @Override
    public String toString() {
        return "MethodInfo [method_name=" + method_name + ", result=" + result + ", error_msg=" + error_msg + "]";
    }

}

然后,需要寫一個監聽器ExecutionListener,繼承junit的RunListener,並在監聽時給對象賦值

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("執行結果 : " + result.wasSuccessful());
        System.out.println("執行時間 : " + result.getRunTime());
        System.out.println("執行數量 : " + result.getRunCount());
        System.out.println("失敗數量 : " + result.getFailureCount());
        System.out.println("忽略數量 : " + 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 {

    }
}

寫一個junit的類,做多個執行的處理

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

}

最后,寫一個執行類,看一下執行的效果

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

執行的結果如下:

--------- 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 ----------
執行結果 : false
執行時間 : 11
執行數量 : 3
失敗數量 : 2
忽略數量 : 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 ----------
執行結果 : true
執行時間 : 1
執行數量 : 1
失敗數量 : 0
忽略數量 : 0
MyResultRecorder [script_name=test.JunitDemo2, list=[MethodInfo [method_name=test2, result=true, error_msg=null]], result=true]

 

這樣就通過重寫junit的監聽,將junit的執行結果,存儲到一個對象當中


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM