pytest.fixture()基础使用实例

目录

一、pytest.fixture()简介

源码:

: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  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 .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:
        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='').

二、scope参数

2.1简介特点

1、function:每一个函数或方法都可以调用,默认值
2、Class:每一个类调用一次(只限制了class,不限制.py文件),一个类中可以有多个方法
3、Module:每一个.py文件调用一次(只限制.py文件),该文件内可以有多个function和class
4、Session:是多个文件调用一次(与conftest.py配合使用),可以跨.py文件调用,每个.py文件就是module
下面会依次介绍

2.2scope=function

方法和函数上一行加入@pytest.fixture(scope=’function’)或者@pytest.fixture()
没有设置scope时,默认等于function

import pytest
@pytest.fixture()
def login_init():
    print("测试用例执行前,先执行我")
    yield
    print("测试用例执行后,在执行我")
@pytest.fixture(scope = 'function')
def login_init_2():
    print("初始化条件2")
def test_login_null_user(login_init,login_init_2):
    print("账号为空登陆")
    assert 1==1
def test_login_null_pass():
    print("密码为空登陆")
    assert 1==1
class Test_Login:
    def test_loing(self,login_init,login_init_2):
        print("使用正确的账号密码登陆")
        assert 1==1

运行结果:

pytest.fixture()基础使用实例
从运行结果可以看出方法和函数运行之前都会调用login_init和login_init_2函数,方法和函数执行后会执行login_init函数yield后的函数体
所以scope = function时,每个方法和函数都可以调用一次
login_init和login_init_2被调用时,执行结果一样,说明 pytest.fixture()默认是scope = function

2.3scope=class

每一个类调用一次(只限制了class,不限制.py文件),一个类中可以有多个方法

import pytest
@pytest.fixture(scope='class')
def login_init():
    print("测试用例执行前,先执行我")
    yield
    print("测试用例执行后,在执行我")
def test_login_null_user(login_init):
    print("账号为空登陆")
    assert 1==1
def test_login_null_pass(login_init):
    print("密码为空登陆")
    assert 1==1
class Test_Login:
    def test_loing(self,login_init):
        print("使用正确的账号密码登陆")
        assert 1==1
    def test_loing_err_user(self,login_init):
        print("使用错误的账号登陆")
        assert 1==1

执行结果

pytest.fixture()基础使用实例
从 执行结果可以看出函数调用login_init时,每执行一个用例都会执行yield前后两个函数体,但是类里的方法test_loing()和test_loing_err_user()都调用login_init却只执行一次yield前后两个函数体,而且两个方法的用例都执行完了才执行结束,所以scope=class时: 每一个类调用一次(只限制了class,不限制.py文件),一个类中可以有多个方法

2.4scope=Module

Module:每一个.py文件调用一次(只限制.py文件),该文件内可以有多个function和class

import pytest
@pytest.fixture(scope='module')
def login_init():
    print("测试用例执行前,先执行我")
    yield
    print("测试用例执行后,在执行我")
def test_login_null_user(login_init):
    print("账号为空登陆")
    assert 1==1
def test_login_null_pass(login_init):
    print("密码为空登陆")
    assert 1==1
class Test_Login:
    def test_loing(self,login_init):
        print("使用正确的账号密码登陆")
        assert 1==1
    def test_loing_err_user(self,login_init):
        print("使用错误的账号登陆")
        assert 1==1
    def test_loing_err_pass(self):
        print("使用错误的密码登陆")
        assert 1==2

执行结果:

pytest.fixture()基础使用实例
从执行结果可以看出一个.py文件只要调用scope = module装饰的方法或函数,这个文件在执行用例前,先执行scope = module装饰的方法或函数,最后执行yield结束函数体,包括 test_loing_err_pass方法 。由此看出:每一个.py文件调用一次(只限制.py文件)当.py文件有多个scope = module装饰的方法或者函数时,大家自己动手 验证下结果

2.5scope=session

与conftest.py配合使用,可以跨.py文件调用,每个.py文件就是module,只能调用一次
目录结构:

pytest.fixture()基础使用实例
conftest.py文件代码 :
import pytest
@pytest.fixture(scope='session')
def init_login_BC():
    print("我是pytest.fixture的session")

test_search.py文件代码:

class Test_Search:
    def test_search_id(self,init_login_BC):
        print("使用ID搜索用户")
        assert 1==1
    def test_search_user(self):
        print("使用昵称搜索用户")
        assert 1==1

运行结果:

pytest.fixture()基础使用实例
从运行结果可以看出scope=session, 可以夸文件调用,与conftest.py配合使用,只能调用一次大家可以自己动手尝试

三、params参数

传递参数,和pytest.parametrize功能大致相同

3.1参数为列表

import pytest
class Test_Login:
    @pytest.fixture(params=["111111","22222","33333"])
    def data_search(self,request):
        return request.param
    def test_search(self,data_search):
        print("输入IDA搜索:",data_search)
        assert 1==1

运行结果:

pytest.fixture()基础使用实例
从运行结果可以看出可以参数为列表可以使用到测试用例里

3.2参数为元组

import pytest
class Test_Login:
    @pytest.fixture(params=[(13811111111,111111),(13811111112,111111),(13811111111,111112)])
    def data_search(self,request):
        return request.param
    def test_search(self,data_search):
        print("输入账号为:", data_search[0])
        print("输入密码为:", data_search[1])
        assert 1==1

运行结果:

pytest.fixture()基础使用实例
运行结果可以看出,能把参数为元组的数据传递给用例
3.3参数为字典
import pytest
class Test_Login:
    @pytest.fixture(params=[{"userId":"11111","username":"眼泪","sex":"男"},{"userId":"22222","username":"眼角","sex":"男"}])
    def data_search(self,request):
        return request.param
    def test_search(self,data_search):
        print("搜索用户ID为:", data_search["userId"])
        print("搜索用户昵称为:", data_search["username"])
        print("搜索用户性别为:", data_search["sex"])
        assert 1==1

运行结果为

pytest.fixture()基础使用实例
从运行结果可以看出,参数为字典时能把数据传递给用例

四、autouse参数

autouse=True时,这个.py文件里的test方法和函数都执行pytest.fixture装饰的函数或方法
autouse=False时,只有主动调用时才会执行装饰器修饰的方法,默认是False,不写参数时按照默认处理

import pytest
class Test_Autouse:
    @pytest.fixture(autouse=True)
    def login_deta(self):
        print("测试用例执行前先执行我")
        yield
        print("测试用例执行后,执行我")
    def test_logi(self):
        print("测试登陆操作")
        assert 1==1
    def test_logi_err_user(self):
        print("使用错误的账号登陆")
        assert 1 == 1

运行结果:

pytest.fixture()基础使用实例

五、ids参数

六、name参数

Original: https://blog.csdn.net/changxingang/article/details/122117501
Author: changxingang
Title: pytest.fixture()基础使用实例

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/772636/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球