Python單元測試unittest

2021-09-24 01:54:32 字數 917 閱讀 7415

乙個 testcase 類應該派生自unittest.testcase

setupclass()teardownclass()必須使用@classmethod裝飾器

assertequal一般first是預期值,second是實際值。

斷言丟擲異常

with self.assertraises(someexception)

: do_something(

)

asserttrue、assertfalseasserttrue實際上是斷言 bool(expr) 為 true 。同理,assertfalse實際上是斷言 bool(expr) 為 false 。 因此,若要斷言 expr 為 true 或 false,不要用asserttrueassertfalse

命令列執行unittest

# 執行test_module

python -m unittest test_module

# 執行testclass

python -m unittest test_module.testclass

# 執行test_method

python -m unittest test_module.testclass.test_method

Python 單元測試

一 假設我們編寫了一段程式,主要功能是完成阿拉伯數字和羅馬數字之間的轉換 在羅馬數字中,利用7個字母進行重複或者組合來表達各式各樣的數字 i 1 v 5 x 10 l 50 c 100 d 500 m 1000 還有一些關於構造羅馬數字的規則。此程式的框架如下 其中,class romanerror...

python 單元測試

assertequal a,b assertnotequal a,b 斷言值是否相等 assertis a,b assertisnot a,b 斷言是否同一物件 記憶體位址一樣 assertlistequal list1,list2 assertitemnotequal list1,list2 斷言...

Python單元測試

本文章整理自 使用python3.6編寫乙個單元測試demo,例如 對學生student類編寫乙個簡單的單元測試。1 編寫student類 usr bin env python3 coding utf 8 class student object def init self,name,score s...