nose 快速安装 Installation and quick start
On most UNIX-like systems, you’ll probably need to run these commands as root or using sudo.
Install nose using setuptools/distribute:
easy_install nose
Or pip:
pip install nose
Or, if you don’t have setuptools/distribute installed, use the download link at right to download the source package, and install it in the normal fashion: Ungzip and untar the source package, cd to the new directory, and:
python setup.py install
However, please note that without setuptools/distribute installed, you will not be able to use third-party nose plugins.
This will install the nose libraries, as well as the nosetests script, which you can use to automatically discover and run tests.
Now you can run tests for your project:
cd path/to/project nosetests
You should see output something like this:
.................................. ---------------------------------------------------------------------- Ran 34 tests in 1.440s OK
Indicating that nose found and ran your tests.
For help with nosetests’ many command-line options, try:
nosetests -h
PyCharm 安装 用 pip install nose 安装
(如何安装pip 请参考:https://www.cnblogs.com/tsdxdx/p/7220173.html)
pip install nose
nose 的简单介绍
1、nose 特点:
a) 自动发现测试用例(包含[Tt]est文件以及文件包中包含test的函数)
b) 以test开头的文件
c) 以test开头的函数或方法
d) 以Test开头的类
nose自动收集单元测试,会自动识别源代码文件、目录或包中的测试用例,任何符合正则表达式:(?:^|[b_.-])[Tt]est的类、函数、文件或目录,以及TestCase的子类都会被识别并执行,匹配成功的包、任何python的源文件都会被当做测试用例。
2、nose的setup和teardown:
a) package(包)中的setup、teardown,放在__init__.py文件中 —>整个测试的运行期间只运行一次
b) 模块级别:setup_module、teardown_module —>整个测试的运行期间只运行一次
c) 类级别:setup_class、teardown_class —>每个测试方法执行时都会调用
3、nose相关执行命令:
1、 nosetests –h查看所有nose相关命令
2、 nosetests –s执行并捕获输出
3、 nosetests –with-xunit输出xml结果报告
4、 nosetests -v: 查看nose的运行信息和调试信息
5、 nosetests -w 目录:指定一个目录运行测试
nose 的简单使用
1、编写测试代码
# coding = utf-8
# author:semishigure
class Testclass:
def __init__(self):
pass
def setup(self):
print 'start'
def teardown(self):
print 'stop'
def testfunc1(self):
print 'this is case1'
def testfunc2(self):
print 'this is case2'
def testfunc3(self):
print 'this is case3'
2、用cd * 跳转到 测试目录
如:cd e: (测试文件放在E盘根目录)
3.使用 nosetests 执行测试用例
nosetests -s
常用操作:
.直接运行整个包:nosetests -v test_case
.运行某一个模块:nosetests –v test_case.test_case_0002
.运行某一个用例:nosetests -v test_case.test_case_0002:test_lean_4
.运行不同模式下不同用例:
nosetests -v --tests=test_case.test_case_0002:test_lean_4,test_case.test_case_0001:test_lean_2
工具nose.tools的使用:
1)测试脚本中引入:from nose.tools import nottest,istest;
2)不测试的方法:方法名上加修饰器@nottest;
3)指定为测试方法:方法名上加修饰器@istest(方法名无需符合命名规则);
4)查看要执行的用例列表:nosetests --collect-only –v
参考链接:https://www.cnblogs.com/sunshine-blog/p/6823049.html
参考链接:http://nose.readthedocs.io/en/latest/
参考链接:https://www.cnblogs.com/semishigure/archive/2017/08/03/7268251.html