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

安装(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. 树莓派进阶之路 (031) -字符问题(1) - GBK汉字编码表(转)

    转载:http://blog.sina.com.cn/s/blog_8184e033010109ug.html   基本简介 GB码,全称是GB2312-80<信息交换用汉字编码字符集基本集&g ...

  2. No package的问题解决

    更新pecl扩展 yum  install epel-release  //扩展包更新包yum  update //更新yum源

  3. 【java】详解java多线程

    目录结构: contents structure [+] 线程的创建与启动 继承Thread类创建线程类 实现Runnable接口创建线程类 使用Callable和Future创建线程 线程的生命周期 ...

  4. Git 提交更新到仓库(分布式版本控制系统)

    1.Git 文件生命周期 工作目录下的每一个文件都不外乎这两种状态:已跟踪或未跟踪. 已跟踪的文件是指那些被纳入了版本控制的文件,在上一次快照中有它们的记录,在工作一段时间后,它们的状态可能处于未修改 ...

  5. hashCode和identityHashCode 的关系

    1:首先看一下JDk API的观点 1-1:hashCode方法相关 1-2:identityHashCode()方法相关 2:此例的核心程序,对应的观点在注释中已经有所说明,请自己也动手实验一下看看 ...

  6. Linux 性能測试工具

    Linux 性能測试工具 linux performance 查看系统配置 查看CPU信息 lscpu Architecture: x86_64 CPU op-mode(s): 32-bit, 64- ...

  7. 关于Java读取mysql中date类型字段默认值'0000-00-00'的问题

    今天在做项目过程中,查询一个表中数据时总碰到这个问题:      java.sql.SQLException:Value '0000-00-00' can not be represented as ...

  8. STM32的JTAG下载模式

    SWJ:串行线JTAG配置 (Serial wire JTAG configuration)  SWJ(串行线JTAG)支持JTAG或SWD访问Cortex的调试端口. 系统复位后的默认状态是启用SW ...

  9. Android 数据分析系列一:sharedPreferences

    Android数据存储方式,包括下面四种: 1. Shared Preferences:用来存储"key-value"paires格式的数据,它是一个轻量级的键值存储机制,只可以存 ...

  10. 为什么推荐你用 Kotlin语言?

    谷歌大牛说:为什么 Kotlin 比你们用的那些垃圾语言都好 原标题:谷歌大牛说:为什么 Kotlin 比你们用的那些垃圾语言都好 编译:伯乐在线/黄小非 [伯乐在线/程序员的那些事 导读]:5月18 ...