[本文出自天外归云的博客园]

安装(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

工具nose.tools的使用

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单元测试框架的安装与使用的更多相关文章

  1. Python nose单元测试框架结合requests库进行web接口测试

    [本文出自天外归云的博客园] 之前写过一篇关于nose使用方法的博客.最近在做一元乐购产品的接口测试,结合着python的requests库可以很方便的进行web接口测试并生成测试结果.接口测试脚本示 ...

  2. Python 单元测试框架系列:聊聊 Python 的单元测试框架(一):unittest

    作者:HelloGitHub-Prodesire HelloGitHub 的<讲解开源项目>系列,项目地址:https://github.com/HelloGitHub-Team/Arti ...

  3. python nose测试框架全面介绍七--日志相关

    引: 之前使用nose框架时,一直使用--logging-config的log文件来生成日志,具体的log配置可见之前python nose测试框架全面介绍四. 但使用一段时间后,发出一个问题,生成的 ...

  4. python nose测试框架全面介绍十---用例的跳过

    又来写nose了,这次主要介绍nose中的用例跳过应用,之前也有介绍,见python nose测试框架全面介绍四,但介绍的不详细.下面详细解析下 nose自带的SkipTest 先看看nose自带的S ...

  5. python nose测试框架全面介绍六--框架函数别名

    之前python nose测试框架全面介绍二中介绍了nose框架的基本构成,但在实际应该中我们也会到setup_function等一系列的名字,查看管网后,我们罗列下nose框架中函数的别名 1.pa ...

  6. 聊聊 Python 的单元测试框架(二):nose 和它的继任者 nose2

    作者:HelloGitHub-Prodesire HelloGitHub 的<讲解开源项目>系列,项目地址:https://github.com/HelloGitHub-Team/Arti ...

  7. Python Nose 自动化测试框架介绍

    文章目录 1. unittest 简介 1.1 python 单元测试 1.2 unittest 测试框架 1.3 默认模式 1.4 手工模式 2. nose 扩展框架 2.1 `nose` 的安装和 ...

  8. Python自动单元测试框架

    原文链接:http://www.ibm.com/developerworks/cn/linux/l-pyunit/ 软件的测试是一件非常乏味的事情,在测试别人编写的软件时尤其如此,程序员通常都只对编写 ...

  9. [转] Python自动单元测试框架

    一.软件测试 大型软件系统的开发是一个很复杂的过程,其中因为人的因素而所产生的错误非常多,因此软件在开发过程必须要有相应的质量保证活动,而软件测试则是保证质量的关键措施.正像软件熵(software ...

随机推荐

  1. 安装apr报错rm: cannot remove `libtoolT': No such file or directory

    直接打开/usr/local/src/apr-1.4.6/configure  把 $RM“$cfgfile” 那行删除掉 $RM“$cfgfile” 大约在 42302行 然后再重新运行  ./co ...

  2. jquery.inputmask 输入框input输入内容格式限制插件

    jQuery Input Mask plugin http://robinherbots.github.io/jquery.inputmask README.md jquery.inputmask C ...

  3. 【Spring】SpringMVC之拦截器

    Spring的HandlerMapping处理器支持拦截器应用.当需要为某些请求提供特殊功能时,例如实现对用户进行身份认证.登录检查等功能. 拦截器必须实现HandlerInterceptor接口,实 ...

  4. Linux日期时间显示输出

    1.输出当前年月日 echo $(date +%F) 2014-02-21 2.输出当前时间(时分) echo $(date +%R) 12:45 3.输出当前时间(时分秒) echo $(date ...

  5. ELK 5.X版本遇到的坑

    一.Kafka ->logstash ->elasticsearch    logstash 5.X以上版本不兼容5.x以下版本,因此在升级logstash时会遇到很多坑.首先是配置的变化 ...

  6. 全面拥抱移动测试,Mobile JSON Wire Protocol Specification文档翻译

    Selenium3已经宣布不支持移动化测试.对于老牌测试工具selenium来说这是以退为进,因为移动自动化测试工具的标准还在selenium团队手上. 本文轻度翻译了这个标准,看得懂的人不用翻译也能 ...

  7. iOS 优秀开源框架 开源包 开发包 from : Podfile of chatsecure ---待完善

    前段时间发现chatsecure的podfile中使用了很多非常优秀的开源包 和 大公司的sdk. 拿出来分享下. 各个类库的作用待完善. platform :ios, "7.0" ...

  8. Source Insight中代码块注释

    转载:http://blog.csdn.net/cindylx422/article/details/7560786 修改工程目录下的utils.em文件,将下面代码添加到末尾,然后将此文件添加到工程 ...

  9. notepad++添加插件管理器

    notepad++ 是一个很不错的文本编辑器,添加一些插件可以更好地使用. 首先需要使用插件管理器,最新版本的github地址是:https://github.com/bruderstein/nppp ...

  10. 看MySQL官方文档的示例SQL有感

    [背景] 周末比较闲,我这个人又没有什么爱好,当然了读书除外:前一些天我一个同事说:“你一个dba想去写一本“django”书,合适吗?” 我想也是,一个人不能忘了本,所以MySQL还是要好好的搞一搞 ...