1. fixture优势
相对于setup和teardown的优势:
- 命名方式灵活,不局限于setup和teardown
- conftest.py配置里可以实现数据共享,不需要import就可以自动找到一些配置
- scope='module'可以实现多个.py跨文件共享前置
- scope='session'以实现多个.py跨文件使用一个session来实现多个用例
2. fixture的使用
fixture(
fixture_function: Optional[_FixtureFunction] = None,
*,
scope: "Union[_Scope, Callable[[str, Config], _Scope]]" = "function",
params: Optional[Iterable[object]] = None,
autouse: bool = False,
ids: Optional[Union[Iterable[Union[None, str, float, int, bool]],Callable[[Any], Optional[object]],]] = None,
name: Optional[str] = None,
)
Decorator to mark a fixture factory function.
可以使用此装饰器(带或不带参数)来定义fixture功能
This decorator can be used, with or without parameters, to define a
fixture function.
fixture的功能名称,可以在以后使用,引用它会在运行测试之前调用它
The name of the fixture function can later be referenced to cause its
invocation(调用) ahead of running tests: test modules or classes can use the
``pytest.mark.usefixtures(fixturename)`` marker.
测试功能可以直接使用fixture名称作为输入参数,在这种情况下,fixture实例从fixture返回功能将被注入
Test functions can directly use fixture names as input arguments in which
case the fixture instance returned from the fixture function will be
injected(注射).
Fixtures can provide their values to test functions using ``return`` or
``yield`` statements. When using ``yield`` the code block after the
``yield`` statement is executed as teardown code regardless of the test
outcome, and must yield exactly once.
:param scope:四个级别参数
The scope for which this fixture is shared; one of ``"function"``
(default), ``"class"``, ``"module"``, ``"package"`` or ``"session"``.
This parameter may also be a callable which receives ``(fixture_name, config)``
as parameters, and must return a ``str`` with one of the values mentioned above.
See :ref:`dynamic scope` in the docs for more information.
:param params:
An optional list of parameters which will cause multiple invocations(调用)
of the fixture function and all of the tests using it. The current
parameter is available in ``request.param``.
:param autouse:
If True, the fixture func is activated for all tests that can see it.
If False (the default), an explicit(显式) reference is needed to activate
the fixture.
:param ids:
List of string ids each corresponding(相应的) to the params so that they are
part of the test id. If no ids are provided they will be generated
automatically from the params.
:param name:fixture的名称,默认装饰函数的名称
The name of the fixture. This defaults to the name of the decorated
function. If a fixture is used in the same module in which it is
defined, the function name of the fixture will be shadowed by the
function arg that requests the fixture; one way to resolve this is to
name the decorated function ``fixture_<fixturename>`` and then use
``@pytest.fixture(name='<fixturename>')``.
3. 示例
通过scope参数控制setup级别
# -*- coding:utf-8 -*-
"""
@author:百草Lily
@file:test_1.py
@time:2021/6/13
"""
# 实现场景:用例1需要先登录,用例2不需要登录,用例3需要先登录
import pytest
@pytest.fixture() # 不带参数,scope=function默认,即对函数有效
def login():
print("login fist!")
def test_1(login):
print("test 1:先登录")
def test_2():
print("test 2:不需要登录")
def test_3(login):
print("test 3:先登录")
if __name__ =="__main__":
pytest.main(["-s", "test_1.py"])
4. conftest.py配置
多个.py
文件调用公共模块时,公共模块不能写到用例中
需要一个配置文件,单独管理一些预置的操作场景,pytest
里面漠然读取conftest.py
里面的配置
注意
-
conftest.py
配置脚本名称是固定的,不能改名称 -
conftest.py
与运行的用例要放置同一package下并且有__init__.py
文件 - 不需要
import
导入conftest.py
,pytest
会自动查找
不然无法找到对应的fixture
conftest.py
# -*- coding:utf-8 -*-
"""
@author:百草Lily
@file:conftest.py
@time:2021/6/15
"""
import pytest
@pytest.fixture()
def login():
print("fixture: login")
test_1.py
# -*- coding:utf-8 -*-
"""
@author:百草Lily
@file:test_1.py
@time:2021/6/13
"""
import pytest
def test_1(login):
print("test 1:先登录")
def test_2():
print("test 2:不需要登录")
def test_3(login):
print("test 3:先登录")
if __name__ == "__main__":
pytest.main(["-s", "test_1.py"])
fixture之 yield实现teardown
fixture中使用yield唤起teardown操作
5. scope='module'
- 参数 fixture参数scope='module',module作用是整个.py文件都会生效,用例调用时,参数写上函数名就行
上述示例,conftest.py
修改,添加传参scope='module'
后执行结果如下:
注:它只会在第一个用例前执行一次
如修改,test_1(),test_3()不调用,仅test_2(login)调用
6. yield执行teardown
# -*- coding:utf-8 -*-
"""
@author:百草Lily
@file:conftest.py
@time:2021/6/15
"""
import pytest
@pytest.fixture(scope="module")
def login():
print("fixture: login")
yield # yield唤起teardown
print("yield: log out!")
执行结果如下:
7. yeild遇到异常
#修改test_2(),抛出异常
def test_2():
print("test 2:不需要登录")
raise NameError
- 用例异常,则不影响yield后面的teardown内容;运行结果互不影响,并且在用例全部执行完之后,会唤起teardown的内容(如上)
-
若setup时异常,则不会执行yield后面的teardown内容
- yield也可以配合with语句使用
7. addfinalizer终结函数
除了yield可以实现teardown,在request-context对象中注册addfinalizer方法也可以实现终结函数
addfinalizer VS yield
- addfinalizer可以注册多个终结函数
- addfinalizer中终结方法都会被执行,无论setup 部分是否报错。这个方法对于正确关闭所有的fixture创建的资源非常便利,即使其一在创建或获取时失败
参考1:https://mp.weixin.qq.com/s/OSVEhsMIzLBY04Nz-sbrPg
参考2:https://mp.weixin.qq.com/s/GJLIYRcd_CG0BsfdMlC9wQ