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的源文件都會被當做測試用例。
下面寫一個簡單的測試用例
Main.py
1 import nose 2 3 #nose.main() 4 5 result=nose.run() 6 7 print(result)
test1.py
1 def Testfunc(): 2 3 a = 1 4 5 b = 1 6 7 assert a == b
test2.py
1 # coding = utf-8 2 3 # author:semishigure 4 5 class Testclass: 6 7 def __init__(self): 8 9 pass 10 11 def setup(self): 12 13 print 'start' 14 15 def teardown(self): 16 17 print 'stop' 18 19 def testfunc1(self): 20 21 print 'this is case1' 22 23 def testfunc2(self): 24 25 print 'this is case2' 26 27 def testfunc3(self): 28 29 print 'this is case3'
執行結果如下: