python nose測試框架全面介紹二


二、基本使用

nosetest腳本的使用(在安裝完nose之后)

nosetests [options] [(optional) test files or directories]

我們可以使用配置文件,將需要運行的參數放入,配置文件的文件名為nose.cfg中,是標准的配置文件格式,如下:

[nosetests]
verbosity=3
with-doctest=1

有部分參數可以不使用或禁用,你可以將配置放入配置文件中,但必須在系統中建立一個環境變量,名字為 NOSE_IGNORE_CONFIG_FILES

 

除了上面的運行方式,還有以下的途徑使用nose

在python腳本中直接使用,如:

import nose
nose.main()

如果你不想使用unittest框架測試結束時這種顯示方式,可以在腳本中使用以下方式:

import nose
result = nose.run()

 

說了這么多,可能還是沒看明白,下面來幾個實例,

在nose中,測試用例可以以函數的形式來組織,下面分別來演示下,

1、以函數形式寫的測試用例

#coding:utf-8
'''
Created on 2015年6月22日
@author: 
'''
import logging

log = logging.getLogger(__name__)


def test_learn_3():
    print("test_lean_3")
    pass


def test_lean_4():
    print("test_learn_2")
    
    
def test_lean_5():
    print("test_learn_5")
def setUp():
    print "0002 test setUp"
    
    
def tearDown():
    print "0002 test teardown"
    
    
test_learn_3.teardown=test_lean_5.teardown= tearDown

運行的結果如下:

E:\workspace\nosetest_lear>nosetests -v -s test_case.test_case_0002

0002 test setUp
test_case.test_case_0002.test_learn_3 ... test_lean_3
0002 test teardown
ok
test_case.test_case_0002.test_lean_4 ... test_learn_2
ok
test_case.test_case_0002.test_lean_5 ... test_learn_5
0002 test teardown
ok
0002 test teardown

這里加了一句

test_learn_3.teardown=test_lean_5.teardown= tearDown,說明在運行完成后就執行tearDown操作

所以這里運行順序為:setUp-->test_learn_3-->tearDown-->test_learn_3-->test_learn_4-->tearDown-->tearDown

因次,在函數或者函數式的用例組織中,setUp和tearDown只會執行一次,分別是開始及結束,注意:必須要用@classmethod裝飾器標識

 

2、以類的形式組織的用例:

'''

@author: huzq
'''
class TestClass():
    
    def setUp(self):
        print "MyTestClass setup"

    def tearDown(self):
        print "MyTestClass teardown"
        
    def Testfunc1(self):
        print "this is Testfunc1"
        pass
    
    def test_func1(self):
        print "this is test_func1"
        pass 
    
    def Testfunc2(self):
        print "this is Testfunc2"
        pass 
    
    def test_func2(self):
        print "this is test_func2"
        pass 
    
    

運行結果:

E:\workspace\nosetest_lear>nosetests -v -s test_case.test_case_0003
test_case.test_case_0003.TestClass.Testfunc1 ... MyTestClass setup
this is Testfunc1
MyTestClass teardown
ok
test_case.test_case_0003.TestClass.Testfunc2 ... MyTestClass setup
this is Testfunc2
MyTestClass teardown
ok
test_case.test_case_0003.TestClass.test_func1 ... MyTestClass setup
this is test_func1
MyTestClass teardown
ok
test_case.test_case_0003.TestClass.test_func2 ... MyTestClass setup
this is test_func2
MyTestClass teardown
ok

----------------------------------------------------------------------
Ran 4 tests in 0.026s

從運行結果中可以看出,每個用例都會分別執行setUp及tearDown,但是與unittest一樣,在類中的測試用例順序變更了,由字母排序了。

                              注:在以類形式運行時,還有setUpClass及tearDownClass兩個函數功能,是運行測試時,只運行一次。

 

3、以整個包的形式運行


我們創建python package時會自動生成__init__.py文件,我們將setUp或tearDown寫入時,會在每個文件執行時都執行setUp和tearDown,如下

