012、多个fixture的使用顺序

2021/7/19 6:07:46

本文主要是介绍012、多个fixture的使用顺序,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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的使用顺序的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程