搭建一个pytest+allure+request接口自动化框架
时间: 2023-10-01 11:05:17 浏览: 88
好的,这里为您提供一个简单的pytest+allure+requests接口自动化框架搭建步骤:
1. 安装pytest和allure
```
pip install pytest
pip install allure-pytest
```
2. 安装requests库
```
pip install requests
```
3. 创建测试用例文件和测试数据文件
在项目中创建一个tests文件夹,用于存放测试用例文件。在tests文件夹中创建一个test_api.py文件,用于编写测试用例。
同时,也需要在项目中创建一个data文件夹,用于存放测试数据文件。在data文件夹中创建一个api_data.json文件,用于存放接口测试数据。
4. 编写测试用例
在test_api.py文件中,使用pytest编写测试用例。可以使用requests库发送接口请求,并对返回结果进行断言。
示例代码:
```python
import pytest
import requests
class TestAPI:
@pytest.fixture(scope='session', autouse=True)
def setup_class(self):
self.base_url = 'https://api.github.com'
def test_get_user(self):
url = self.base_url + '/users/github'
response = requests.get(url)
assert response.status_code == 200
assert response.json()['login'] == 'github'
def test_create_gist(self):
url = self.base_url + '/gists'
data = {
"description": "test gist",
"public": True,
"files": {
"test.txt": {
"content": "Test gist content"
}
}
}
headers = {
"Authorization": "token <your access token>"
}
response = requests.post(url, json=data, headers=headers)
assert response.status_code == 201
```
5. 编写测试数据
在api_data.json文件中,编写接口测试用到的数据。可以对不同接口的测试数据进行分类,方便维护。
示例代码:
```json
{
"get_user": {
"url": "/users/github",
"method": "get",
"headers": {},
"params": {},
"data": {},
"json": {},
"expected_status_code": 200,
"expected_data": {
"login": "github"
}
},
"create_gist": {
"url": "/gists",
"method": "post",
"headers": {
"Authorization": "token <your access token>"
},
"params": {},
"data": {
"description": "test gist",
"public": true,
"files": {
"test.txt": {
"content": "Test gist content"
}
}
},
"json": {},
"expected_status_code": 201,
"expected_data": {}
}
}
```
6. 使用pytest执行测试用例
在项目根目录下打开终端,执行如下命令:
```
pytest --alluredir=./report
```
该命令将会执行tests文件夹下的所有测试用例,并将测试结果保存在report文件夹中。
7. 生成测试报告
执行完测试用例后,使用如下命令生成测试报告:
```
allure generate ./report -o ./report/html --clean
```
该命令将会在report文件夹下生成一个html文件夹,里面包含测试结果的详细信息和图表。
至此,一个简单的pytest+allure+requests接口自动化框架就搭建完成了。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)