关于nose的简单用法
<span style="font-size: 14px;">以下在mac系统下操作</span>
官网:参考官网
参考英文文档:点击打开链接
1.安装nose
a.同其他第三方软件安装一样,你可以
easy_install nose
b.或者
pip install nose(在mac上这种安装完会出现nosetests命令无法使用,建议用a)
安装完成之后就可以使用nose了,下面介绍简单的用法。
def test_numbers_3_4():print 'success'assert 3*4 ==12
打开终端,进入test_nose.py所在的路径,运行nosetests -s test_nose.py,-s、-v分别表示是否打印内部的print输出信息
success
.
----------------------------------------------------------------------
Ran 1 test in 0.001sOK
-v返回结果:
test_nose.test_numbers_3_4 ... ok----------------------------------------------------------------------
Ran 1 test in 0.002sOK
无参数:
.
----------------------------------------------------------------------
Ran 1 test in 0.001sOK
b.关于nose的setup()和teardown()函数的用法
- module
- class
- function
from nose import with_setup
def my_setup_function():print 'this is my_setup_function'
def my_teardown_function():print 'this is my_teardown_function'@with_setup(my_setup_function,my_teardown_function)
def test_numbers_3_4():print 'success'assert 3*4 ==12
返回结果:
this is my_setup_function
success
this is my_teardown_function
.
----------------------------------------------------------------------
Ran 1 test in 0.001sOK
如果不喜欢使用装饰器,也可以直接赋属性值:
test_numbers_3_4.setup = my_setup_function
test_numbers_3_4.teardown = my_teardown_function
2)作用于class中的,不带@classmethod的运行于每一个test的前后,带有@classmethod的运行与开始于与结束
class TestUM:def setup(self):print ("TestUM:setup() before each test method")def teardown(self):print ("TestUM:teardown() after each test method")@classmethoddef setup_class(cls):print ("setup_class() before any methods in this class")@classmethoddef teardown_class(cls):print ("teardown_class() after any methods in this class")def test_numbers_5_6(self):print 'test_numbers_5_6() <============================ actual test code'assert 5*6 == 30def test_strings_b_2(self):print 'test_strings_b_2() <============================ actual test code'assert 'b'*2 == 'bb'
返回值:
setup_class() before any methods in this class
TestUM:setup() before each test method
test_numbers_5_6() <============================ actual test code
TestUM:teardown() after each test method
.TestUM:setup() before each test method
test_strings_b_2() <============================ actual test code
TestUM:teardown() after each test method
.teardown_class() after any methods in this class----------------------------------------------------------------------
Ran 2 tests in 0.002sOK
3)作用于module的,运行与模块的开始于结束:
from nose import with_setup
def my_setup_function():print 'this is my_setup_function'
def my_teardown_function():print 'this is my_teardown_function'
#module
def setup_module(module):print ("") # this is to get a newline after the dotsprint ("setup_module before anything in this file")
def teardown_module(module):print ("teardown_module after everything in this file")
#function
@with_setup(my_setup_function,my_teardown_function)
def test_numbers_3_4():print 'success'assert 3*4 ==12@with_setup(my_setup_function,my_teardown_function)
def test_numbers_2_3():print 'fail'assert 2+3 == 5# test_numbers_3_4.setup = my_setup_function
# test_numbers_3_4.teardown = my_teardown_function#class
class TestUM:def setup(self):print ("TestUM:setup() before each test method")def teardown(self):print ("TestUM:teardown() after each test method")@classmethoddef setup_class(cls):print ("setup_class() before any methods in this class")@classmethoddef teardown_class(cls):print ("teardown_class() after any methods in this class")def test_numbers_5_6(self):print 'test_numbers_5_6() <============================ actual test code'assert 5*6 == 30def test_strings_b_2(self):print 'test_strings_b_2() <============================ actual test code'assert 'b'*2 == 'bb'
返回结果:
setup_module before anything in this file
setup_class() before any methods in this class
TestUM:setup() before each test method
test_numbers_5_6() <============================ actual test code
TestUM:teardown() after each test method
.TestUM:setup() before each test method
test_strings_b_2() <============================ actual test code
TestUM:teardown() after each test method
.teardown_class() after any methods in this class
this is my_setup_function
success
this is my_teardown_function
.this is my_setup_function
fail
this is my_teardown_function
.teardown_module after everything in this file----------------------------------------------------------------------
Ran 4 tests in 0.001sOK
3.nose 可以直接运行unittest的测试用例
关于nose的简单用法相关推荐
- 反编译工具jad简单用法
反编译工具jad简单用法 下载地址: [url]http://58.251.57.206/down1?cid=B99584EFA6154A13E5C0B273C3876BD4CC8CE672& ...
- QCustomPlot的简单用法总结
QCustomPlot的简单用法总结 第一部分:QCustomPlot的下载与安装 第二部分:QCustomPlot在VS2013+QT下的使用 QCustomPlot的简单用法总结 写在前面, ...
- python matplotlib 简单用法
python matplotlib 简单用法 具体内容请参考官网 代码 import matplotlib.pyplot as plt import numpy as np # 支持中文 plt.rc ...
- Windump网络命令的简单用法
Windump网络命令的简单用法 大家都知道,unix系统下有个tcpdump的抓包工具,非常好用,是做troubleshooting的好帮手.其实在windows下也有一个类似的工作,叫windum ...
- Android TabLayout(选项卡布局)简单用法实例分析
本文实例讲述了Android TabLayout(选项卡布局)简单用法.分享给大家供大家参考,具体如下: 我们在应用viewpager的时候,经常会使用TabPageIndicator来与其配合.达到 ...
- shell expect的简单用法
为什么需要expect? 我们通过Shell可以实现简单的控制流功能,如:循环.判断等.但是对于需要交互的场合则必须通过人工来干预,有时候我们可能会需要实现和交互程序如 telnet服务器等进 ...
- Shellz中awk的简单用法
其实shell脚本的功能常常被低估.在实际应用中awk sed 等用法可以为shell提供更为强大的功能.下面我们将一下awk调用的简单方法进行了总结.方便同学们学习: awk的简单用法: 第一种调用 ...
- python装饰器实例-Python装饰器原理与简单用法实例分析
本文实例讲述了Python装饰器原理与简单用法.分享给大家供大家参考,具体如下: 今天整理装饰器,内嵌的装饰器.让装饰器带参数等多种形式,非常复杂,让人头疼不已.但是突然间发现了装饰器的奥秘,原来如此 ...
- python装饰器实例-Python装饰器简单用法实例小结
本文总结分析了Python装饰器简单用法.分享给大家供大家参考,具体如下: 装饰器在python中扮演着很重要的作用,例如插入日志等,装饰器可以为添加额外的功能同时又不影响业务函数的功能. 比如,运行 ...
最新文章
- 【CV】深度学习中Epoch, Batch, Iteration的含义
- 结构体指针和数组理解
- iOS之WebView的使用总结
- BaseExecutor.query()-清空本地缓存
- redis持久化策略梳理及主从环境下的策略调整记录
- Codeforces Round #530 (Div. 1) 1098A Sum in the tree
- NPOI 菜鸟实践行之根据指定的模板生成Excel 2003格式的文件 (一)
- 科普 | 以链接为中心的系统:Link-based Systems
- python--文件
- HuggingFace学习3:加载预训练模型完成机器翻译(中译英)任务
- python语言程序设计是干什么的-Python语言程序设计
- libiconv android,iconv库 android ndk可运行
- ASP.NET页面间的传值方法(2)
- 苹果和亚马逊因疑似侵犯云端同步功能专利而被提起美国337调查
- 一键实现视频二维码分享功能,So easy!
- 抖音OAuth2.0快速登录源码
- 阅读《Android 从入门到精通》(10)——单项选择
- 农民工如何拥有500多家加盟连锁店,看他是怎样做到的?
- jquery 实现购物车的商品总数及价格计算
- mongodb更新文档