1. 程式人生 > 其它 >020、內建 fixture —— request

020、內建 fixture —— request

參考資料:https://www.cnblogs.com/yoyoketang/p/14041751.html

一、內建 fixture —— request

  概述:

    a、request 是 pytest 的內建 fixture , "為請求物件提供對請求測試上下文的訪問權, 並且在fixture被間接引數化的情況下具有可選的“param” 屬性。 "

    

  1)request.param 獲取測試的請求引數

    前面講 fixture引數化 的時候,有接觸到 "request.param" 用於獲取測試的請求引數,以下示例 :

    a、在 fixture裡面使用 request

,示例程式碼如下:

import pytest


user_data = [
    {'name': 'sky', 'age': 18, 'hobby': '游泳'},
    {'name': 'Jack', 'age': 250, 'hobby': '搞錢'},
    {'name': 'Tony', 'age': 250, 'hobby': '玩遊戲'}
]


# 在 fixture 裡面使用內建的fixture,request
@pytest.fixture(params=user_data)
def login(request):
    users = request.param
    
return users def test_login(login): user = login print(f"使用者名稱為:{user['name']},年齡為:{user['age']},愛好為:{user['hobby']}") # 如果在 測試用例函式 裡面使用內建的fixture,request 會報錯 # 報錯資訊:AttributeError: 'FixtureRequest' object has no attribute 'param' # def test_login(login, request): # print(request.param)
# user = login # print(f"使用者名稱為:{user['name']},年齡為:{user['age']},愛好為:{user['hobby']}")
View Code

      執行結果如下,無報錯:

D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day11\venv\Scripts\python.exe "C:\SkyWorkSpace\WorkTools\PyCharm\PyCharm_Community_Edition_202003\PyCharm Community Edition 2020.3\plugins\python-ce\helpers\pycharm\_jb_pytest_runner.py" --path D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day11/gg/test_gg.py
Testing started at 18:22 ...
Launching pytest with arguments D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day11/gg/test_gg.py in D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day11\gg

============================= test session starts =============================
platform win32 -- Python 3.8.6, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day11\venv\Scripts\python.exe
cachedir: .pytest_cache
rootdir: D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day11\gg
collecting ... collected 3 items

test_gg.py::test_login[login0] PASSED                                    [ 33%]使用者名稱為:sky,年齡為:18,愛好為:游泳

test_gg.py::test_login[login1] PASSED                                    [ 66%]使用者名稱為:Jack,年齡為:250,愛好為:搞錢

test_gg.py::test_login[login2] PASSED                                    [100%]使用者名稱為:Tony,年齡為:250,愛好為:玩遊戲


============================== 3 passed in 0.02s ==============================

Process finished with exit code 0
View Code

    b、如果在 測試用例函式 裡面使用內建的 fixture,request 會報錯報錯資訊:AttributeError: 'FixtureRequest' object has no attribute 'param' 。

      示例程式碼如下:

import pytest


user_data = [
    {'name': 'sky', 'age': 18, 'hobby': '游泳'},
    {'name': 'Jack', 'age': 250, 'hobby': '搞錢'},
    {'name': 'Tony', 'age': 250, 'hobby': '玩遊戲'}
]


# 在 fixture 裡面使用內建的fixture,request
@pytest.fixture(params=user_data)
def login(request):
    users = request.param
    return users


# 如果在 測試用例函式 裡面使用內建的fixture,request 會報錯
# 報錯資訊:AttributeError: 'FixtureRequest' object has no attribute 'param'
def test_login(login, request):
    print(request.param)
    user = login
    print(f"使用者名稱為:{user['name']},年齡為:{user['age']},愛好為:{user['hobby']}")
View Code

      執行結果如下:

D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day11\venv\Scripts\python.exe "C:\SkyWorkSpace\WorkTools\PyCharm\PyCharm_Community_Edition_202003\PyCharm Community Edition 2020.3\plugins\python-ce\helpers\pycharm\_jb_pytest_runner.py" --path D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day11/gg/test_gg.py
Testing started at 18:28 ...
Launching pytest with arguments D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day11/gg/test_gg.py in D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day11\gg

============================= test session starts =============================
platform win32 -- Python 3.8.6, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day11\venv\Scripts\python.exe
cachedir: .pytest_cache
rootdir: D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day11\gg
collecting ... collected 3 items

test_gg.py::test_login[login0] FAILED                                    [ 33%]
test_gg.py:20 (test_login[login0])
login = {'age': 18, 'hobby': '游泳', 'name': 'sky'}
request = <FixtureRequest for <Function test_login[login0]>>

    def test_login(login, request):
>       print(request.param)
E       AttributeError: 'FixtureRequest' object has no attribute 'param'

test_gg.py:22: AttributeError
FAILED                                    [ 66%]
test_gg.py:20 (test_login[login1])
login = {'age': 250, 'hobby': '搞錢', 'name': 'Jack'}
request = <FixtureRequest for <Function test_login[login1]>>

    def test_login(login, request):
>       print(request.param)
E       AttributeError: 'FixtureRequest' object has no attribute 'param'

test_gg.py:22: AttributeError
FAILED                                    [100%]
test_gg.py:20 (test_login[login2])
login = {'age': 250, 'hobby': '玩遊戲', 'name': 'Tony'}
request = <FixtureRequest for <Function test_login[login2]>>

    def test_login(login, request):
>       print(request.param)
E       AttributeError: 'FixtureRequest' object has no attribute 'param'

test_gg.py:22: AttributeError




Assertion failed

test_gg.py::test_login[login1] 
test_gg.py::test_login[login2] 

================================== FAILURES ===================================
_____________________________ test_login[login0] ______________________________

login = {'age': 18, 'hobby': '游泳', 'name': 'sky'}
request = <FixtureRequest for <Function test_login[login0]>>

    def test_login(login, request):
>       print(request.param)
E       AttributeError: 'FixtureRequest' object has no attribute 'param'

test_gg.py:22: AttributeError
_____________________________ test_login[login1] ______________________________

login = {'age': 250, 'hobby': '搞錢', 'name': 'Jack'}
request = <FixtureRequest for <Function test_login[login1]>>

    def test_login(login, request):
>       print(request.param)
E       AttributeError: 'FixtureRequest' object has no attribute 'param'

test_gg.py:22: AttributeError
_____________________________ test_login[login2] ______________________________

login = {'age': 250, 'hobby': '玩遊戲', 'name': 'Tony'}
request = <FixtureRequest for <Function test_login[login2]>>

    def test_login(login, request):
>       print(request.param)
E       AttributeError: 'FixtureRequest' object has no attribute 'param'

test_gg.py:22: AttributeError
=========================== short test summary info ===========================
FAILED test_gg.py::test_login[login0] - AttributeError: 'FixtureRequest' obje...
FAILED test_gg.py::test_login[login1] - AttributeError: 'FixtureRequest' obje...
FAILED test_gg.py::test_login[login2] - AttributeError: 'FixtureRequest' obje...
============================== 3 failed in 0.09s ==============================

Process finished with exit code 1




Assertion failed

Assertion failed
View Code

  2)、request.config

      request.config 是獲取測試的配置檔案引數,這個在前面講命令列引數的時候有用到過 ,

      在 conftest.py 寫一個 hook函式, pytest_addoption 的作用是用於獲取命令列引數,request.config 用於讀取測試的配置資料,

未完成,待續。