詳解Python nose單元測(cè)試框架的安裝與使用
本文介紹了Python nose單元測(cè)試框架的安裝與使用 ,分享給大家,具體如下:
安裝(Python2下安裝)
pip install nose
原理與命名規(guī)則
Nose會(huì)自動(dòng)查找源文件、目錄或者包中的測(cè)試用例,符合正則表達(dá)式(?:^|[\b_\.%s-])[Tt]est,以及TestCase的子類都會(huì)被識(shí)別并執(zhí)行。
例如:我們可以將python腳本文件名以“_test”結(jié)尾或包含“_test_”,方法名以“_test”結(jié)尾。
使用方法
查看所有nose相關(guān)命令:
nosetests -h
執(zhí)行并捕獲輸出:
nosetests -s
查看nose的運(yùn)行信息和調(diào)試信息:
nosetests -v9
輸出xml結(jié)果報(bào)告:
nosetests --with-xunit
支持測(cè)試方法傳參:
1)安裝:需要下載插件“nose_ittr”:
pip install nose_ittr
2)腳本中使用示例:
# -*- coding: utf-8 -*- import os from 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 ''' 測(cè)試方法傳入兩個(gè)參數(shù) 參數(shù)一:channels_txt_name 參數(shù)二:check_list_txt_name 使用方法:通過“self.參數(shù)名”進(jìn)行調(diào)用 ''' @istest @ittr(channels_txt_name=["channels.txt"],check_list_txt_name=["check_list.txt"]) def test_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 != '': the_channels.append(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)執(zhí)行示例:
nosetests --with-html-output --html-out-file=result1.html -v --with-setup-ittr
以上執(zhí)行將輸出html結(jié)果報(bào)告,但是需要先安裝插件:
1)安裝:
需要下載插件,在解壓縮后在命令行中cd到該目錄下:
python setup.py install
通過命令行安裝:
pip install nosehtmloutput-2 pip install nose-html-reporting
2)在待測(cè)路徑打開cmd使用命令如下,就可以執(zhí)行測(cè)試并生成測(cè)試結(jié)果html文件了:
nosetests --with-html-output --html-out-file=result1.html
1)測(cè)試腳本中引入:from nose.tools import nottest,istest;
2)不測(cè)試的方法:方法名上加修飾器@nottest;
3)指定為測(cè)試方法:方法名上加修飾器@istest(方法名無需符合命名規(guī)則);
4)查看要執(zhí)行的用例列表:nosetests --collect-only -v。
測(cè)試項(xiàng)目
腳本示例
from nose.tools import nottest,istest from nose.tools import assert_equal class TestClass: def test_one(self): x = "this" assert 'h' in x def test_two(self): x = "hello" assert hasattr(x, 'check') @nottest def test_three(self): assert True @istest def xxxxx(self): assert True class test_haha(): def setUp(self): print("============test class setup==============") def teardown(self): print("============test class teardown==============") def test_xxx(self): print "test_xxx" assert_equal(9, 9) def test_kkk(self): print "test_kkk" assert_equal(1, 1)
測(cè)試執(zhí)行
測(cè)試結(jié)果
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python單元測(cè)試unittest實(shí)例詳解
- Python單元測(cè)試框架unittest使用方法講解
- 詳解Python的單元測(cè)試
- 在Python中進(jìn)行自動(dòng)化單元測(cè)試的教程
- Python之PyUnit單元測(cè)試實(shí)例
- Python中unittest模塊做UT(單元測(cè)試)使用實(shí)例
- Python單元測(cè)試框架unittest簡(jiǎn)明使用實(shí)例
- 利用Python中unittest實(shí)現(xiàn)簡(jiǎn)單的單元測(cè)試實(shí)例詳解
- 在Python編程過程中用單元測(cè)試法調(diào)試代碼的介紹
- Python單元測(cè)試簡(jiǎn)單示例
- Python單元測(cè)試與測(cè)試用例簡(jiǎn)析
相關(guān)文章
PyTorch并行訓(xùn)練DistributedDataParallel完整demo
這篇文章主要為大家介紹了PyTorch并行訓(xùn)練DistributedDataParallel完整demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06關(guān)于TensorFlow新舊版本函數(shù)接口變化詳解
今天小編就為大家分享一篇關(guān)于TensorFlow新舊版本函數(shù)接口變化詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-02-02