1、多个fixture的使用顺序
# -*- coding:utf-8 -*- # @Author: Sky # @Email: [email protected] # @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: [email protected] # @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: [email protected] # @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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。