python好用的测试库-Nose

前序:

python除了unittest,还有一款更快捷的nose,nose可以说是对unittest的一种简化吧,但是他不需要unittest那种必须有固有的格式,他只需要文件,类名,方法名等含有test就可以,unittest是需要手动来写discover函数来遍历用例的。

附github项目地址:https://github.com/nose-devs/nose 
官方文档地址:http://pythontesting.net/framework/nose/nose-introduction/ 

不错的科普文章:http://www.cnblogs.com/liaofeifight/p/5148717.html

  • Name my test modules/files starting with ‘test_’.

  • Name my test functions starting with ‘test_’.

  • Name my test classes starting with ‘Test’.

  • Name my test methods starting with ‘test_’.

  • Make sure all packages with test code have an ‘init.py’ file.

安装:

python3,pip3 install nose 在线安装未成功,去下载了安装包进行离线安装:

abc@abcs-Mac:~$ pip3 install /Users/abc/Downloads/nose-1.3.7-py3-none-any.whl 

Processing ./Downloads/nose-1.3.7-py3-none-any.whl

Installing collected packages: nose

Successfully installed nose-1.3.7

abc@abcs-Mac:~$ nosetests -s

执行case

1、命令行执行:

nosetests [文件]:

         1.nosetest test.py 执行test.py这一个文件

     2.nosetest *.py 正则执行要执行的py文件

nosetests [目录]:

如果不加目录,默认执行当前目录下的所有符合nose条件的用例,

如果加目录,则运行指定目录里面符合nose条件的用例

-v 把运行中的具体过程输出:nosetests -v

-s 把测试用例中的print输出打印到控制台,这个调试的时候还是挺方便的:nosetest -s

2、放到main函数中执行

import nose
nose.main(argv=['nosetests -s'])
# 执行相同目录的满足上述格式的py文件中的所有case

nose的执行规则:和unittest一样,会优先执行setUp,最后执行tesrDown,和函数的位置没关系

断言:nose里面常用的工具有很多函数,比如类似unittest的assertEqual

使用方法:

from nose import tools

然后看下里面有这么多的方法

assert_almost_equal
assert_almost_equals
assert_dict_contains_subset
assert_dict_equal
assert_equal
assert_equals
assert_false
assert_greater
assert_greater_equal
assert_in
assert_is
assert_is_instance
assert_is_none
assert_is_not
assert_is_not_none
assert_items_equal
assert_less
assert_less_equal
assert_list_equal
assert_multi_line_equal
assert_not_almost_equal
assert_not_almost_equals
assert_not_equal
assert_not_equals
assert_not_in
assert_not_is_instance
assert_not_regexp_matches
assert_raises
assert_raises_regexp
assert_regexp_matches
assert_sequence_equal
assert_set_equal
assert_true
assert_tuple_equal
eq_
istest
make_decorator
nontrivial
nontrivial_all
nottest
ok_
raises
set_trace
timed
trivial
trivial_all
with_setup

nose这样写case:

from nose import tools

class TestOne:

    def __init__(self):
        pass

    def setup(self):
        print("start!")

    def teardown(self):
        print("End....")

    def testfunc1(self):
        a = 1
        b = 1
        tools.ok_(a == b,"通过")
        print("case1通过")

    def testfunc2(self):
        a = 2
        b = 1
        tools.ok_(a == b,"失败")
        print("case2失败")

执行结果:

测试报告输出:

nose支持各种插件,其中比较好用的叫nose_htmloutput 
这个插件可以直接执行case执行后的结果,并形成html,通过下面的调用方式(需要安装nose_htmloutput插件)
from nose_htmloutput import HtmlOutput

nose.main(argv=['nosetests -s','--verbose', '--with-html', '--html-file

输出结果为:

这个页面可以嵌入到邮件正文,以邮件的形式发送出来;

实际应用

有同学说python的UT只有在python的工程中才能使用,但是我们做的项目并不是python写的,那这个框架有什么用呢? 
回答这个问题之前先引用一句名言“人生苦短,我用python” 
python写的快这个优点对测试来说尤其珍贵,我们用这个框架来做接口功能验证,能比较快的自定义我们的检查点; 
来个实际应用: 
一个接口http://test.sogou.com/testnose?a=100&b=hello预期的返回值是:{"result":"pass","resay":"hey"} 
可以配合requests来做接口正确性的验证:

# coding=utf-8

import nose
import requests
import json

def testCase1():
    res = requests.get("http://test.sogou.com/testnose?a=100&b=hello")
    res = res.text[1:-1]
    j_res = json.loads(res)
    nose.tools.ok_(j_res["result"] == "pass", "result is failed!!!")
    nose.tools.ok_(j_res["resay"] == "hey", "resay is failed!!!")

用nose的理由

1、nose兼容pyunit,可以自动识别继承python unittest的测试用例 
2、nose可以自动识别符合 def test***()规则的测试用例 
3、执行测试更灵活,nose的命令行支持执行单个py文件内的case、支持执行指定目录的所有case 
4、便于扩展,不同项目的case可以分别维护在各自的py文件中,由命令行统一执行 
5、持续集成的好朋友

结尾

其实我也是刚刚接触nose,官方文档特别长,尤其是插件相关的,由此可见,nose可以自定义各种功能,接下来会继续在项目中试用其它功能,如果有同学感兴趣,或者使用过,欢迎回复交流;微信号:Lily554784810



 
之前在搜狗公众号看到了不错的科普文章,附上。
https://mp.weixin.qq.com/s?__biz=MjM5ODY4ODIxOA==&mid=2653201013&idx=1&sn=aae920f92f0b507d7c64f97b9c41fba4&chksm=bd16e6c08a616fd64bb756d6ee5d877205fe4c3d64a4720e4e5f4077a1e4659a40963718af66&mpshare=1&scene=23&srcid=0309iUOOSfs4sjNgEC60Ad2X%23rd
原文地址:https://www.cnblogs.com/lily1989/p/8534823.html