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 -*-

importosfrom nose.tools importnottest,istestfrom nose_ittr importIttrMultiplier, ittr

curr_dir= os.path.dirname(os.path.abspath(__file__))classTestCheckChannels(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 inchannels.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 incheck_items:if check_item.strip() inthe_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

1)测试脚本中引入:from nose.tools import nottest,istest;

2)不测试的方法:方法名上加修饰器@nottest;

3)指定为测试方法:方法名上加修饰器@istest(方法名无需符合命名规则);

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

测试项目

e204e8be61fffffcd1748d246cd2f0d9.png

脚本示例

from nose.tools importnottest,istestfrom nose.tools importassert_equalclassTestClass:deftest_one(self):

x= "this"

assert 'h' inxdeftest_two(self):

x= "hello"

assert hasattr(x, 'check')

@nottestdeftest_three(self):assertTrue

@istestdefxxxxx(self):assertTrueclasstest_haha():defsetUp(self):print("============test class setup==============")defteardown(self):print("============test class teardown==============")deftest_xxx(self):print "test_xxx"assert_equal(9, 9)deftest_kkk(self):print "test_kkk"assert_equal(1, 1)

测试执行

52ed5e89f957921764d4e7095460db6c.png

测试结果

c82d2aebcb57ad8e670640681fd8612f.png

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值