據說nose是一個比較牛逼的單元測試框架,今天打算來學習學習。
nose不是python自帶模塊,這里我才用pip的方式安裝
pip install nose
這樣就完成了安裝,然后再確認下是否安裝成功了,直接打開cmd輸入nosetests
出現這個一般就說明安裝成功了。
好了,下面是正戲:
nose相關執行命令:
1、 nosetests –h查看所有nose相關命令
2、 nosetests –s執行並捕獲輸出
3、 nosetests –with-xunit輸出xml結果報告
4、 nosetests -v: 查看nose的運行信息和調試信息
5、 nosetests -w 目錄:指定一個目錄運行測試
nose 特點:
a) 自動發現測試用例(包含[Tt]est文件以及文件包中包含test的函數)
b) 以test開頭的文件
c) 以test開頭的函數或方法
d) 以Test開頭的類
經過研究發現,nose會自動識別[Tt]est的類、函數、文件或目錄,以及TestCase的子類,匹配成功的包、任何python的源文件都會被當做測試用例。
下面寫一個簡單的測試用例
# coding = utf-8 # author:semishigure class Testclass: def __init__(self): pass def setup(self): print 'start' def teardown(self): print 'stop' def testfunc1(self): print 'this is case1' def testfunc2(self): print 'this is case2' def testfunc3(self): print 'this is case3'
執行結果如下: