日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python测试框架nose研究_详解Python nose单元测试框架的安装与使用

發布時間:2023/12/19 python 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python测试框架nose研究_详解Python nose单元测试框架的安装与使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文介紹了Python nose單元測試框架的安裝與使用 ,分享給大家,具體如下:

安裝(Python2下安裝)

pip install nose

原理與命名規則

Nose會自動查找源文件、目錄或者包中的測試用例,符合正則表達式(?:^|[\b_\.%s-])[Tt]est,以及TestCase的子類都會被識別并執行。

例如:我們可以將python腳本文件名以“_test”結尾或包含“_test_”,方法名以“_test”結尾。

使用方法

查看所有nose相關命令:

nosetests -h

執行并捕獲輸出:

nosetests -s

查看nose的運行信息和調試信息:

nosetests -v9

輸出xml結果報告:

nosetests --with-xunit

支持測試方法傳參:

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

'''

測試方法傳入兩個參數

參數一:channels_txt_name

參數二:check_list_txt_name

使用方法:通過“self.參數名”進行調用

'''

@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)執行示例:

nosetests --with-html-output --html-out-file=result1.html -v --with-setup-ittr

以上執行將輸出html結果報告,但是需要先安裝插件:

1)安裝:

需要下載插件,在解壓縮后在命令行中cd到該目錄下:

python setup.py install

通過命令行安裝:

pip install nosehtmloutput-2

pip install nose-html-reporting

2)在待測路徑打開cmd使用命令如下,就可以執行測試并生成測試結果html文件了:

nosetests --with-html-output --html-out-file=result1.html

1)測試腳本中引入:from nose.tools import nottest,istest;

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

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

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

測試項目

腳本示例

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)

測試執行

測試結果

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

總結

以上是生活随笔為你收集整理的python测试框架nose研究_详解Python nose单元测试框架的安装与使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。