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=======‘)
执行结果如下:
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>
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=======‘)
执行结果如下:
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>
示例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‘
执行结果如下:
==================================================================================== 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>