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


Posted in Python onDecember 20, 2017

本文介绍了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

工具nose.tools的使用:

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

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

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

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

测试项目

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

脚本示例

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单元测试框架的安装与使用 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
使用70行Python代码实现一个递归下降解析器的教程
Apr 17 Python
Python3实现将文件树中所有文件和子目录归档到tar压缩文件的方法
May 22 Python
Python自动化开发学习之三级菜单制作
Jul 14 Python
详解Python下ftp上传文件linux服务器
Jun 21 Python
详解Python3中的迭代器和生成器及其区别
Oct 09 Python
Python实现截取PDF文件中的几页代码实例
Mar 11 Python
python设计微型小说网站(基于Django+Bootstrap框架)
Jul 08 Python
python 生成器和迭代器的原理解析
Oct 12 Python
python 用 xlwings 库 生成图表的操作方法
Dec 22 Python
Python利用Pillow(PIL)库实现验证码图片的全过程
Oct 04 Python
python 自动刷新网页的两种方法
Apr 20 Python
解决numpy和torch数据类型转化的问题
May 23 Python
使用python实现knn算法
Dec 20 #Python
python实现kNN算法
Dec 20 #Python
解析Python中的eval()、exec()及其相关函数
Dec 20 #Python
详解Python中 sys.argv[]的用法简明解释
Dec 20 #Python
简单了解Django模板的使用
Dec 20 #Python
python机器学习之决策树分类详解
Dec 20 #Python
python机器学习之神经网络(三)
Dec 20 #Python
You might like
php更新mysql后获取影响的行数发生异常解决方法
2013/03/28 PHP
PHP图片等比例缩放生成缩略图函数分享
2014/06/10 PHP
PHP中shuffle数组值随便排序函数用法
2014/11/21 PHP
利用PHP获取汉字首字母并且分组排序详解
2017/10/22 PHP
Laravel 验证码认证学习记录小结
2019/12/20 PHP
php获取小程序码的实现代码(B类接口)
2020/06/13 PHP
如何将JS的变量值传递给ASP变量
2012/12/10 Javascript
jQuery实现长按按钮触发事件的方法
2015/02/02 Javascript
javascript日期格式化方法汇总
2015/10/04 Javascript
Spring Boot+AngularJS+BootStrap实现进度条示例代码
2017/03/02 Javascript
JavaScript求一组数的最小公倍数和最大公约数常用算法详解【面向对象,回归迭代和循环】
2018/05/07 Javascript
Vuejs学习笔记之使用指令v-model完成表单的数据双向绑定
2019/04/29 Javascript
vue使用echarts图表自适应的几种解决方案
2020/12/04 Vue.js
Python配置文件解析模块ConfigParser使用实例
2015/04/13 Python
神经网络理论基础及Python实现详解
2017/12/15 Python
python 梯度法求解函数极值的实例
2019/07/10 Python
django mysql数据库及图片上传接口详解
2019/07/18 Python
详解python内置常用高阶函数(列出了5个常用的)
2020/02/21 Python
keras 模型参数,模型保存,中间结果输出操作
2020/07/06 Python
27个经典Linux面试题及答案,你知道几个?
2013/01/10 面试题
Unix如何添加新的用户
2014/08/20 面试题
物流合作计划书
2014/01/10 职场文书
高中生期末评语
2014/01/28 职场文书
《美丽的彩虹》教学反思
2014/02/25 职场文书
财务情况说明书范文
2014/05/06 职场文书
商业计算机应用专业自荐书
2014/06/09 职场文书
2014向国旗敬礼网上签名活动总结
2014/09/27 职场文书
2014教师年度思想工作总结
2014/11/10 职场文书
齐云山导游词
2015/02/06 职场文书
2015年学校教研室主任工作总结
2015/07/20 职场文书
勤俭节约主题班会
2015/08/13 职场文书
《颐和园》教学反思
2016/02/19 职场文书
《社戏》教学反思
2016/02/22 职场文书
七年级作文之英语老师
2019/10/28 职场文书
MATLAB 全景图切割及盒图显示的实现步骤
2021/05/14 Python
Win11运行cmd提示“请求的操作需要提升”的两种解决方法
2022/07/07 数码科技