pytest-fixture使用詳解03(上)

一、fixture的特點(diǎn)

  1. 在測(cè)試函數(shù)運(yùn)行前后,由pytest執(zhí)行的外殼函數(shù),代碼可定制
  2. 用于將測(cè)試前后進(jìn)行預(yù)備或清理核心邏輯的一種機(jī)制,在執(zhí)行測(cè)試函數(shù)之前(或之后)加載運(yùn)行他們
  3. 跟定義函數(shù)差不多,區(qū)別是函數(shù)上加個(gè)裝飾器@pytest.fixture()

通常作用:

  1. 有明確的名字,在其他函數(shù)、模塊、類或整個(gè)工程調(diào)用它時(shí)激活,也可相互調(diào)用;
  2. 參數(shù)化功能,根據(jù)配置與不同組件選擇不同的參數(shù);
  3. 許多測(cè)試用例都只需要執(zhí)行一次的操作:登錄、數(shù)據(jù)庫初始連接與關(guān)閉;

滿足多變的需求:

  1. 定義傳入測(cè)試中的數(shù)據(jù)集
  2. 配置測(cè)試前系統(tǒng)的初始狀態(tài)
  3. 為批量測(cè)試提供數(shù)據(jù)源等

二、conftest.py 配置可實(shí)現(xiàn)數(shù)據(jù)共享,有如下特點(diǎn):

  1. conftest這個(gè)名字是固定的
  2. 與運(yùn)行的用例一個(gè)包里,有init.py文件
  3. 不需要導(dǎo)入,一個(gè)目錄中可自動(dòng)找到,同目錄下文件運(yùn)行前執(zhí)行
  4. 全局的配置和前期工作都寫這里,放著文件包中,共享數(shù)據(jù)
    【多層文件中,當(dāng)包里有只運(yùn)行里面的,里面沒有,運(yùn)行外面的conftest】
@pytest.fixture(scope="function",params=None,autouse=False,ids=None,name=None)
def test():
  print("fixture初始化的參數(shù)列表")

三、fixture的使用方法

特性—:通過前后置屬性,控制用例是否需要登錄
import pytest

@pytest.fixture
def login():
    print('登錄操作')
    yield
    print("退出登錄")

# 哪條用例需要用到對(duì)應(yīng)的前置或后置操作時(shí),直接將fixture的名稱通過傳參的形式放在用例中。
class Test_Login():
    def test_s1(self,login):
        print("用例1:登錄之后其他動(dòng)作 123")

    def test_s2(self):  #不傳 login
        print("用例2:不需要登錄,操作 456")

    def test_s3(self,login):
        print("用例3:登錄之后其他動(dòng)作 123")

if __name__ == '__main__':
    pytest.main(['1super.py','-vs'])

運(yùn)行結(jié)果
特性二:不是test開頭,加了裝飾器也不會(huì)執(zhí)行fixture
import pytest

@pytest.fixture
def login2():
    print("please輸入賬號(hào),密碼先登錄")

@pytest.mark.usefixtures("login2")
def login4():
    print("123")
# 無結(jié)果
特性三:pytest.mark.usefixtures裝飾類,可以讓執(zhí)行每個(gè)case前,都執(zhí)行一遍指定的fixture

conftest.py

import pytest

@pytest.fixture()
def login2():
    print("please輸入賬號(hào),密碼先登錄")

另外一個(gè)文件

import pytest

@pytest.mark.usefixtures("login2")
class Test1():
    def test_s1(self):
        print("用例1:登錄之后其他動(dòng)作")
    def test_s2(self):
        print("用例2:登錄之后其他動(dòng)作")

if __name__ == '__main__':
    pytest.main(['1super.py','-vs'])
運(yùn)行結(jié)果
實(shí)操實(shí)現(xiàn):https://{ip}/api/ideal-new-org/api/group/member/info?skUserId=10001800

需要傳token、skUserId

第一步,請(qǐng)求接口
def user_position(token,skUserId):
        """
        獲取單個(gè)用戶信息
        :token:
        :return: 自定義的關(guān)鍵字返回結(jié)果 result
        """

    result = ResultBase()
    header = {
        "Content-Type": "application/json",
        "Authorization": token,
    }
    params_data = { "skUserId": skUserId}

    # parameters = {"skUserId": skUserId}
    res = user.get_user_position(params=params_data, headers=header)
第二步,提取參數(shù) skUserId:data.skUserId

@pytest.fixture(scope="function")

def test_get_user_skUserId(login_fixture):

    # global skUserId
    user_info = login_fixture
    token = user_info['data']['accessToken']
    result = get_one_user_info(token)
    skUserId = result.response.json()['data']['skUserId']
    yield skUserId
第三步,引用參數(shù)
def test_get_user_position(self,test_get_user_skUserId, login_fixture, except_result, except_code, except_msg):

    logger.info("*************** 開始執(zhí)行用例 ***************")
    # 獲取token
    user_info = login_fixture
    token = user_info['data']['accessToken']

    # 獲取skUserId
    skUserId = test_get_user_skUserId
    result = user_position(token,skUserId)

執(zhí)行結(jié)果:

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容