requests+pytest接口自动化框架
时间: 2023-10-01 15:08:05 浏览: 114
Requests-pytest 是一个基于 Python 的接口自动化测试框架,它集成了 Requests 库和 Pytest 测试框架,可以方便地编写和运行接口测试用例。
使用 Requests-pytest,你可以:
1. 使用简洁的语法编写接口测试用例,例如发送请求、检查响应状态码、检查响应内容等。
2. 结合 Pytest 的 Fixture 功能,方便地管理测试数据和测试环境。
3. 支持生成测试报告,方便查看测试结果和分析测试覆盖率。
总之,Requests-pytest 是一个简单易用、功能强大的接口自动化测试框架,可以大大提高接口测试效率和质量。
相关问题
python+requests+pytest 编写自动化测试框架
1. 确定测试用例
首先,需要确定测试用例,即要测试的功能和场景。可以根据产品需求文档或者功能列表来确定测试用例,尽量覆盖所有的功能和场景。
2. 安装pytest和requests
pytest是Python的一个单元测试框架,可以方便地进行测试用例的编写和执行。requests是Python的一个HTTP库,可以方便地进行网络请求。
可以使用pip命令来安装这两个库:
pip install pytest
pip install requests
3. 编写测试用例
测试用例应该按照功能和场景分组,每个测试用例应该有一个明确的描述和预期结果。可以使用pytest的装饰器来标记测试用例:
import pytest
import requests
@pytest.mark.parametrize('url, expected', [
('http://example.com', 200),
('http://example.com/notfound', 404),
])
def test_requests(url, expected):
r = requests.get(url)
assert r.status_code == expected
这个测试用例中,使用了pytest的parametrize装饰器来指定不同的URL和期望的HTTP响应码。然后,使用requests库来发送HTTP请求,并使用assert语句来判断响应码是否符合预期。
4. 运行测试用例
使用pytest命令来运行测试用例:
pytest test_requests.py
如果所有测试用例都通过,那么pytest会输出一个“PASSED”字样。如果有测试用例失败,pytest会输出一个“FAILED”字样,并显示具体的错误信息。
5. 生成测试报告
可以使用pytest-html插件来生成测试报告:
pip install pytest-html
pytest --html=report.html
这样就可以生成一个HTML格式的测试报告,并保存到report.html文件中。测试报告会包含所有测试用例的运行结果和错误信息。可以将测试报告发送给开发人员或者项目经理,以便于跟踪测试进度和问题。
python+requests+pytest+allure+yaml+DDT+logs 接口自动化框架终极版怎么将环境配置成自己的
要在Python中使用requests、pytest、allure、yaml、DDT和logs构建接口自动化框架,并将环境配置成自己的,可以按照以下步骤进行:
### 1. 安装必要的库
首先,确保安装了必要的Python库。你可以使用pip来安装这些库:
```bash
pip install requests pytest allure-pytest PyYAML
```
### 2. 项目结构
创建一个项目目录,并按照以下结构组织文件:
```
project/
│
├── tests/
│ ├── test_cases/
│ │ ├── test_example.py
│ ├── conftest.py
│
├── data/
│ ├── test_data.yaml
│
├── utils/
│ ├── logger.py
│ ├── request_utils.py
│
├── reports/
│
├── config/
│ ├── config.yaml
│
└── requirements.txt
```
### 3. 配置环境
在`config/config.yaml`文件中配置环境变量:
```yaml
base_url: "https://api.example.com"
timeout: 10
```
### 4. 日志配置
在`utils/logger.py`中配置日志:
```python
import logging
import os
def get_logger():
logger = logging.getLogger("api_logger")
logger.setLevel(logging.DEBUG)
if not logger.handlers:
fh = logging.FileHandler("logs/api.log")
fh.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
logger.addHandler(fh)
logger.addHandler(ch)
return logger
```
### 5. 请求工具
在`utils/request_utils.py`中编写请求工具:
```python
import requests
from utils.logger import get_logger
import yaml
logger = get_logger()
def send_request(method, url, **kwargs):
response = requests.request(method, url, **kwargs)
logger.info(f"Request: {method} {url} {kwargs}")
logger.info(f"Response: {response.status_code} {response.text}")
return response
```
### 6. 测试用例
在`tests/test_cases/test_example.py`中编写测试用例:
```python
import pytest
import yaml
from utils.request_utils import send_request
@pytest.fixture(scope="module")
def config():
with open("config/config.yaml", "r") as file:
config = yaml.safe_load(file)
return config
def test_example(config):
url = config["base_url"] + "/endpoint"
response = send_request("GET", url)
assert response.status_code == 200
```
### 7. 运行测试
使用pytest运行测试,并生成Allure报告:
```bash
pytest --alluredir=reports
allure serve reports
```
### 8. 数据驱动
使用DDT进行数据驱动测试。在`tests/test_cases/test_ddt_example.py`中:
```python
import pytest
import yaml
from utils.request_utils import send_request
@pytest.fixture(scope="module")
def config():
with open("config/config.yaml", "r") as file:
config = yaml.safe_load(file)
return config
@pytest.mark.parametrize("data", yaml.safe_load(open("data/test_data.yaml", "r")))
def test_ddt_example(config, data):
url = config["base_url"] + "/endpoint"
response = send_request("POST", url, json=data)
assert response.status_code == 200
```
### 9. 报告生成
Allure会生成详细的测试报告,可以通过浏览器查看。
阅读全文
相关推荐
















