Idea下Juint 單元測試實踐

2021-08-29 18:03:43 字數 1127 閱讀 9971

單元測試用的包:junit-4.1.jar

本質上就是提供了乙個方便且功能強大的介面,省去了你寫main方法的麻煩。

package junitpractice;

public

class

calculator

public

intdivide

(int x,int y)

public

static

void

main

(string args)

}

然後寫個junit的例子

package junittest;

import junitpractice.calculator;

import org.junit.after;

import org.junit.assert;

import org.junit.before;

import org.junit.test;

import

static org.junit.assert.*;

public

class

calculatortest

@after

public

void

teardown

()throws exception

@test

public

void

add()

throws exception

@test

public

void

divide

()throws exception

}

其中@before是在測試方法前呼叫的,@after是測試方法後呼叫的,這兩個方法不管測試方法是否報錯是否發生異常,都會執行。另外,before和after是方法級的,意思是這倆方法會在所有的@test方法執行時都會執行。測試方法就是個簡單的計算,如果斷言結果是錯的,就會發生異常中斷,assert後面的方法將不會執行。

idea單元測試

參考 單元測試jar包 第一步 引入jar包 新建test資料夾 在src同級目錄下新建test資料夾,然後右擊test資料夾,選擇mark directory as test sources root 新建測試類 在test目錄下新建測試類,可以與src建立一樣的包結構 測試類 public cl...

單元測試 單元測試文章收藏

前言 前段時間公司計畫做自動化測試,自己也打算圍繞幾個點做相關調研,現在想想呢?其實對自動化測試的概念都還不是十分清晰,當時主要還是圍繞 單元測試 向qa小夥伴學習了一段時間,現由於公司重組,學習中斷,這裡簡單記錄一些單元測試好文,留待後續參考.什麼叫自動化測試?自動化測試覆蓋率?覆蓋率如何做到的?...

單元測試之Django單元測試

每個應用,自帶tests.py 整合在django的專案檔案裡,更多是開發人員寫django自動的測試執行 3.1 前後置方法執行特點 django.test.testcase類主要由前 後置處理方法和test開頭的方法組成 特點 繼承於django.test.testcase 測試用例都是test...