pytest-项目结构

项目结构

python 复制代码
api_test_project/
├── config/
│   └── config.py  # 配置文件,存储接口的基本信息,如 URL、请求头、认证信息等
├── data/
│   └── test_data.json  # 测试数据文件,存储接口的请求参数、预期结果等
├── tests/
│   ├── __init__.py
│   ├── test_single_api.py  # 单接口测试用例文件
├── utils/
│   ├── __init__.py
│   └── api_client.py  # 封装接口请求的工具类
├── report/
│   └── report.html  # 测试报告文件
├── requirements.txt  # 项目依赖文件
└── run_tests.py  # 运行测试用例的脚本

1. config目录------存放项目的配置文件

python 复制代码
# config.py
# 接口的基本 URL
BASE_URL = 'https://api.example.com'
# 公共请求头
HEADERS = {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer your_token'
}

2. data目录------存放测试数据

python 复制代码
// test_data.json
{
    "get_user": {
        "request": {
            "method": "GET",
            "endpoint": "/users/1",
            "params": {}
        },
        "expected": {
            "status_code": 200,
            "data": {
                "id": 1,
                "name": "John Doe"
            }
        }
    }
}

3. test 目录------存放测试用例文件

python 复制代码
# tests/test_single_api.py
import pytest
import requests
from config.config import BASE_URL, HEADERS
import json

# 读取测试数据
with open('../data/test_data.json', 'r', encoding='utf-8') as f:
    TEST_DATA = json.load(f)

@pytest.mark.parametrize("test_case", TEST_DATA.values())
def test_single_api(test_case):
    request_info = test_case['request']
    expected = test_case['expected']

    method = request_info['method']
    endpoint = request_info['endpoint']
    params = request_info.get('params', {})

    url = BASE_URL + endpoint

    if method == 'GET':
        response = requests.get(url, headers=HEADERS, params=params)
    elif method == 'POST':
        response = requests.post(url, headers=HEADERS, json=params)
    # 可以根据需要添加更多的请求方法

    # 断言响应状态码
    assert response.status_code == expected['status_code']

    # 断言响应数据
    if 'data' in expected:
        assert response.json() == expected['data']

4. utils目录------存放工具类

python 复制代码
# utils/api_client.py
import requests
from config.config import BASE_URL, HEADERS

class APIClient:
    def __init__(self):
        self.base_url = BASE_URL
        self.headers = HEADERS

    def send_request(self, method, endpoint, params=None):
        url = self.base_url + endpoint
        if method == 'GET':
            response = requests.get(url, headers=self.headers, params=params)
        elif method == 'POST':
            response = requests.post(url, headers=self.headers, json=params)
        # 可以根据需要添加更多的请求方法
        return response

5. report目录------存放测试报告文件

运行测试用例后会生成 report.html 文件。

6. requirements.txt文件

该文件列出了项目的依赖库,例如:

pytest

requests

pytest-html

7. run_tests.py文件

该文件用于运行测试用例并生成测试报告:

python 复制代码
# run_tests.py
import pytest

if __name__ == "__main__":
    pytest.main(['-s', '-v', '--html=report/report.html', 'tests/'])
相关推荐
YC运维6 分钟前
网络配置综合实验全攻略(对之前学习的总结)
linux·服务器·网络
MarkGosling1 小时前
【开源项目】网络诊断告别命令行!NetSonar:开源多协议网络诊断利器
运维·后端·自动化运维
无敌的牛1 小时前
Linux基础开发工具
linux·运维·服务器
Edingbrugh.南空1 小时前
实战指南:用pmap+gdb排查Linux进程内存问题
linux·运维·服务器
朱颜辞镜花辞树‎2 小时前
GitHub 操作指南:项目协作与自动化工作流实践
运维·自动化·github
孙克旭_2 小时前
day051-ansible循环、判断与jinja2模板
linux·运维·服务器·网络·ansible
总有刁民想爱朕ha3 小时前
零基础搭建监控系统:Grafana+InfluxDB 保姆级教程,5分钟可视化服务器性能!
运维·服务器·grafana
Mr_Orangechen3 小时前
Linux 下使用 VS Code 远程 GDB 调试 ARM 程序
linux·运维·arm开发
撰卢4 小时前
【个人笔记】负载均衡
运维·笔记·负载均衡
Shartin4 小时前
Can201-Introduction to Networking: Application Layer应用层
服务器·开发语言·php