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