文件內容為:

def setUp():
    print "ALL starting..."

然后再次執行以類的文件,結果如下:

E:\workspace\nosetest_lear>nosetests -v -s test_case.test_case_0003
ALL starting...
test_case.test_case_0003.TestClass.Testfunc1 ... MyTestClass setup
this is Testfunc1
MyTestClass teardown
ok
test_case.test_case_0003.TestClass.Testfunc2 ... MyTestClass setup
this is Testfunc2
MyTestClass teardown
ok
test_case.test_case_0003.TestClass.test_func1 ... MyTestClass setup
this is test_func1
MyTestClass teardown
ok
test_case.test_case_0003.TestClass.test_func2 ... MyTestClass setup
this is test_func2
MyTestClass teardown
ok

----------------------------------------------------------------------
Ran 4 tests in 0.026s

OK

從結果中可以看出,在執行前執行了__init__.py中的setUp函數

 

 with_setup修飾器的使用,nose支持在每個用例前使用with_setup來自定義測試用例的setUp及tearDown功能,如下:

def setup_func():
    "set up test fixtures"

def teardown_func():
    "tear down test fixtures"

@with_setup(setup_func, teardown_func)
def test():
    "test ..."

也可以單獨定義setup或tearDown,寫法就必須如下:

test.setup = setup_func
test.teardown = teardown_func

 

4、多個文件時nose的執行

如下圖,建立了多個py文件

再次運行大的文件,查看運行結果:

E:\workspace\nosetest_lear>nosetests -v  test_case
test_case.test_case_0001.test_lean_2 ... ok
test_case.test_case_0002.test_learn_3 ... ok
test_case.test_case_0002.test_lean_4 ... ok
test_case.test_case_0002.test_lean_5 ... ok
test_case.test_case_0003.TestClass.Testfunc1 ... ok
test_case.test_case_0003.TestClass.Testfunc2 ... ok
test_case.test_case_0003.TestClass.test_func1 ... ok
test_case.test_case_0003.TestClass.test_func2 ... ok

----------------------------------------------------------------------
Ran 8 tests in 0.022s

OK

從結果中,可以看出,文件名是按相應的字母排序,但文件里的用例根據函數式及類式不同而不一樣。

 5、nose運行方式

如前文中提到的一樣,nose可以直接放在腳本里面運行,但不建議這樣方法,更建議的是通過命令行的方式,對以后的持續集成有好處。

直接運行整個包:

E:\workspace\nosetest_lear>nosetests -v test_case
test_case.test_case_0000.test_learn_3 ... ok
test_case.test_case_0001.test_lean_2 ... ok
test_case.test_case_0002.test_learn_3 ... ok
test_case.test_case_0002.test_lean_4 ... ok
test_case.test_case_0002.test_lean_5 ... ok
test_case.test_case_0003.TestClass.Testfunc1 ... ok
test_case.test_case_0003.TestClass.Testfunc2 ... ok
test_case.test_case_0003.TestClass.test_func1 ... ok
test_case.test_case_0003.TestClass.test_func2 ... ok

----------------------------------------------------------------------
Ran 9 tests in 0.020s

OK

 

運行某一個模塊:

E:\workspace\nosetest_lear>nosetests -v test_case.test_case_0002
test_case.test_case_0002.test_learn_3 ... ok
test_case.test_case_0002.test_lean_4 ... ok
test_case.test_case_0002.test_lean_5 ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.007s

OK

 

運行某一個用例:

E:\workspace\nosetest_lear>nosetests -v test_case.test_case_0002:test_lean_4
test_case.test_case_0002.test_lean_4 ... ok

----------------------------------------------------------------------
Ran 1 test in 0.003s

OK

運行不同模塊下不同用例:

 

E:\workspace\nosetest_lear>nosetests -v --tests=test_case.test_case_0002:test_lean_4,test_case.test_case_0001:test_lean_2
test_case.test_case_0002.test_lean_4 ... ok
test_case.test_case_0001.test_lean_2 ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.004s

OK

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM