pytest接口自动化代码
时间: 2023-08-22 18:13:29 浏览: 112
接口自动化源代码--自己练习部分
pytest接口自动化代码可以使用pytest框架结合requests库来编写。首先,需要安装pytest和requests库。然后,可以按照以下步骤编写pytest接口自动化代码:
1. 导入pytest和requests库:
```python
import pytest
import requests
```
2. 编写测试用例函数,使用pytest的装饰器`@pytest.mark.parametrize`来传递参数:
```python
@pytest.mark.parametrize("url, data", \[
("http://example.com/api/login", {"username": "admin", "password": "123456"}),
("http://example.com/api/logout", {"token": "abcdefg"})
\])
def test_api(url, data):
response = requests.post(url, data=data)
assert response.status_code == 200
assert response.json()\["success"\] == True
```
3. 运行测试用例:
```python
pytest.main(\["-v"\])
```
这样就可以使用pytest框架和requests库编写接口自动化测试代码了。在测试报告中,可以看到每个测试用例的执行结果和断言的结果。
#### 引用[.reference_title]
- *1* *3* [Python+Requests+PyTest+Excel+Allure(一)接口自动化测试实战(附代码)](https://blog.csdn.net/fish_study_csdn/article/details/112246432)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [Python+Pytest 接口自动化](https://blog.csdn.net/weixin_44738514/article/details/127919385)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文