柚子快報激活碼778899分享:程序人生 pytest
fixture跨模塊共享(conftest.py)
閱讀目錄:
1. conftest.py 基本介紹
2. 示例
3. 指定引用全局fixture
conftest.py 基本介紹
conftest.py定義: 是一個特殊的文件,用于定義測試配置 – 包含:fixture(測試夾具)、markers(標記)、 hooks(鉤子) 以及其他配置選項
conftest.py作用: 提供了一種集中管理和重測試設(shè)置的方式,有助于提高測試代碼的可讀性、可維護性和效率
共享fixtures: 若多個模塊使用的fixture相同,則可以將fixture寫在conftest.py中(定義備注: fixtures是pytest中的一個核心概念,用于設(shè)置和清理測試環(huán)境)組織測試配置:conftest.py文件可以用來組織和集中管理測試配置, 例如測試數(shù)據(jù)、測試環(huán)境的配置等,有助于保持測試代碼的整潔和可維護性定義markers: Markers 是用來標記測試用例的,可以用于分類測試,或者指定某些測試需要的滿足條件, 子啊conftest.py中定義markers可以方便的在測試用例中引用配置插件: 在conftest.py文件中使用 pytest_plugins 變量后, 可以導(dǎo)入和配置插件,使得插件的功能在測試中可用定義hooks:pytest提供了多個hooks,可在測試執(zhí)行的不同階段去執(zhí)行自定義代碼,通過在conftest.py中自定義的hooks,可以控制測試的執(zhí)行流程控制測試并執(zhí)行: 若使用的pytest-xdist 插件來并行執(zhí)行測試, conftest.py 中的fixture 可以被用來控制并行測試的行為,例: 通過scope= ‘module’ 來確保每個模塊的測試在單獨的進程中執(zhí)行測試環(huán)境隔離: conftest.py 文件通常位于測試目錄中,有助于隔離測試環(huán)境和生產(chǎn)環(huán)境,并確保測試不會影響生產(chǎn)代碼模塊級別的fixture: 在conftest.py中定義的fixture默認具有模塊級別的作用域,即意味著他們會為該模塊中的所有測試用例執(zhí)行一次,你可以通過置頂不同的scope參數(shù)來改變這個行為-- function、class、session
conftest.py特點
文件名稱默認為conftest.py 是pytest框架中固定的名字,不能隨意更改,通常在里面寫用例執(zhí)行前的一些初始化操作conftest.py文件 可以有多個(全局、局部), 搜索優(yōu)先級自底而上(從和模塊統(tǒng)計目錄開始找,一直到項目根目錄),遵循就近原則conftest.py 中的fixture 可以跨文件調(diào)用,支持函數(shù)引用、通過裝飾器調(diào)用,也可以自動適配(此時autouse=True; 若就近的一個都是False,遠的一個都是True,此時還是會自動適配近的,詳見:文章末尾)conftest.py 文件作用范圍是它同級的test文件,或者下面的test文件不需要import conftest.py , pytest框架會自動識別該文件,放到根目錄下 就可以全局目錄調(diào)用conftesst.py 文件不能被其他文件導(dǎo)入
示例
conftest.py 僅做用于局部
局部 conftest.py
#! usr/bin/env python
# _*_ coding: utf-8 _*_
# @Author: zsc
# @vx: M_Haynes
import pytest
#
# @pytest.fixture()
# def fixture_login():
# print(" --------------- 進行登錄")
# yield
# print(" ----------------- 進行登出")
#
# @pytest.fixture()
# def login(fixture_login):
# print(" ============ fun")
test_example7.py
#! usr/bin/env python
# _*_ coding: utf-8 _*_
# @Author: zsc
# @vx: M_Haynes
import pytest
# def test_example7(login):
# print(" ----------------------- test example7")
conftest.py 作用于全局
#! usr/bin/env python
# _*_ coding: utf-8 _*_
# @Author: zsc
# @vx: M_Haynes
"""
作用于全局 -- 即 sub_case這個文件下
"""
import pytest
# @pytest.fixture(autouse=True , scope="function")
# def setup_sub_function():
# print(" -------------- setup_sub_function : 前置 -------------")
# yield
# print(" -------------- setup_sub_function : 后置 --------------")
#
#
# @pytest.fixture(autouse=True, scope="module")
# def setup_sub_module():
# print(" -------------- setup_sub_module : 前置 ------------")
# yield
# print(" -------------- setup_sub_module teardown : 后置 --------------")
#
#
# @pytest.fixture(autouse=True, scope="class")
# def setup_sub_class():
# print("-------------- teardown_sub_class : 前置 -----------------")
# yield
# print(" -------------- teardown_sub_class : 后置 ---------------")
#
# @pytest.fixture(autouse=True, scope="package")
# def setup_sub_package():
# print(" -------------- setup_sub_package : 前置 --------------")
# yield
# print(" -------------- setup_sub_package : 后置 --------------")
#
# @pytest.fixture(autouse=True, scope="session")
# def setup_sub_session():
# print(" -------------- setup_sub_session : 前置 --------------")
# yield
# print(" -------------- setup_sub_session : 后置 ---------------")
結(jié)果:
conftest.py 局部和全局同時存在且同名的情況
全局添加:
### 全局和局部都有同名的 fixture
# @pytest.fixture(autouse=True, scope="function")
# def tongming_fixture():
# print(" -------------- tongming_fixture : 前置 同名的fixure --- 全局")
# yield
# print(" -------------- tongming_fixture : 后置 同名的fixture --- 全局")
局部添加:
# @pytest.fixture(autouse=False, scope="function")
# def tongming_fixture():
# print(" -------------- tongming_fixture : 前置 同名的fixure --- 局部")
# yield
# print(" -------------- tongming_fixture : 后置 同名的fixture --- 局部")
#
結(jié)果: 調(diào)用的fixture是局部的tongming_fixture,雖然是False,但是全局是True,所以哪怕沒調(diào)用,也會執(zhí)行這個局部fixture 全局添加:
@pytest.fixture(autouse=False, scope="function")
def tongming_fixture():
print(" -------------- tongming_fixture : 前置 同名的fixure --- 全局")
yield
print(" -------------- tongming_fixture : 后置 同名的fixture --- 全局")
局部添加:
@pytest.fixture(autouse=True, scope="function")
def tongming_fixture2():
print(" -------------- tongming_fixture : 前置 同名的fixure --- 局部")
yield
print(" -------------- tongming_fixture : 后置 同名的fixture --- 局部")
指定引用全局fixture
用例中可以通過@pytest.mark.usefixtures()指定引用全局autouse=False的fixture
@pytest.mark.usefixtures("tongming_fixture")
def test_example7():
print(" ----------------------- test example7")
更多交流和持續(xù)更新 請掃碼 + VX
本文由mdnice多平臺發(fā)布
柚子快報激活碼778899分享:程序人生 pytest
精彩文章
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。
轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。