我有一個正在運行的基本端點測試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)建單獨的測試來完成。
添加回答
舉報
0/150
提交
取消