Python 各种测试框架简介(四):pytest

pytest 有时也被称为 py.test,是因为它使用的执行命令是 $ py.test。本文中我们使用 pytest 指代这个测试框架,py.test 特指运行命令。 <br /> ##较于 nose

这里没有使用像前三篇一样(简介-举例-discovery-环境)式的分段展开,是因为 pytest 与 nose 的基本用法极其相似。因此只做一个比较就好了。他俩的区别仅在于

  1. 调用测试的命令不同,pytest 用的是 $ py.test
  2. 创建测试环境(setup/teardown)的 api 不同

下面使用一个例子说明 pytest 的 setup/teardown 使用方式。

some_test.py:

lang:python
import pytest
        
@pytest.fixture(scope='function')
def setup_function(request):
    def teardown_function():
        print("teardown_function called.")
    request.addfinalizer(teardown_function)
    print('setup_function called.')

@pytest.fixture(scope='module')
def setup_module(request):
    def teardown_module():
        print("teardown_module called.")
    request.addfinalizer(teardown_module)
    print('setup_module called.')

    
def test_1(setup_function):
    print('Test_1 called.')

def test_2(setup_module):
    print('Test_2 called.')

def test_3(setup_module):
    print('Test_3 called.')

pytest 创建测试环境(fixture)的方式如上例所示,通过显式指定 scope='' 参数来选择需要使用的 pytest.fixture 装饰器。即一个 fixture 函数的类型从你定义它的时候就确定了,这与使用 @nose.with_setup() 十分不同。对于 scope='function' 的 fixture 函数,它就是会在测试用例的前后分别调用 setup/teardown。测试用例的参数如 def test_1(setup_function) 只负责引用具体的对象,它并不关心对方的作用域是函数级的还是模块级的。

有效的 scope 参数限于:'function','module','class','session',默认为 function

运行上例:$ py.test some_test.py -s-s 用于显示 print() 函数

============================= test session starts =============================
platform win32 -- Python 3.3.2 -- py-1.4.20 -- pytest-2.5.2
collected 3 items

test.py setup_function called.
Test_1 called.
.teardown_function called.
setup_module called.
Test_2 called.
.Test_3 called.
.teardown_module called.


========================== 3 passed in 0.02 seconds ===========================

这里需要注意的地方是:setup_module 被调用的位置。 <br /> ##pytest 与 nose 二选一

首先,单是从不需要使用特定类模板的角度上,nose 和 pytest 就较于 unittest 好出太多了。doctest 比较奇葩我们在这里不比。因此对于 “选一个自己喜欢的测试框架来用” 的问题,就变成了 nose 和 pytest 二选一的问题。

pythontesting.net 的作者非常喜欢 pytest,并表示

<span style="text-decoration:line-through;">pytest 赛高,不服 solo</span>

好吧,其实他说的是 “如果你挑不出 pytest 的毛病,就用这个吧”。

于是下面我们就来挑挑 pytest 的毛病:

  1. 它的 setup/teardown 语法与 unittest 的兼容性不如 nose 高,实现方式也不如 nose 直观
  2. 第一条足矣

毕竟 unittest 还是 Python 自带的单元测试框架,肯定有很多怕麻烦的人在用,所以与其语法保持一定兼容性能避免很多麻烦。即使 pytest 在命令行中有彩色输出让我很喜欢,但这还是不如第一条重要。

实际上,PyPI 中 nose 的下载量也是 pytest 的 8 倍多。

所以假如再继续写某一个框架的详解的话,大概我会选 nose 吧。

转载于:https://my.oschina.net/lionets/blog/269892

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值