搭建一个pytest 接口测试框架
时间: 2023-10-27 20:31:14 浏览: 50
好的,下面是搭建 PyTest 接口测试框架的步骤:
1. 安装 PyTest。在命令行中输入 `pip install pytest` 即可安装。
2. 创建一个测试用例文件。通常来说,测试用例文件名都以 `test_` 开头,如 `test_login.py`。
3. 编写测试用例。测试用例通常是一个函数,以 `test_` 开头,例如 `def test_login():`。
4. 在测试用例函数中编写测试代码。测试代码中应该包含请求接口、获取响应数据和断言的过程。
5. 运行测试用例。在命令行中输入 `pytest`,就可以运行所有以 `test_` 开头的测试用例了。
希望这些步骤能够帮到你。如果你有其他问题,欢迎继续提问。
相关问题
pytest接口测试框架搭建
### 如何搭建和配置 Pytest API 测试框架
#### 一、安装依赖库
为了构建一个完整的 pytest 接口测试框架,需要先安装必要的 Python 库。通常情况下,除了 `pytest` 自身外,还需要其他辅助工具如 `requests` 和 `pyyaml` 来发送 HTTP 请求以及解析 YAML 文件。
可以通过 pip 安装这些包:
```bash
pip install pytest requests pyyaml allure-pytest
```
这一步骤确保了所有必需组件都已就绪[^1]。
#### 二、创建项目结构
合理的文件夹布局有助于更好地管理和维护代码。建议按照以下方式组织项目的目录结构:
```
project_root/
├── tests/
│ ├── __init__.py
│ └── test_cases.py
├── config/
│ └── settings.yaml
└── conftest.py
```
这种结构使得不同类型的资源可以被清晰地区分开来,便于后续扩展和调试[^2]。
#### 三、编写配置文件
在 `config/settings.yaml` 中定义全局变量或常量,比如不同的环境 URL 地址等信息。通过这种方式可以在不修改具体实现的情况下轻松切换目标服务器地址[^4]:
```yaml
environments:
dev: "http://dev.example.com"
prod: "https://prod.example.com"
```
#### 四、初始化 Pytest 配置
编辑根目录下的 `conftest.py` 文件用于自定义 fixture 函数或其他共享逻辑。这里还可以设定一些默认参数以便于整个工程统一使用[^3]:
```python
import os
import yaml
import pytest
@pytest.fixture(scope="session", autouse=True)
def env_setup():
"""读取YAML配置"""
with open(os.path.join(os.getcwd(), 'config', 'settings.yaml')) as f:
global_config = yaml.safe_load(f)
yield global_config['environments']['dev'] # 默认开发环境URL
```
#### 五、编写测试用例
最后,在 `tests/test_cases.py` 编写具体的API调用逻辑,并利用前面提到的各种设施完成实际的功能验证工作。下面是一个简单的例子展示如何发起GET请求并断言响应状态码为200 OK:
```python
from urllib.parse import urljoin
import requests
class TestApiExample(object):
@staticmethod
def get_base_url() -> str:
from conftest import env_setup
return env_setup()
def test_get_status(self):
base_url = self.get_base_url()
endpoint = "/api/status"
response = requests.get(url=urljoin(base_url, endpoint))
assert response.status_code == 200
```
以上就是关于如何搭建和配置 pytest API 测试框架的主要步骤说明。希望上述内容能够帮助理解该过程中的各个要点。
搭建一个pytest+allure+request接口自动化框架
好的,这里为您提供一个简单的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接口自动化框架就搭建完成了。
阅读全文
相关推荐















