项目准备
项目准备
环境准备
1. 测试环境搭建
1.1 服务器环境
| 环境类型 |
服务器配置 |
操作系统 |
备注 |
| 测试环境 |
CPU: 4核, 内存: 8GB, 硬盘: 100GB |
CentOS 7.9 |
用于接口功能测试 |
| 性能测试环境 |
CPU: 8核, 内存: 16GB, 硬盘: 200GB |
CentOS 7.9 |
用于性能压力测试 |
| 开发环境 |
CPU: 2核, 内存: 4GB, 硬盘: 50GB |
Windows 10 |
用于开发调试 |
1.2 软件环境
| 软件名称 |
版本 |
安装方式 |
用途 |
| Java |
11.0.16 |
yum install |
运行Spring Boot应用 |
| Maven |
3.8.6 |
源码编译 |
项目构建工具 |
| Git |
2.31.1 |
yum install |
代码版本管理 |
| Docker |
20.10.17 |
yum install |
容器化部署 |
| Nginx |
1.20.2 |
yum install |
反向代理 |
1.3 数据库环境
| 数据库类型 |
版本 |
配置 |
用途 |
| H2 |
2.1.214 |
内存数据库 |
开发测试 |
| MySQL |
8.0.30 |
主从复制 |
生产环境 |
| Redis |
6.2.7 |
单机模式 |
缓存服务 |
2. 测试工具准备
2.1 接口测试工具
| 工具名称 |
版本 |
安装方式 |
用途 |
| Python |
3.9.0 |
官网下载 |
测试脚本运行环境 |
| pytest |
7.0.0 |
pip install |
测试框架 |
| requests |
2.28.0 |
pip install |
HTTP请求库 |
| allure-pytest |
2.10.0 |
pip install |
测试报告生成 |
| pytest-html |
3.1.1 |
pip install |
HTML测试报告 |
2.2 性能测试工具
| 工具名称 |
版本 |
安装方式 |
用途 |
| JMeter |
5.5.0 |
官网下载 |
性能压力测试 |
| Apache Bench |
2.3 |
yum install |
简单性能测试 |
| wrk |
4.2.0 |
源码编译 |
高性能压测工具 |
2.3 监控工具
| 工具名称 |
版本 |
安装方式 |
用途 |
| Prometheus |
2.40.0 |
官网下载 |
监控数据收集 |
| Grafana |
9.3.0 |
官网下载 |
监控数据可视化 |
| ELK Stack |
8.5.0 |
Docker部署 |
日志分析 |
3. 测试数据准备
3.1 基础测试数据
{
"owners": [
{
"id": 1,
"firstName": "George",
"lastName": "Davis",
"address": "110 W. Liberty St.",
"city": "Madison",
"telephone": "6085551023"
},
{
"id": 2,
"firstName": "Betty",
"lastName": "Davis",
"address": "638 Cardinal Ave.",
"city": "Sun Prairie",
"telephone": "6085551749"
},
{
"id": 3,
"firstName": "Eduardo",
"lastName": "Rodriquez",
"address": "2693 Commerce St.",
"city": "McFarland",
"telephone": "6085558763"
}
]
}
3.2 边界测试数据
{
"boundary_data": {
"max_length_name": "a".repeat(100),
"min_length_name": "a",
"special_characters": "@#$%^&*()",
"unicode_characters": "测试数据",
"empty_string": "",
"null_value": null
}
}
3.3 性能测试数据
{
"performance_data": {
"large_dataset": "1000条宠物主人记录",
"concurrent_users": "100个并发用户",
"request_frequency": "每秒1000次请求"
}
}
4. 配置文件准备
4.1 测试配置文件
# config/test_config.yaml
test:
base_url: "http://localhost:8080"
timeout: 30
retry_count: 3
database:
host: "localhost"
port: 3306
username: "testuser"
password: "testpass"
database: "petclinic_test"
logging:
level: "INFO"
format: "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
file: "logs/test.log"
4.2 环境变量配置
# .env
TEST_ENV=test
BASE_URL=http://localhost:8080
DB_HOST=localhost
DB_PORT=3306
DB_USERNAME=testuser
DB_PASSWORD=testpass
LOG_LEVEL=INFO
5. 测试脚本准备
5.1 项目结构
petclinic_api_test/
├── config/
│ ├── __init__.py
│ └── test_config.yaml
├── data/
│ ├── __init__.py
│ ├── test_data.json
│ └── boundary_data.json
├── utils/
│ ├── __init__.py
│ ├── api_client.py
│ ├── data_generator.py
│ └── report_generator.py
├── tests/
│ ├── __init__.py
│ ├── test_owners_api.py
│ ├── test_performance.py
│ └── test_security.py
├── conftest.py
├── pytest.ini
├── requirements.txt
└── README.md
5.2 依赖文件
# requirements.txt
pytest==7.0.0
requests==2.28.0
allure-pytest==2.10.0
pytest-html==3.1.1
pytest-xdist==3.0.2
pytest-cov==4.0.0
faker==18.0.0
pyyaml==6.0
python-dotenv==1.0.0
5.3 测试配置
# pytest.ini
[tool:pytest]
testpaths = tests
python_files = test_*.py
python_classes = Test*
python_functions = test_*
addopts =
--html=reports/report.html
--self-contained-html
--alluredir=reports/allure-results
--cov=utils
--cov-report=html:reports/coverage
markers =
smoke: 冒烟测试
regression: 回归测试
performance: 性能测试
security: 安全测试
6. 部署脚本准备
6.1 Docker部署脚本
# Dockerfile
FROM openjdk:11-jre-slim
WORKDIR /app
COPY target/petclinic-*.jar app.jar
EXPOSE 8080
CMD ["java", "-jar", "app.jar"]
6.2 部署脚本
#!/bin/bash
# deploy.sh
echo "开始部署PetClinic应用..."
# 构建应用
mvn clean package -DskipTests
# 构建Docker镜像
docker build -t petclinic:latest .
# 停止旧容器
docker stop petclinic-app || true
docker rm petclinic-app || true
# 启动新容器
docker run -d \
--name petclinic-app \
-p 8080:8080 \
-e SPRING_PROFILES_ACTIVE=test \
petclinic:latest
echo "部署完成!"
7. 监控配置准备
7.1 Prometheus配置
# prometheus.yml
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'petclinic'
static_configs:
- targets: ['localhost:8080']
metrics_path: '/actuator/prometheus'
7.2 Grafana仪表板
{
"dashboard": {
"title": "PetClinic API监控",
"panels": [
{
"title": "API响应时间",
"type": "graph",
"targets": [
{
"expr": "http_server_requests_seconds{application='petclinic'}"
}
]
},
{
"title": "API请求数量",
"type": "graph",
"targets": [
{
"expr": "http_server_requests_total{application='petclinic'}"
}
]
}
]
}
}
8. 测试执行准备
8.1 测试执行脚本
#!/bin/bash
# run_tests.sh
echo "开始执行接口自动化测试..."
# 创建报告目录
mkdir -p reports
# 执行测试
pytest tests/ \
--html=reports/report.html \
--self-contained-html \
--alluredir=reports/allure-results \
--cov=utils \
--cov-report=html:reports/coverage
# 生成Allure报告
allure generate reports/allure-results -o reports/allure-report --clean
echo "测试执行完成!"
echo "HTML报告: reports/report.html"
echo "Allure报告: reports/allure-report/index.html"
echo "覆盖率报告: reports/coverage/index.html"
8.2 CI/CD配置
# .github/workflows/api-test.yml
name: API Tests
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main ]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.9'
- name: Install dependencies
run: |
pip install -r requirements.txt
- name: Run tests
run: |
pytest tests/ --html=reports/report.html --self-contained-html
- name: Upload test results
uses: actions/upload-artifact@v3
with:
name: test-results
path: reports/