首页主机资讯GitLab如何在Linux上实现自动化测试

GitLab如何在Linux上实现自动化测试

时间2025-11-17 15:42:03发布访客分类主机资讯浏览447
导读:在 Linux 上使用 GitLab 实现自动化测试 一 架构与前置条件 核心组件:GitLab Runner(在 Linux 服务器上执行作业)、.gitlab-ci.yml(定义 stages、jobs、脚本与产出)、Pipeline...

在 Linux 上使用 GitLab 实现自动化测试

一 架构与前置条件

  • 核心组件:GitLab Runner(在 Linux 服务器上执行作业)、.gitlab-ci.yml(定义 stages、jobs、脚本与产出)、Pipeline(由 push、merge request 等事件自动触发)。Runner 可安装在自托管 Linux 主机,也可使用共享 Runner;执行器可选 shell、docker、kubernetes 等,以兼顾速度与一致性。提交代码后,Runner 按配置自动运行测试并将结果回传 GitLab 界面查看。

二 安装与注册 GitLab Runner

  • 安装(Debian/Ubuntu 示例)
    • 添加仓库并安装:
      • curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh | sudo bash
      • sudo apt-get install gitlab-runner
  • 安装(RHEL/CentOS 示例)
    • 添加仓库并安装:
      • curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh | sudo bash
      • sudo yum install gitlab-runner
  • 注册 Runner
    • 在项目 Settings → CI/CD → Runners 获取 URLRegistration token
    • 执行注册(按需选择 executor,如 shell/docker):
      • sudo gitlab-runner register --url --registration-token --executor shell --description “linux-runner” --tag-list “ci,test” --run-untagged false --locked false
  • 启动服务
    • sudo gitlab-runner start
  • 验证
    • 在项目 Runners 页面应看到新注册 Runner 处于 Active 状态。

三 编写 .gitlab-ci.yml 运行测试

  • 最小可用示例(Maven)
    • stages:
      • build
      • test
    • build:
      • stage: build
      • script:
        • mvn clean package
      • artifacts:
        • paths:
          • target/*.jar
    • test:
      • stage: test
      • script:
        • mvn test
      • artifacts:
        • reports:
          • junit: target/surefire-reports/TEST-*.xml
  • 关键要点
    • 使用 stages 定义执行顺序(如 build → test → deploy)。
    • 使用 artifacts.reports.junit 上传 JUnit XML,在 GitLab 测试报告面板展示用例与失败详情。
    • 使用 artifacts.paths 保存构建产物(如 target/*.jar),供后续阶段或部署使用。

四 常见测试场景示例

  • Node.js + Playwright(UI 自动化)
    • test_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:
        • reports:
          • junit: test-results/results.xml
  • 使用 Docker 运行测试(环境一致、易复用)
    • test:
      • stage: test
      • image: python:3.11
      • script:
        • pip install -r requirements.txt
        • pytest --junitxml=report.xml
      • artifacts:
        • reports:
          • junit: report.xml
  • 并行与缓存(加速反馈)
    • variables:
      • PYTEST_WORKERS: 4
    • test_unit:
      • stage: test
      • script:
        • pytest -n $PYTEST_WORKERS --junitxml=unit.xml
      • artifacts:
        • reports:
          • junit: unit.xml
    • cache:
      • key: ${ CI_COMMIT_REF_SLUG}
      • paths:
        • .cache/pip
        • node_modules/

五 触发与优化实践

  • 触发与查看
    • 推送代码或创建 Merge Request 会自动触发 Pipeline;在 CI/CD → PipelinesJobs 页面查看状态与日志,测试报告在 Tests 选项卡展示。
  • 变量与规则
    • 使用 variables 管理环境差异;用 rulesonly/except 控制作业执行分支与条件,例如仅 main/master 执行部署作业。
  • 缓存与产物
    • 合理使用 cache 缓存依赖(如 node_modules/、.m2/、.cache/pip)显著缩短执行时间;用 artifacts 在阶段间传递二进制与报告。
  • 通知与质量门禁
    • 可结合 SonarQube 做质量扫描,或接入 邮件/Slack 通知;在合并前以测试与质量门禁作为准入条件,保证主线稳定性。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: GitLab如何在Linux上实现自动化测试
本文地址: https://pptw.com/jishu/749014.html
Linux环境下GitLab的日志如何查看 如何在Linux中自定义GitLab界面

游客 回复需填写必要信息