Skip to content

Commit b848236

Browse files
authored
feat: 新增E2E测试 (#57)
* feat: 新增E2E测试 * fix: 修复docker compose命令书写错误 * fix: 运行测试前执行`playwright install` * fix: 增强检查以尝试修复3.11连接拒绝的问题 * fix: 增加mysql服务检查 * fix: 修复python 3.9兼容性问题 * perf: 优化禁用验证码脚本 * feat: 新增pg测试 * fix: 修复pg测试检查服务名书写错误 * perf: 优化测试 * style: 格式化代码
1 parent 332ddbe commit b848236

34 files changed

+3101
-31
lines changed

.github/workflows/playwright.yml

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
name: Playwright Tests
2+
3+
on:
4+
push:
5+
branches: [master]
6+
pull_request:
7+
branches: [master]
8+
9+
jobs:
10+
mysql-test:
11+
runs-on: ubuntu-latest
12+
strategy:
13+
fail-fast: false
14+
matrix:
15+
python-version: ["3.9", "3.10", "3.11", "3.12"]
16+
17+
steps:
18+
- uses: actions/checkout@v5
19+
20+
- name: Set up Python ${{ matrix.python-version }}
21+
uses: actions/setup-python@v5
22+
with:
23+
python-version: ${{ matrix.python-version }}
24+
25+
- name: Cache Docker layers
26+
uses: actions/cache@v5
27+
with:
28+
path: /tmp/.docker_cache
29+
key: ${{ runner.os }}-docker-${{ matrix.python-version }}-${{ hashFiles('**/Dockerfile*', '**/requirements.txt') }}
30+
31+
- name: Set up Docker Buildx
32+
uses: docker/setup-buildx-action@v3
33+
34+
- name: Create temporary Dockerfile with Python ${{ matrix.python-version }}
35+
run: |
36+
# Save original Dockerfile.my
37+
cp ruoyi-fastapi-backend/Dockerfile.my ruoyi-fastapi-backend/Dockerfile.my.bak
38+
# Create temporary Dockerfile with target Python version
39+
sed "s/FROM python:3.10/FROM python:${{ matrix.python-version }}/g" ruoyi-fastapi-backend/Dockerfile.my.bak > ruoyi-fastapi-backend/Dockerfile.my
40+
41+
- name: Start services with docker-compose
42+
run: |
43+
cd ruoyi-fastapi-test
44+
docker compose -f docker-compose.test.my.yml up -d --build
45+
46+
- name: Wait for services to be ready
47+
run: |
48+
cd ruoyi-fastapi-test
49+
# Wait for backend to be running
50+
timeout 180 bash -c 'until docker compose -f docker-compose.test.my.yml ps ruoyi-backend-my | grep -q "Up"; do sleep 5; done'
51+
# Wait for frontend to be running
52+
timeout 120 bash -c 'until docker compose -f docker-compose.test.my.yml ps ruoyi-frontend | grep -q "Up"; do sleep 5; done'
53+
# Additional wait for services to be fully ready and listening on ports
54+
sleep 30
55+
# Check that backend is actually responding on the API endpoint
56+
echo "Checking if backend service is ready..."
57+
for i in {1..30}; do
58+
if curl -f http://localhost:9099/captchaImage > /dev/null 2>&1; then
59+
echo "Backend service is ready!"
60+
break
61+
fi
62+
echo "Waiting for backend service to be ready... ($i/30)"
63+
sleep 5
64+
done
65+
# Final check
66+
if ! curl -f http://localhost:9099/captchaImage > /dev/null 2>&1; then
67+
echo "Backend service failed to start properly. Checking logs..."
68+
docker logs ruoyi-backend-my-test
69+
exit 1
70+
fi
71+
72+
- name: Run tests
73+
run: |
74+
cd ruoyi-fastapi-test
75+
pip install -r requirements.txt
76+
playwright install
77+
pytest -v
78+
79+
- name: Stop services
80+
if: always()
81+
run: |
82+
cd ruoyi-fastapi-test
83+
docker compose -f docker-compose.test.my.yml down
84+
85+
- name: Restore original Dockerfile.my
86+
if: always()
87+
run: |
88+
# Restore original Dockerfile.my after test
89+
mv ruoyi-fastapi-backend/Dockerfile.my.bak ruoyi-fastapi-backend/Dockerfile.my
90+
91+
pg-test:
92+
runs-on: ubuntu-latest
93+
strategy:
94+
fail-fast: false
95+
matrix:
96+
python-version: ["3.9", "3.10", "3.11", "3.12"]
97+
98+
steps:
99+
- uses: actions/checkout@v5
100+
101+
- name: Set up Python ${{ matrix.python-version }}
102+
uses: actions/setup-python@v5
103+
with:
104+
python-version: ${{ matrix.python-version }}
105+
106+
- name: Cache Docker layers
107+
uses: actions/cache@v5
108+
with:
109+
path: /tmp/.docker_cache
110+
key: ${{ runner.os }}-docker-${{ matrix.python-version }}-${{ hashFiles('**/Dockerfile*', '**/requirements.txt') }}
111+
112+
- name: Set up Docker Buildx
113+
uses: docker/setup-buildx-action@v3
114+
115+
- name: Create temporary Dockerfile with Python ${{ matrix.python-version }}
116+
run: |
117+
# Save original Dockerfile.my
118+
cp ruoyi-fastapi-backend/Dockerfile.my ruoyi-fastapi-backend/Dockerfile.my.bak
119+
# Create temporary Dockerfile with target Python version
120+
sed "s/FROM python:3.10/FROM python:${{ matrix.python-version }}/g" ruoyi-fastapi-backend/Dockerfile.my.bak > ruoyi-fastapi-backend/Dockerfile.my
121+
122+
- name: Start services with docker-compose
123+
run: |
124+
cd ruoyi-fastapi-test
125+
docker compose -f docker-compose.test.pg.yml up -d --build
126+
127+
- name: Wait for services to be ready
128+
run: |
129+
cd ruoyi-fastapi-test
130+
# Wait for backend to be running
131+
timeout 180 bash -c 'until docker compose -f docker-compose.test.pg.yml ps ruoyi-backend-pg | grep -q "Up"; do sleep 5; done'
132+
# Wait for frontend to be running
133+
timeout 120 bash -c 'until docker compose -f docker-compose.test.pg.yml ps ruoyi-frontend | grep -q "Up"; do sleep 5; done'
134+
# Additional wait for services to be fully ready and listening on ports
135+
sleep 30
136+
# Check that backend is actually responding on the API endpoint
137+
echo "Checking if backend service is ready..."
138+
for i in {1..30}; do
139+
if curl -f http://localhost:9099/captchaImage > /dev/null 2>&1; then
140+
echo "Backend service is ready!"
141+
break
142+
fi
143+
echo "Waiting for backend service to be ready... ($i/30)"
144+
sleep 5
145+
done
146+
# Final check
147+
if ! curl -f http://localhost:9099/captchaImage > /dev/null 2>&1; then
148+
echo "Backend service failed to start properly. Checking logs..."
149+
docker logs ruoyi-backend-pg-test
150+
exit 1
151+
fi
152+
153+
- name: Run tests
154+
run: |
155+
cd ruoyi-fastapi-test
156+
pip install -r requirements.txt
157+
playwright install
158+
pytest -v
159+
160+
- name: Stop services
161+
if: always()
162+
run: |
163+
cd ruoyi-fastapi-test
164+
docker compose -f docker-compose.test.pg.yml down
165+
166+
- name: Restore original Dockerfile.my
167+
if: always()
168+
run: |
169+
# Restore original Dockerfile.my after test
170+
mv ruoyi-fastapi-backend/Dockerfile.my.bak ruoyi-fastapi-backend/Dockerfile.my

.github/workflows/ruff.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Ruff Check
2+
3+
on:
4+
push:
5+
branches: [master]
6+
pull_request:
7+
branches: [master]
8+
9+
jobs:
10+
lint-format:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v5
14+
15+
- name: Set up Python
16+
uses: actions/setup-python@v5
17+
with:
18+
python-version: "3.9"
19+
20+
- name: Install dependencies
21+
run: |
22+
python -m pip install --upgrade pip
23+
pip install ruff
24+
25+
- name: Run linting
26+
run: |
27+
ruff check ruoyi-fastapi-backend
28+
ruff check ruoyi-fastapi-test
29+
30+
- name: Run format check
31+
run: |
32+
ruff format ruoyi-fastapi-backend --check
33+
ruff format ruoyi-fastapi-test --check

.github/workflows/test.yml

Lines changed: 0 additions & 31 deletions
This file was deleted.

ruoyi-fastapi-test/README.md

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# RuoYi-Vue3-FastAPI 项目测试套件
2+
3+
这是一个为 RuoYi-Vue3-FastAPI 项目创建的完整测试套件,使用 Playwright 进行端到端测试。测试环境已禁用验证码功能,以简化测试流程。
4+
5+
## 功能特性
6+
7+
- 使用默认方式手动启动前后端服务或`Docker Compose`自动启动项目前后端服务
8+
- 测试环境已禁用验证码功能
9+
- 验证登录流程和认证机制
10+
- 测试所有受保护的页面功能
11+
- 验证未登录用户访问受保护页面时的重定向行为
12+
13+
## 依赖安装
14+
15+
```bash
16+
pip install -r requirements.txt
17+
playwright install
18+
```
19+
20+
## 使用方法
21+
22+
### 方式一:默认方法
23+
24+
#### 启动前端
25+
26+
```bash
27+
cd ruoyi-fastapi-frontend
28+
npm install
29+
npm run dev
30+
```
31+
32+
#### 启动后端
33+
34+
```bash
35+
cd ruoyi-fastapi-backend
36+
pip install -r requirements.txt
37+
python app.py --env=dev
38+
```
39+
40+
#### 运行测试
41+
42+
```bash
43+
cd ruoyi-fastapi-test
44+
pip install -r requirements.txt
45+
python -m pytest -v
46+
```
47+
48+
### 方式二:使用Docker
49+
50+
#### 进入测试目录
51+
52+
```bash
53+
cd ruoyi-fastapi-test
54+
```
55+
56+
#### 启动 Docker 服务
57+
58+
```bash
59+
# MySQL版本
60+
docker compose -f docker-compose.test.my.yml up -d --build
61+
# PostgreSQL版本
62+
docker compose -f docker-compose.test.pg.yml up -d --build
63+
```
64+
65+
#### 运行测试
66+
67+
```bash
68+
pip install -r requirements.txt
69+
python -m pytest -v
70+
```
71+
72+
## 测试内容
73+
74+
### 登录测试
75+
76+
- 验证登录页面正常加载
77+
- 验证登录流程(测试环境已禁用验证码)
78+
- 测试认证后的页面访问
79+
80+
### 页面访问和功能测试
81+
82+
- 仪表盘页面
83+
- 用户管理页面
84+
- 角色管理页面
85+
- 菜单管理页面
86+
- 部门管理页面
87+
- 岗位管理页面
88+
- 字典管理页面
89+
- 参数配置页面
90+
- 通知公告页面
91+
- 日志管理页面(操作日志、登录日志)
92+
- 在线用户页面
93+
- 定时任务页面
94+
- 服务监控页面
95+
- 数据监控页面
96+
- 缓存监控页面
97+
- 缓存列表页面
98+
- 代码生成页面
99+
- 系统接口页面
100+
101+
### 认证测试
102+
103+
- 验证未登录用户访问受保护页面时被重定向到登录页
104+
- 验证登录后可以访问受保护页面
105+
106+
## 配置说明
107+
108+
使用 `docker-compose.test.my.yml``docker-compose.test.pg.yml`启动服务,默认前端端口为 `80`,后端端口为 `9099`。测试环境已禁用验证码功能。
109+
110+
## 注意事项
111+
112+
1. 确保系统已安装 Docker 和 Docker Compose
113+
2. 确保端口 `80``9099` 未被占用
114+
3. 首次运行时 Docker 镜像构建可能需要几分钟时间
115+
4. 测试使用默认管理员账户:用户名 `admin`,密码 `admin123`

ruoyi-fastapi-test/common/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)