JUnit注釋的執行順序

2022-04-30 20:27:12 字數 1507 閱讀 2744

注釋就好像你可以在你的**中新增並且在方法或者類中應用的元標籤。junit 中的這些注釋為我們提供了測試方法的相關資訊,哪些方法將會在測試方法前後應用,哪些方法將會在所有方法前後應用,哪些方法將會在執行中被忽略。

序號注釋和描述

1@test這個注釋說明依附在 junit 的 public void 方法可以作為乙個測試案例。

2@before有些測試在執行前需要創造幾個相似的物件。在 public void 方法加該注釋是因為該方法需要在 test 方法前執行。

3@after如果你將外部資源在 before 方法中分配,那麼你需要在測試執行後釋放他們。在 public void 方法加該注釋是因為該方法需要在 test 方法後執行。

4@beforeclass在 public void 方法加該注釋是因為該方法需要在類中所有方法前執行。

5@afterclass它將會使方法在所有測試結束後執行。這個可以用來進行清理活動。

6@ignore這個注釋是用來忽略有關不需要執行的測試的。

建立乙個測試案例

package annotation;

import com.sun.xml.internal.ws.api.model.wsdl.wsdloutput;

import org.junit.*;

public class junitannotation

@test

public void testtwo()

@before

public void testbefore()

@beforeclass

public static void testbeforeclass()

@after

public void testafter()

@afterclass

public static void testafterclass()

@ignore

public void testignore()

}

執行測試案例

package annotation;

import org.junit.runner.junitcore;

import org.junit.runner.result;

import org.junit.runner.notification.failure;

public class testrunner

system.out.println(result.wassuccessful());}}

執行結果

結果分析

Junit執行順序

乙個junit4的單元測試用例執行順序為 beforeclass before test after afterclass 每乙個測試方法的呼叫順序為 before test after 示例 4 public class junittest 10 11 before 12 public void ...

junit測試方法執行順序

junit 4.11裡增加了指定測試方法執行順序的特性 測試類的執行順序可通過對測試類新增註解 fixmethodorder value 來指定,其中value 為執行順序 三種執行順序可供選擇 預設 methodsorters.default 按方法名 methodsorters.name asc...

Junit 測試執行順序

junit 單元測試中設定測試執行順序 junit中 的測試中,有些資料有依賴性,比如 註冊使用者後,才能執行編輯 操作之類的,所以需要測試用例 按照一定順序執行。乙個簡單的例項,見如下 引入了 testsuite,import junit.framework.test import junit.f...