From cc19328a81241966076bb5251c700f3ea52e41b4 Mon Sep 17 00:00:00 2001 From: xfy Date: Wed, 22 Apr 2026 14:58:52 +0800 Subject: [PATCH] =?UTF-8?q?ci:=20=E6=B7=BB=E5=8A=A0=E5=88=86=E5=B1=82?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=20CI=20=E5=B7=A5=E4=BD=9C=E6=B5=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加 GitHub Actions 工作流支持 L1/L2/L3 三层测试 - Makefile 新增 test-integration、test-e2e、test-all 目标 - 支持单元测试、集成测试、E2E 测试分离执行 Co-Authored-By: Claude Opus 4.7 --- .github/workflows/test.yml | 118 +++++++++++++++++++++++++++++++++++++ Makefile | 14 +++++ 2 files changed, 132 insertions(+) create mode 100644 .github/workflows/test.yml diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..dfe2e8b --- /dev/null +++ b/.github/workflows/test.yml @@ -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 diff --git a/Makefile b/Makefile index c2484a0..83043a9 100644 --- a/Makefile +++ b/Makefile @@ -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..."