帮我写pytest+python接口自动化框架
时间: 2023-10-07 21:09:38 浏览: 129
好的,下面是一个简单的 pytest + Python 接口自动化框架:
1. 安装pytest和requests模块
```shell
pip install pytest requests
```
2. 创建一个tests文件夹,在该文件夹下创建一个conftest.py文件,用于存放pytest的fixture和hook函数
```python
import pytest
import requests
@pytest.fixture(scope="session")
def base_url():
return "https://api.example.com"
@pytest.fixture(scope="session")
def session():
return requests.Session()
@pytest.fixture(scope="session")
def headers():
return {"Content-Type": "application/json"}
```
3. 在tests文件夹下创建一个test_xxx.py文件,用于编写测试用例
```python
def test_login(base_url, session, headers):
url = base_url + "/login"
data = {"username": "test", "password": "123456"}
response = session.post(url, json=data, headers=headers)
assert response.status_code == 200
assert response.json()["msg"] == "success"
```
4. 运行测试用例
```shell
pytest tests/
```
这个框架非常简单,可以根据需要自行扩展。例如,可以添加一个fixture来读取测试数据,可以使用pytest-xdist插件实现并行运行,等等。
阅读全文
相关推荐


















