加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

python测试工具nosetests

(2018-03-30 21:37:58)
标签:

nosetests

pythontest

分类: 编程语言学习

1、安装

同其他python第三方组件一样,你可以尽情使用easy_install或pip,甚至是setuptools(但前提是你已经安装了它们):

easy_install nose,或者

pip install nose,或者

python setup.py install

安装后,nosetests就在你的python安装目录下的Scripts目录中。然后,你就可以使用nose了,就这么简单。

pip show nose # 显示安装与否

参见   nose官方网站

下载地址参见 Python nose 1.3.7

C:\WINDOWS\system32>pip show nose
Name: nose
Version: 1.3.7
Summary: nose extends unittest to make testing easier
Home-page: http://readthedocs.org/docs/nose/
Author: Jason Pellerin
Author-email: jpellerin+nose@gmail.com
License: GNU LGPL
Location: c:\users\administrator\appdata\local\programs\python\python36\lib\site-packages
Requires:


2、使用

举例说明,比如我有一个这样的目录:

骨架内容
首先使用下述命令创建你的骨架目录:
$ mkdir projects
$ cd projects/
$ mkdir skeleton
$ cd skeleton
$ mkdir bin
$ mkdir NAME
$ mkdir tests
$ mkdir docs
我使用了一个叫 projects 的目录,用来存放我自己的各个项目。然后我在里边建立了一个叫做
skeleton 的文件夹,这就是我们新项目的基础目录。其中叫做 NAME 的文件夹是你的项目的主文件
夹,你可以将它任意取名。

接下来我们要配置一些初始文件。以下是如何在 Linxu/OSX 环境下进行配置:
~/projects/skeleton $ touch NAME/__init__.py
~/projects/skeleton $ touch tests/__init__.py

Windows PowerShell 的配置方式如下:
$ new-item -type file NAME/__init__.py
$ new-item -type file tests/__init__.py
以上命令为你创建了空的模组目录,以供你后面为其添加代码。然后我们需要建立一个 setup.py 文
件,这个文件在安装项目的时候我们会用到它:

try:
    from setuptools import setup
except ImportError:
    from distutils.core import setup
   
config = {
        'description': "Mprj",
        'author': 'Liu Xiang',
        'url': 'www.iclassmate.cn',
        'download_url': 'www.baidu.com',
        'author_emali': 'liuxiangxyd@163.com',
        'version': '0.1',
        'install_requires':['nose'],
        'packages': ['NM'],
        'scripts':[],
        'name': 'prjname'
        }

setup(**config)


./tests/name_test.py

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 calss 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)


在windows powershell运行:注意一定要在 ./tests的父目录下运行

nosetests

用 下述第4步命令用例列表。 notetest --collect-only -v

nosetest -h 查看帮助文件

工具nose.tools的使用:
1)测试脚本中引入:from nose.tools import nottest,istest;
2)不测试的方法:方法名上加修饰器@nottest;
3)指定为测试方法:方法名上加修饰器@istest(方法名无需符合命名规则);
4)查看要执行的用例列表:nosetests --collect-only -v。


       

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有