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

安装(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. Android——检测TXT文件中是否含有双字节字符

    在读取双字节字符时,主要涉及到编码的选取: public static boolean isRightfulTXT(File f) { // TODO Auto-generated method st ...

  2. mvc 模型验证及正则表达式

    ASP.NET MVC3中的Model是自验证的,这是通过.NET4的System.ComponentModel.DataAnnotations命名空间完成的. 我们要做的只是给Model类的各属性加 ...

  3. redis常用性能分析命令

    一.连接 src/redis-cli -h 10.20.137.141 -p 6379 >auth 123456789 src/redis-cli -h 10.20.137.141 -p 637 ...

  4. Python学习笔记001——Linux

    Linux文件系统采用树形目录结构,系统中一切皆文件.文件名字母区分大小写 Linux命令使用格式(终端窗口) 命令名 [选项] [参数] 命令名:在命令行输入命令. 备注:命令名字母区分大小写, 1 ...

  5. mysql主从复制配置问题

    一,基本步骤 1,创建在主从数据上都创建复制账号,权限选上super, replication slave , replication master(选上这个可以方便从库变成主库): 2,配置主库和备 ...

  6. javascript 中 split 函数分割字符串成数组

    分割字符串成数组的方法有很多,不过使用最多的还是split函数 <script language="javascript"> str="2,2,3,5,6,6 ...

  7. android 蓝牙编程重点---如何发送和接收16进制数据

    最近的android蓝牙开发项目也逐渐接近尾声,基本的功能都已经完成,只剩下界面的设计.现在真的是舒了一口气! 作为编程学习经验只有1年的菜鸟,这是我独自完成的商业性产品,而且还是涉及到与单片机蓝牙模 ...

  8. Oracle 12C -- top-n查询新特性

    Oracle 12C -- top-n查询新特性在12C中,增加了一些新的特性.可以指定返回结果集的指定数量的行.或按照百分比返回行. SQL> select count(*) from emp ...

  9. CListCtrl选中某行

    原文链接: http://blog.csdn.net/wxq1987525/article/details/7461461 1.设置CListCtrl选中行 m_list.SetItemState( ...

  10. U811.1接口EAI系列之一--通用把XML传送给EAI处理方法--PowerBuilder语言

    1.前面配置参考:http://www.cnblogs.com/spring_wang/p/3393147.html 2.pb通用调EAI方法代码如下: //===================== ...