python 测试框架 pytest
来源:互联网 发布:超星阅读器mac下载 编辑:程序博客网 时间:2024/05/16 08:20
pytest 有时也被称为 py.test,是因为它使用的执行命令是 $ py.test
。本文中我们使用 pytest 指代这个测试框架,py.test 特指运行命令。
较于 nose
这里没有使用像前三篇一样(简介-举例-discovery-环境)式的分段展开,是因为 pytest 与 nose 的基本用法极其相似。因此只做一个比较就好了。他俩的区别仅在于
- 调用测试的命令不同,pytest 用的是
$ py.test
- 创建测试环境(setup/teardown)的 api 不同
下面使用一个例子说明 pytest 的 setup/teardown 使用方式。
some_test.py:
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.2collected 3 itemstest.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 被调用的位置。
pytest 与 nose 二选一
首先,单是从不需要使用特定类模板的角度上,nose 和 pytest 就较于 unittest 好出太多了。doctest 比较奇葩我们在这里不比。因此对于 “选一个自己喜欢的测试框架来用” 的问题,就变成了 nose 和 pytest 二选一的问题。
pythontesting.net 的作者非常喜欢 pytest,并表示
pytest 赛高,不服 solo
好吧,其实他说的是 “如果你挑不出 pytest 的毛病,就用这个吧”。
于是下面我们就来挑挑 pytest 的毛病:
- 它的 setup/teardown 语法与 unittest 的兼容性不如 nose 高,实现方式也不如 nose 直观
- 第一条足矣
毕竟 unittest 还是 Python 自带的单元测试框架,肯定有很多怕麻烦的人在用,所以与其语法保持一定兼容性能避免很多麻烦。即使 pytest 在命令行中有彩色输出让我很喜欢,但这还是不如第一条重要。
实际上,PyPI 中 nose 的下载量也是 pytest 的 8 倍多。
所以假如再继续写某一个框架的详解的话,大概我会选 nose 吧。
- python 测试框架 pytest
- Python pytest测试
- 【Pytest】python单元测试框架pytest简介
- 12.python开源——pytest自动化测试框架
- 一个好用的python测试框架(pytest)及其CI设置
- python测试模块pytest之坑
- excle+pytest+jenkins+allure接口自动化测试框架
- Python单元测试框架Pytest——如何生成测试报告
- 用pytest做服务端自动化协议测试
- pytest框架执行用例例子
- python - pytest - Very easy sample in steps
- python里面Nose和pytest的区别
- python unittest 测试框架
- python的测试框架
- python 各种测试框架
- Python测试框架doctest
- Python测试框架--nose
- python 测试框架doctest
- docker学习笔记16:Dockerfile 指令 ADD 和 COPY介绍
- 微信小程序--你所遇见的秘密
- Android 6.0 变更
- android 抓包工具Charles破解
- tensorFlow Helloworld
- python 测试框架 pytest
- HTML常用标签(列表标签)二
- 使用git命令将代码托管到github(1)
- php通过ssh2扩展控制linux,模拟阿里云web控制台
- app的运行效果录制下来并保存成gif动画
- hibernate事务代码规范和session与本地线程绑定
- Java设计模式概述之结构型模式(装饰器模式)
- 国内外主流的三维GIS软件
- Weblogic HTTPS SSL证书制作(自己使用openssl来制作CA证书和服务器证书,自己充当CA的角色)