python nose測試框架全面介紹四


四、內部插件介紹

1、Attrib 標記,用於篩選用例

在很多時候,用例可以分不同的等級來運行,在nose中很增加了這個功能,使用attrib將用例進行划分

有兩種方式:

ef test_big_download():
    import urllib
    # commence slowness...

test_big_download.slow = 1

在運行時,用下面方式來執行:

$ nosetests -a '!slow'

這種方式不太好用,另一種方式更簡單

from nose.plugins.attrib import attr
@attr(speed='slow')
def test_big_download():
    import urllib
    # commence slowness...

運行時該用例時,只需在運行時加入,如下:

$ nosetests -a speed=slow

在實際項目中,屬性可以有多個,在使用時:

nosetests -a priority=2,status=stable

nose屬性加表達式的用例在實際項目中運行的少,這里就不介紹,需要的可以上官方查看:https://nose.readthedocs.io/en/latest/plugins/attrib.html

 

2、Capture: 獲取測試過程中的標准輸出

該插件主要用鋪獲測試過程中的標准輸出,該參數在項目實際過程中用的比較少,用法如下:

-s, --nocapture
不捕獲

 

3、Collect:快速收集需要測試的測試用例名

該功能一般與testid插件(后面會介紹到)一起使用,主要是列出需要測試的ID及名字,使用如下:

E:\workspace\nosetest_lear\test_case>nosetests -v test_case_0001 --collect-only
test_case_0001.test_learn_1 ... ok
test_case_0001.test_lean_2 ... ok

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

OK

4、Skip:跳過測試

  在實際測試過程中,有些測試在特定情況下需要將用例跳過不執行,可以使用SkipTest,如下

from nose.plugins.skip import SkipTest
@attr(mode=1) 
def test_learn_1():
    raise SkipTest

執行時,該條用例就會跳過

E:\workspace\nosetest_lear\test_case>nosetests -v  -a "mode" test_case_0001.py
test_case.test_case_0001.test_learn_1 ... SKIP

 

5、Logcapture: 在測試過程中獲取日志

改插件使用頻率很高,在測試過程中許要定位問題都要使用日志,該模塊可以將日志按配置進行存儲及顯示,有以下幾個選項

--nologcapture
不使用log

--logging-format=FORMAT
使用自定義的格式顯示日志

--logging-datefmt=FORMAT
和上面類類似,多了日期格式
--logging-filter=FILTER 日志過濾,一般很少用,可以不關注 --logging-clear-handlers 也可以不關注 --logging-level=DEFAULT log的等級定義

比如,下面這樣一個log配置文件:

# logging.conf

[loggers]
keys=root,nose,boto

[handlers]
keys=consoleHandler,rotateFileHandler

[formatters]
keys=simpleFormatter

[formatter_simpleFormatter]
format=%(asctime)s [%(levelname)s] %(filename)s[line:%(lineno)d] %(message)s


[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleFormatter
args=(sys.stdout,)

[handler_rotateFileHandler]
class=handlers.RotatingFileHandler
level=DEBUG
formatter=simpleFormatter
args=('F:/test_log.log', 'a', 200000, 9)


[logger_root]
level=DEBUG
handlers=consoleHandler,rotateFileHandler

[logger_nose]
level=DEBUG
handlers=consoleHandler,rotateFileHandler
qualname=nose
propagate=0

[logger_boto]
level=ERROR
handlers=consoleHandler,rotateFileHandler
qualname=boto
propagate=0

在使用時只需要按以下方法使用就行:

E:\workspace\nosetest_lear\test_case>nosetests -v test_case_0001 --logging-config=logging.conf

 

6、Testid: 在輸出文件中填加testid的顯示

這個使用很簡單,如下:

% nosetests -v --with-id
#1 tests.test_a ... ok
#2 tests.test_b ... ok
#3 tests.test_c ... ok

這里的-v是可以輸出用例名

有了ID后,就可以通過id,自定義要運行的測試用例,使用如下:

% nosetests -v --with-id 2
#2 tests.test_b ... ok
% nosetests -v --with-id 2 3
#2 tests.test_b ... ok
#3 tests.test_c ... ok

運行上次運行失敗的用例:

很有用處的一個插件,只運行上次測試失敗的用例,使用方法如下:

第一次運行,加入--failed參數

% nosetests -v --failed
#1 test.test_a ... ok
#2 test.test_b ... ERROR
#3 test.test_c ... FAILED
#4 test.test_d ... ok

第二次運行時,還是--failed參數,但只運行錯誤的用例了:

% nosetests -v --failed
#2 test.test_b ... ERROR
#3 test.test_c ... FAILED

當所有的用例全部運行通過后,再次運行會運行所有的用例

 

7、Xunit: 以xunit格式輸出結果

改插件主要用於在持續集(jenkins)成中使用,在jenkis中將輸出設成“Publish JUnit test result report”,並輸入文件名,如下:

在jenkins添加構建步驟,使nosetests運行命令帶有--with-xunit參數

在輸出中選擇以junit輸入,並輸入文件名,nose中----with-xunit默認輸出的XML是nosetests.xml文件名,如果要變更,需要用到參數:--xunit-file=FILE,FILE中寫入你需要的文件名

完成后進行構建,構建結果如下:

 

update:

   在測試中實用的功能介紹

1) html報告輸出

   這里使用第三方插件進行輸出html報告,介紹及安裝見:https://pypi.python.org/pypi/nose-html-reporting

   使用方法比較簡單,nosetest --with-html --html-file=FILE

2)測試用例報告時顯示中文

使用__doc__文檔函數,來使報告顯示中文,只要在測試用例下加入對應的注釋,如下:

def test_learn_1():
    u'''測試取消'''
    raise SkipTest
    #print "test_lean_1"
    #pass
    #assert 1==2
    eq_(6, 6, msg="Wrong")

@attr(mode=2) 
def test_lean_2():
    u'''測試失敗'''
    print "test_learn_2"
    ok_(4==3,msg="xxx")
    
@attr(mode=2) 
def test_lean_3():
    u'''測試成功'''
    pass

測試運行后,結果如下:

 8、-i、-I、-e

這幾個參數在實際測試中也經常使用到

-i REGEX, --include=REGEX: 加了該參數表明測試的時候去按 REGEX正則去執行用例,不匹配的則不執行

-e REGEX, --exclude=REGEX : 不跑與正則匹配的用例

-I REGEX, --ignore-file=REGEX: 忽略文件

如:

nosetests -v -s -e test_case_0002

表示不執行test_case_0002這個文件的全部用例

 

update------------2020.5.22-----------

發現還有2個很重要的參數沒有說到:

1、-s :默認情況下,nose會在用例執行結束后將執行過程中的標准輸出打印,就是print里的東西打印出來。如果加了-s,就可以將該功能關閉

2、-v :可以看到日志的輸出。不加的話,只能看到...,加了的話,除了可以看到當前執行哪個用例,還可以輸出你在程序里的正常log


免責聲明!

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



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