pytest有一些内置的light fixturee怎么查看

自动化测试框架pytest中文文档:pytest参考文档 - 在Fixture中使用yield和context - 推酷
自动化测试框架pytest中文文档:pytest参考文档 - 在Fixture中使用yield和context
使用yield代替return返回一个结果,注意是一个。
test_yield.py
# content of test_yield.py
import pytest
@pytest.yield_fixture
def passwd():
print (&\nsetup before yield&)
f = open(&/etc/passwd&)
yield f.readlines()
print (&teardown after yield&)
def test_has_lines(passwd):
print (&test called&)
assert passwd
执行结果:
]# py.test -q -s test_yield.py
setup before yield
test called
.teardown after yield
1 passed in 0.01 seconds
yield后面的代码就是teardown。下例会使用with:
test_yield2.py
# content of test_yield2.py
import pytest
@pytest.yield_fixture
def passwd():
with open(&/etc/passwd&) as f:
yield f.readlines()
def test_has_lines(passwd):
assert len(passwd) &= 1
执行结果:
# py.test -v -s test_yield2.py
============================================================================================ test session starts ============================================================================================
platform linux2 -- Python 2.7.5 -- py-1.4.23 -- pytest-2.6.1 -- /usr/bin/python
collected 1 items
test_yield2.py::test_has_lines PASSED
========================================================================================= 1 passed in 0.01 seconds ==========================================================================================
yield易于和下上文使用且不用注册回调。缺点是:只能yield一次,处理异常不够方便等。
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致推荐这篇日记的豆列
&&&&&&&&&&&&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.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 被调用的位置。 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 吧。

我要回帖

更多关于 fixture note 的文章

 

随机推荐