第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用函數(shù)為pytest測試提供參數(shù)

使用函數(shù)為pytest測試提供參數(shù)

FFIVE 2024-01-27 15:29:19
我有一個正在運行的基本端點測試pytest filename.py:ENDPOINTS = ['/users']def test_get_requests(application_headers):    """ Test all endpoints with a GET method """    for endpoint in ENDPOINTS:        response = requests.get(API_URL + endpoint, headers=application_headers)    assert response.status_code == 200這使用ENDPOINTS列表和application_headers固定裝置將 HTTP 請求發(fā)送到指定端點并斷言其成功狀態(tài)代碼。這很好用。我在遠程 JSON 文件中有一個端點列表,我想加載該端點,然后從中篩選端點進行測試,而不是使用手動定義的 ENDPOINTS list。我創(chuàng)建了一個這樣的裝置:@pytest.fixture(scope='module')def application_endpoints():    endpoints = []    with request.urlopen('https://my.endpoint.file.json') as response:        if response.getcode() == 200:            source = response.read()            data = json.loads(source)    for path in data["paths"]:        endpoints.append(path)    return endpoints然后我將測試定義更改為def test_get_requests(application_headers, application_endpoints),但這會導致錯誤TypeError: can only concatenate str (not "list") to str。我還嘗試了一種不同的方法,使用參數(shù)化,但這也失敗了:@pytest.mark.parametrize('endpoint', application_endpoints)def test_get_requests(application_headers):    """ Test all endpoints with a GET method """    for endpoint in ENDPOINTS:        response = requests.get(API_URL + endpoint, headers=application_headers)    assert response.status_code == 200該故障的錯誤是NameError: name 'application_endpoints' is not defined。感覺就像我在這里做了一些非常簡單的事情,卻弄錯了。我是否以錯誤的方式思考這個問題,或者我的語法不正確?
查看完整描述

1 回答

?
暮色呼如

TA貢獻1853條經(jīng)驗 獲得超9個贊

您可以使用該parametrize方法,但不能使用參數(shù)的固定裝置 - 只能使用函數(shù)。endpoints另外,您的代碼中存在一些混淆application_headers。忽略application_headers來自哪里,您可以嘗試以下操作:


def application_endpoints():  # not a fixture!

    endpoints = []

    ...

    return endpoints


@pytest.mark.parametrize('endpoint', application_endpoints())

def test_get_requests(endpoint):

    response = requests.get(API_URL + endpoint, headers=application_headers)

    assert response.status_code == 200

無需自己迭代端點 - 這mark.parametrize已經(jīng)通過為每個端點創(chuàng)建單獨的測試來完成。


查看完整回答
反對 回復 2024-01-27
  • 1 回答
  • 0 關注
  • 115 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號