收藏)Junit 初步教程

2021-09-19 07:43:20 字數 2003 閱讀 4347

annotations

描述@beforeeach在方法上註解,在每個測試方法執行之前執行。

@aftereach在方法上註解,在每個測試方法執行之後執行

@beforeall該註解方法會在所有測試方法之前執行,該方法必須是靜態的。

@afterall該註解方法會在所有測試方法之後執行,該方法必須是靜態的。

@test用於將方法標記為測試方法

@displayname用於為測試類或測試方法提供任何自定義顯示名稱

@disable用於禁用或忽略測試類或方法

@nested用於建立巢狀測試類

@tag用於測試發現或過濾的標籤來標記測試方法或類

@testfactory標記一種方法是動態測試的測試工場

本圖來自ismael在slideshare分享的junit 5 vs junit 4

接下來來我們來乙個個註解詳細介紹。

如junit5 vs junit4圖中所示,@beforeall是替換junit4的@beforeclass。它用於表示在當前測試類中的所有測試之前應該執行註解的方法。

@beforeall

public void init() - repetition of ")

void addnumber(testinfo testinfo) :: repetition of ")

void addnumber(testinfo testinfo) )

public class junit5example

不能在單個測試計畫中同時包含@includetags@excludetags註解。

假設我們有3個測試,我們想在開發環境中執行所有3個測試; 但是想在生產中只執行乙個。所以我們將標記測試如下:

public class classatest

@test

@tag("development")

@tag("production")

void testcasea(testinfo testinfo) { //run in all environments

public class classbtest

@test

@tag("development")

void testcaseb(testinfo testinfo) {

public class classctest

@test

@tag("development")

void testcasec(testinfo testinfo) {

讓我們為這兩種環境建立測試計畫。

測試在生產環境中執行

@runwith(junitplatform.class)

@selectpackages("com.github.tonydeng.junit5.examples")

@includetags("production")

public class productiontests

測試在開發環境中執行

@runwith(junitplatform.class)

@selectpackages("com.github.tonydeng.junit5.examples")

@includetags("development")

public class developmenttests

收藏 ERP教程

家 中 請 客 可以嗎?訂貨意向 妻子 當然可以,來幾個人,幾點來,想吃什麼菜?丈夫 6個人,我們7點左右回來,準備些酒 烤鴨 番茄炒蛋 冷盤 蛋花 湯。你看可以嗎?商務溝通 妻子 沒問題,我會準備好的,訂單確認 妻子記錄下需要做的選單 mps計畫 具體要準備的菜 鴨 酒 番茄 雞蛋 作 油。bom...

Junit使用教程(一)

幾乎所有程式設計師都聽說過junit的大名,但不知真正懂得運用它的人有多少,我便是其中的乙個小白。知道junit是用來測試的,但卻把 寶刀 當成了 菜刀 用。為了從此不再菜鳥,特此總結整理了下junit的知識點。一 建立junit測試類 1.右擊test測試包,選擇new oher.2.在視窗中找到...

Junit使用教程(一)

幾乎所有程式設計師都聽說過junit的大名,但不知真正懂得運用它的人有多少,我便是其中的乙個小白。知道junit是用來測試的,但卻把 寶刀 當成了 菜刀 用。為了從此不再菜鳥,特此總結整理了下junit的知識點。一 建立junit測試類 1.右擊test測試包,選擇new oher.2.在視窗中找到...