ci: 添加分层测试 CI 工作流

- 添加 GitHub Actions 工作流支持 L1/L2/L3 三层测试
- Makefile 新增 test-integration、test-e2e、test-all 目标
- 支持单元测试、集成测试、E2E 测试分离执行

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
xfy 2026-04-22 14:58:52 +08:00
parent 00c5319819
commit cc19328a81
2 changed files with 132 additions and 0 deletions

118
.github/workflows/test.yml vendored Normal file
View File

@ -0,0 +1,118 @@
name: Test
on:
push:
branches: [master]
pull_request:
branches: [master]
jobs:
# L1 单元测试
unit:
name: Unit Tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.26'
cache: true
- name: Download dependencies
run: go mod download
- name: Run unit tests
run: make test
- name: Run linter
uses: golangci/golangci-lint-action@v6
with:
version: latest
# L2 集成测试(无需 Docker
integration:
name: Integration Tests (L2)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.26'
cache: true
- name: Download dependencies
run: go mod download
- name: Run integration tests
run: make test-integration
# L3 E2E 测试(需要 Docker
e2e:
name: E2E Tests (L3)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.26'
cache: true
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Download dependencies
run: go mod download
- name: Run E2E tests
run: make test-e2e
continue-on-error: true # E2E 测试可能因 Docker 限制失败
# 构建验证
build:
name: Build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.26'
cache: true
- name: Build
run: make build
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: lolly
path: bin/lolly
# 代码覆盖率
coverage:
name: Coverage
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.26'
cache: true
- name: Generate coverage report
run: make test-cover
- name: Upload coverage
uses: codecov/codecov-action@v4
with:
files: ./coverage.out
fail_ci_if_error: false

View File

@ -148,6 +148,20 @@ test:
@echo "Running tests..."
go test -v ./...
# 运行 L2 集成测试(无需 Docker
test-integration:
@echo "Running L2 integration tests..."
go test -v -tags=integration ./internal/integration/...
# 运行 L3 E2E 测试(需要 Docker
test-e2e:
@echo "Running L3 E2E tests (requires Docker)..."
go test -v -tags=e2e ./internal/e2e/...
# 运行所有测试(单元 + 集成 + E2E
test-all: test test-integration test-e2e
@echo "All tests passed."
# 运行测试(带覆盖率)
test-cover:
@echo "Running tests with coverage..."