美文网首页
pytest-fixture使用详解03(下)

pytest-fixture使用详解03(下)

作者: 软件开发技术修炼 | 来源:发表于2022-05-11 00:16 被阅读0次

结合上一篇,补充fixture的用法

一、什么是fixture?

fixture属于pytest中的一种方法,可以用作测试用例的前置或后置操作,通过yield关键字进行区分。
代码在yield前面的属于前置操作,代码在yield后面的属于后置操作。可以只存在一种,如果有后置,一定会执行后置的代码

分析源码:

def fixture(  # noqa: F811
    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
) -> Union[FixtureFunctionMarker, _FixtureFunction]:
    fixture_marker = FixtureFunctionMarker(
        scope=scope, params=params, autouse=autouse, ids=ids, name=name,
    )
    # Direct decoration.
    if fixture_function:
        return fixture_marker(fixture_function)
    return fixture_marker

二、fixture参数列表:

scope:可以理解成fixture的作用域,默认:function,还有class、module、package、session四个【常用】
scope = "module",可实现多个.py 跨文件共享前置;只对里面生效,放conftest对包里所有文件生效
scope = "session",可实现多个.py 跨文件使用一个session完成多个用例
autouse:默认:False,需要用例手动调用该fixture; 如果是True,所有作用域内的测试用例都会自动调用该fixture;每个函数都想调用,可采用自动:@pytest.fixture(autouse=True)
name:默认:装饰器的名称,同一模块的fixture相互调用建议写个不同的name
注意:session的作用域:是整个测试会话,即开始执行pytest到结束测试
执行顺序:session>>module>>class>>function

三、多个fixture直接可以互相调用

用例异常后继续执行后置操作
前面提到过用例出现异常后,fixture继续执行后置操作。这里安静也距离给大家说明。
将上面的代码用例中加入断言内容,保证用例3断言失败,看看是否会执行后置操作(退出登录)。

三、fixture参数详解

1、参数name
import pytest
@pytest.fixture(name='anjing')
def login():
    print('\n登录操作')
    yield
    print('\n退出登录!')
class Test_Login():
    def test_01(self, anjing):
        print('---用例01---')
    def test_02(self):
        print('---用例02---')
    def test_03(self, anjing):
        print('---用例03---')
if __name__ == '__main__':
    pytest.main(['-vs'])

执行后发现,通过参数name更改过的名字可以直接进行调用。
注意:通过name重命名后,继续使用以前的名字调用会报错。

2、参数autouse:当每个函数都想调用,可采用自动
import pytest

@pytest.fixture(autouse=True)
def login():
    print("===auto===")
class Test_Login():
    def test_o1(self):
        print("--自动登录--")
    def test_o2(self):
        print("--自动登录--")

if __name__ == '__main__':
    pytest.main(['1super.py','-vs'])
运行结果
3、params 表示fixture的参数化概念
import pytest

data = [12, 'ab']
@pytest.fixture(scope='class', params=data)
def login(request):
    print("登录")
    yield request.param
    print("登出操作")

def test_case0(login):
    print("testcase0")

class TestLogin:
    def test_case1(self, login):
        print("testcase1")
    def test_case2(self, login):
        print("testcase2")

if __name__ == '__main__':
    pytest.main(['1super.py','-vs'])
运行结果
4、ids表示在fixture对参数化的内容进行加上标识,默认是传参内容
import pytest

data = [12, 'ab']
@pytest.fixture(params=data,ids = ['ly=12','ly=ab'])
def login():
    print("登录")

def test_case0(login):
    print("testcase0")

if __name__ == '__main__':
    pytest.main(['1super.py','-vs'])
运行结果

相关文章

网友评论

      本文标题:pytest-fixture使用详解03(下)

      本文链接:https://www.haomeiwen.com/subject/eyalurtx.html