在 GitLab 上实现自动化测试
一 核心思路与前置准备
二 最小可用流水线示例
stages:
- test
variables:
NODE_VERSION: "18"
test:
stage: test
image: node:$NODE_VERSION
script:
- npm ci
- npm test -- --ci --reporters=jest-junit
artifacts:
when: always
reports:
junit: reports/junit.xml
paths:
- reports/
stages:
- test
test:
stage: test
image: maven:3.9-openjdk-17
script:
- mvn test
artifacts:
when: always
reports:
junit: target/surefire-reports/TEST-*.xml
要点:通过 artifacts.reports.junit 指定测试结果文件路径,GitLab 会在 Pipelines/Jobs 页面展示测试报告与失败用例。
三 常见测试类型与配置要点
e2e:
stage: test
image: mcr.microsoft.com/playwright:v1.44.0-jammy
script:
- npm ci
- npx playwright install --with-deps
- npx playwright test --reporter=junit
artifacts:
when: always
reports:
junit: reports/e2e-junit.xml
paths:
- playwright-report/
四 查看结果与质量门禁
五 排错与最佳实践