写一个完整的接口自动化框架
时间: 2023-08-25 13:54:37 浏览: 35
下面是一个简单的完整接口自动化测试框架的示例:
1. 安装所需的库:
```shell
pip install pytest
pip install requests
pip install allure-pytest
```
2. 创建一个测试用例文件,比如`test_api.py`,并导入所需的库:
```python
import pytest
import requests
import allure
@pytest.mark.parametrize("url", ["https://api.example.com/users", "https://api.example.com/posts"])
@allure.feature("API Test")
def test_api(url):
with allure.step("发送请求"):
response = requests.get(url)
with allure.step("断言状态码"):
assert response.status_code == 200
with allure.step("断言响应结果"):
assert response.json()["success"] == True
```
3. 创建一个pytest的配置文件,比如`pytest.ini`,用于配置测试环境和其他选项:
```ini
[pytest]
addopts = -s -v --alluredir=./allure-report
```
4. 运行测试用例并生成测试报告:
```shell
pytest
allure serve allure-report
```
这样,pytest会自动查找当前目录下以`test_`开头的文件,并执行其中的测试函数。测试结果将会生成一个详细的HTML测试报告,可以通过allure命令来查看。
这个示例使用了pytest进行测试用例的管理和执行,使用requests库发送请求并获取响应,使用allure-pytest插件生成漂亮的测试报告。你可以根据实际需求对框架进行扩展和修改,例如加入参数化、前置后置处理等功能。
希望这个示例能对你有所帮助!