PyTest中PUT请求第二次执行总是失败的排查求助
问题描述
在PyTest中执行PUT请求时,第二次总是失败,但使用Postman连续发送多次PUT请求却无此异常。PyTest中首次执行PUT请求成功,但第二次必失败,只有每次修改JSON请求内容才能成功,不知该如何调试。测试按post、get、update、delete的顺序执行,但只有update(PUT请求)失败。
使用的技术栈版本
- python 3.10
- flask 2.1.2
- sqlalchemy 1.4.39
PUT请求测试代码
@pytest.mark.asyncio async def test_put_student(): randomFavSub = "" randomFavSub = randomFavSub.join(random.choice(letters) for i in range(10)) request_dict = { "user_id": "0e4c1d44-04f6-4a26-a02d-8e67a91b00f1", "fav_sub": "Subject" + randomFavSub } headers = { 'content-type': 'application/json', 'Accepts': 'application/json' } async with aiohttp.ClientSession(headers=headers) as session: await asyncio.sleep(5) async with session.put(URL + GLOBAL_ID, json=request_dict) as response: if response.status == 200: data = await response.json() data_student = data['student'] data_student_first = data_student[0] else: data = await response.text() assert False, "modify Failure response is text " + str(response.status) # TODO #38 Generalize the assert to all conditions for key, value in request_dict.items(): assert (value == data_student_first[key]), "modify FAILURE " + key
GLOBAL_ID来源(POST请求测试代码)
@pytest.mark.asyncio async def test_post_student(): request_dict = { "user_id": "0e4c1d44-04f6-4a26-a02d-8e67a91b00f1", "fav_sub": "Science" } global GLOBAL_ID async with aiohttp.ClientSession() as session: async with session.post(URL, json=request_dict) as response: if response.status == 200: data = await response.json() data_student = data['student'] data_student_first = data_student[0] GLOBAL_ID = data_student_first['id'] assert GLOBAL_ID, "GLOBAL_ID couldn't be created" else: data = await response.text() assert False, 'retrieve Failure response is text' for key, value in request_dict.items(): assert value == data_student_first[key], "create FAILURE key"
内容的提问来源于stack exchange,提问作者miazocool
相关产品推荐
相关产品推荐

