Python nose單元測試框架的安裝與使用

2021-09-07 09:18:05 字數 3318 閱讀 7875

[本文出自天外歸雲的]

pip install nose
nose會自動查詢原始檔、目錄或者包中的測試用例,符合正規表示式(?:^|[\b_\.%s-])[tt]est,以及testcase的子類都會被識別並執行。

nosetests -h
執行並捕獲輸出:

nosetests -s
檢視nose的執行資訊和除錯資訊:

nosetests -v9
輸出xml結果報告:

nosetests --with-xunit
支援測試方法傳參:

pip install nose_ittr
2)指令碼中使用示例:

#

-*- coding: utf-8 -*-

import

osfrom nose.tools import

nottest,istest

from nose_ittr import

ittrmultiplier, ittr

curr_dir = os.path.dirname(os.path.abspath(__file__

))class

testcheckchannels(object):

__metaclass__ =ittrmultiplier

'''測試方法傳入兩個引數

引數一:channels_txt_name

引數二:check_list_txt_name

使用方法:通過「self.引數名」進行呼叫

'''@istest

@ittr(channels_txt_name=["

channels.txt

"],check_list_txt_name=["

check_list.txt

"])

deftest_check_channels(self):

channels_txt_path =os.path.join(curr_dir,self.channels_txt_name)

check_list_txt_path =os.path.join(curr_dir,self.check_list_txt_name)

the_channels =

with open(channels_txt_path) as channels:

for line in

channels.readlines():

line =line.strip()

if line != ''

: with open(check_list_txt_path) as check_list:

check_items =check_list.readlines()

for check_item in

check_items:

if check_item.strip() in

the_channels:

pass

elif check_item=='\n'

:

pass

else

:

print check_item

3)執行示例:

nosetests --with-html-output --html-out-file=result1.html -v --with-setup-ittr
以上執行將輸出html結果報告,但是需要先安裝外掛程式:

1)安裝:

外掛程式,在解壓縮後在命令列中cd到該目錄下:

python setup.py install
通過命令列安裝:

pip install nosehtmloutput-2pip install nose-html-reporting
2)在待測路徑開啟cmd使用命令如下,就可以執行測試並生成測試結果html檔案了:

nosetests --with-html-output --html-out-file=result1.html
工具nose.tools的使用:

1)測試指令碼中引入:from nose.tools import nottest,istest;

2)不測試的方法:方法名上加修飾器@nottest;

3)指定為測試方法:方法名上加修飾器@istest(方法名無需符合命名規則);

4)檢視要執行的用例列表:nosetests --collect-only -v。

python nose單元測試生成報告

生成html報告 1.安裝nose pip install nose 2.安裝nose htmloutput 3.構建好的 執行後在當前目錄自動生成nosetests.html檔案 4.網頁訪問該html檔案 file path nosetests.html 補充 1 主要是第二步安裝 2 裡不需要...

python單元測試之unittest框架使用總結

一 什麼是單元測試 單元測試是用來對乙個模組 乙個函式或者乙個類來進行正確性檢驗的測試工作。比如對於函式abs 我們可以編寫的測試用例為 1 輸入正數,比如1 1.2 0.99,期待返回值與輸入相同 2 輸入複數,比如 1 1.2 0.99,期待返回值與輸入相反 3 輸入0,期待返回0 4 輸入非數...

python單元測試之unittest框架

單元測試是用來對乙個模組 乙個函式或者乙個類來進行正確性檢驗的測試工作。unittest是python自帶的單元測試框架,我們可以用其來作為我們自動化測試框架的用例組織執行框架。unittest中最核心的四個概念是 test case,test suite,test runner,test fixt...