大家好,我是兰若,喜欢技术,热爱分享。用CSDN来记录及分享关于软件测试的一切,今天给大家分享的是如何从无到有搭建一个接口自动化测试框架。
目录结构
project_name/
│
├── config/
│ ├── __init__.py
│ ├── config.py
│
├── tests/
│ ├── __init__.py
│ ├── test_cases/
│ │ ├── __init__.py
│ │ ├── test_example.py
│ │
│ ├── test_data/
│ │ ├── __init__.py
│ │ ├── data_example.json
│ │
│ ├── test_utils/
│ ├── __init__.py
│ ├── utils.py
│
├── reports/
│ ├── README.md
│
├── logs/
│ ├── README.md
│
├── requirements.txt
├── .gitignore
├── pytest.ini
└── README.md
详细文件说明及代码示例
1. config/config.py
用于配置环境信息、API端点等。
# config/config.py
import os
class Config:
BASE_URL = os.getenv("BASE_URL", "http://localhost:5000/api")
TIMEOUT = 30
config = Config()
2. tests/test_cases/test_example.py
具体的测试用例文件。
# tests/test_cases/test_example.py
import requests
from config.config import config
from tests.test_utils.utils import send_get_request
import allure
@allure.feature('Example API')
@allure.story('Get Example')
def test_get_example():
with allure.step("Send GET request to /example"):
response = send_get_request("example")
with allure.step("Verify the response status code"):
assert response.status_code == 200
with allure.step("Verify the response content"):
assert response.json()["key"] == "value"
3. tests/test_data/data_example.json
测试数据文件。
{
"key": "value"
}
4. tests/test_utils/utils.py
工具函数,包含通用的请求方法、数据处理等。
# tests/test_utils/utils.py
import requests
from config.config import config
def send_get_request(endpoint, params=None):
url = f"{config.BASE_URL}/{endpoint}"
response = requests.get(url, params=params, timeout=config.TIMEOUT)
return response
def send_post_request(endpoint, data=None):
url = f"{config.BASE_URL}/{endpoint}"
response = requests.post(url, json=data, timeout=config.TIMEOUT)
return response
5. requirements.txt
项目依赖包。
requests
pytest
allure-pytest
6. pytest.ini
配置pytest。
# pytest.ini
[pytest]
minversion = 6.0
addopts = --alluredir=reports --clean-alluredir
testpaths = tests/test_cases
7. .gitignore
忽略不需要提交到版本控制的文件。
/logs
/reports
__pycache__
*.pyc
.env
8. README.md
项目说明文档。
# Project Name
## Description
This is a project for API testing using Python, pytest, and Allure.
## Installation
1. Clone the repository
2. Create a virtual environment
3. Install dependencies:
```
pip install -r requirements.txt
```
## Usage
Run tests:
pytest
Generate Allure report:
allure serve reports
详细步骤
-
创建虚拟环境并安装依赖:
- 创建虚拟环境:
python -m venv venv
- 激活虚拟环境并安装依赖:
pip install -r requirements.txt
- 创建虚拟环境:
-
配置环境变量:
- 在项目根目录创建一个
.env
文件,用于存储环境变量,如API的基本URL。 - 使用
python-dotenv
库读取.env
文件中的环境变量。
- 在项目根目录创建一个
-
编写测试用例:
- 在
tests/test_cases/
目录中编写具体的测试用例。 - 使用
requests
库发送HTTP请求,验证响应。 - 使用Allure的装饰器
@allure.feature
和@allure.story
标记测试用例。 - 在测试用例中使用Allure的
allure.step
记录详细的测试步骤。
- 在
-
组织测试数据:
- 在
tests/test_data/
目录中组织测试数据,使用JSON格式存储。
- 在
-
编写工具函数:
- 在
tests/test_utils/utils.py
中编写常用的请求方法和数据处理函数,便于复用。
- 在
-
运行测试并生成报告:
- 使用
pytest
运行测试,并生成Allure格式的测试报告。 - 配置
pytest.ini
文件,便于自定义pytest的行为和生成报告。
- 使用
-
查看Allure报告:
- 使用
allure serve reports
命令生成并查看Allure测试报告。
- 使用
实施自动化测试框架的详细步骤
1. 创建虚拟环境并安装依赖
在项目根目录下,运行以下命令创建并激活虚拟环境:
python -m venv venv
source venv/bin/activate # Unix/macOS
venv\Scripts\activate # Windows
pip install -r requirements.txt
2. 配置环境变量
在项目根目录下创建一个.env
文件,内容如下:
BASE_URL=http://localhost:5000/api
使用python-dotenv
库在config/config.py
中读取环境变量:
from dotenv import load_dotenv
import os
load_dotenv()
class Config:
BASE_URL = os.getenv("BASE_URL", "http://localhost:5000/api")
TIMEOUT = 30
config = Config()
3. 编写测试用例
在tests/test_cases/
目录中编写具体的测试用例,例如:
# tests/test_cases/test_example.py
import requests
from config.config import config
from tests.test_utils.utils import send_get_request
import allure
@allure.feature('Example API')
@allure.story('Get Example')
def test_get_example():
with allure.step("Send GET request to /example"):
response = send_get_request("example")
with allure.step("Verify the response status code"):
assert response.status_code == 200
with allure.step("Verify the response content"):
assert response.json()["key"] == "value"
4. 组织测试数据
在tests/test_data/
目录中创建测试数据文件,例如:
{
"key": "value"
}
5. 编写工具函数
在tests/test_utils/utils.py
中编写常用的请求方法和数据处理函数,例如:
# tests/test_utils/utils.py
import requests
from config.config import config
def send_get_request(endpoint, params=None):
url = f"{config.BASE_URL}/{endpoint}"
response = requests.get(url, params=params, timeout=config.TIMEOUT)
return response
def send_post_request(endpoint, data=None):
url = f"{config.BASE_URL}/{endpoint}"
response = requests.post(url, json=data, timeout=config.TIMEOUT)
return response
6. 运行测试并生成报告
运行以下命令来运行测试并生成Allure报告:
pytest
allure serve reports
7. 查看Allure报告
使用allure serve reports
命令生成并查看Allure测试报告,该命令将启动一个临时的Web服务器,并在浏览器中打开报告。
通过以上步骤和文件组织,可以构建一个高效的接口测试自动化框架,使用Allure生成详细的测试报告,提升测试的覆盖率和可维护性。