首页 > 其他 > 详细

012、多个fixture的使用顺序

时间:2021-07-19 09:54:26      阅读:18      评论:0      收藏:0      [点我收藏+]

1、多个fixture的使用顺序

  依据测试用例方法调用时的排序 执行  

技术分享图片
# -*- coding:utf-8 -*-
# @Author:  Sky
# @Email:   2780619724@qq.com
# @Time:    2021/7/18 23:47
import pytest


@pytest.fixture()
def first():
    print(==========step1==========)


@pytest.fixture()
def second():
    print(==========step2==========)


@pytest.fixture()
def three():
    print(==========step3==========)


def test_01(first, second, three):
    print(===========test_01=======)


def test_02(second, first, three):
    print(===========test_01=======)


def test_03(second, first):
    print(===========test_01=======)
View Code

执行结果如下:

技术分享图片
D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\ff>pytest -s
=================================================================================== test session starts ====================================================================================
platform win32 -- Python 3.8.6, pytest-5.4.3, py-1.10.0, pluggy-0.13.1
rootdir: D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\ff
plugins: allure-pytest-2.9.43, html-2.1.1, metadata-1.11.0
collected 3 items

test_ff.py ==========step1==========
==========step2==========
==========step3==========
===========test_01=======
.==========step2==========
==========step1==========
==========step3==========
===========test_01=======
.==========step2==========
==========step1==========
===========test_01=======
.

==================================================================================== 3 passed in 0.04s =====================================================================================

D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\ff>
View Code

 

Fixture之间也可以互相调用

技术分享图片
# -*- coding:utf-8 -*-
# @Author:  Sky
# @Email:   2780619724@qq.com
# @Time:    2021/7/18 23:47
import pytest


@pytest.fixture()
def first():
    print(==========step1==========)


@pytest.fixture()
def second(first):
    print(==========step2==========)


@pytest.fixture()
def three(second):
    print(==========step3==========)


def test_01(three):
    print(===========test_01=======)
View Code

执行结果如下:

技术分享图片
D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\ff>pytest -s
=================================================================================== test session starts ====================================================================================
platform win32 -- Python 3.8.6, pytest-5.4.3, py-1.10.0, pluggy-0.13.1
rootdir: D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\ff
plugins: allure-pytest-2.9.43, html-2.1.1, metadata-1.11.0
collected 1 item

test_ff.py ==========step1==========
==========step2==========
==========step3==========
===========test_01=======
.

==================================================================================== 1 passed in 0.03s =====================================================================================

D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\ff>
View Code

 

示例2:

技术分享图片
# -*- coding:utf-8 -*-
# @Author:  Sky
# @Email:   2780619724@qq.com
# @Time:    2021/7/18 23:47
import pytest


@pytest.fixture()
def username():
    print(==========获取用户名==========)
    name = sky
    return name


@pytest.fixture()
def passwd(username):
    print(==========获取密码==========)
    pwd = 123456
    return pwd


@pytest.fixture()
def login(username, passwd):
    print(==========登录==========)
    name = username
    pwd = passwd
    return success


def test_01(login):
    print(===========测试登录=======)
    assert login == success
View Code

执行结果如下:

技术分享图片
==================================================================================== 1 passed in 0.03s =====================================================================================

D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\ff>pytest -s
=================================================================================== test session starts ====================================================================================
platform win32 -- Python 3.8.6, pytest-5.4.3, py-1.10.0, pluggy-0.13.1
rootdir: D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\ff
plugins: allure-pytest-2.9.43, html-2.1.1, metadata-1.11.0
collected 1 item

test_ff.py ==========获取用户名==========
==========获取密码==========
==========登录==========
===========测试登录=======
.

==================================================================================== 1 passed in 0.04s =====================================================================================

D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\ff>
View Code

 

012、多个fixture的使用顺序

原文:https://www.cnblogs.com/qq-2780619724/p/15028464.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!