使用 Allure-Ruby 生成交互式测试趋势图

解读

在国内一线互联网公司的 Ruby 技术面试中,“如何基于 Allure-Ruby 做持续测试可视化” 是区分初中高级候选人的高频考点。
面试官真正想考察的不是“能跑起来就行”,而是:

  1. 你是否理解 Allure 的 Report 生命周期钩子 与 Ruby 元编程的结合点;
  2. 能否在 CI(GitLab-CI / GitHub Actions) 里把每一次 Pipeline 的测试结果沉淀为 时间序列数据;
  3. 是否掌握 Allure Trend 插件 的底层逻辑——Allure 只负责渲染,趋势数据必须由你来“喂”;
  4. 对国内网络隔离场景(无法访问 raw.githubusercontent.com)有没有 离线化部署 方案。
    一句话:把“跑完生成漂亮报告”变成“每次 MR 都能看到历史趋势折线”,才算通关。

知识点

  1. Allure-Ruby 适配器架构:RSpec、Minitest、Cucumber 三种主流适配器的 listener 注入点 与 categories.json 定制规则。
  2. Allure 命令行 –history 参数 与 –clean 参数的配合使用,防止历史数据被覆盖。
  3. allure-docker-service 二次封装:国内镜像加速、容器启动参数 CHECK_RESULTS_EVERY_SECONDS 与 KEEP_HISTORY 的布尔语义。
  4. 趋势图数据文件:
    • history/history-trend.json
    • history/history.json
    • widgets/history-trend.json
      三者区别与 merge 策略(Ruby 侧用 deep_merge gem)。
  5. 在 GitLab-CI 中利用 artifacts:reports:allure 与 expire_in 实现 跨 Pipeline 持久化。
  6. 元编程技巧:通过 AllureRuby::Config.trend_scope = :suite 动态切换趋势粒度(case / suite / feature)。
  7. 国内私有化场景:将 Allure UI 静态资源打包进 Rails 引擎 并挂载到 /allure 路由,绕过外网 CDN。

答案

下面给出可直接落地的 最小可运行范例,覆盖本地调试、CI 集成、离线化三步。
强调:所有路径均使用相对路径,确保在 CI 与本地行为一致。

步骤 1:Gemfile 引入

gem 'allure-rspec', '~> 2.20'
gem 'allure-ruby-commons', '~> 2.20'
gem 'deep_merge'   # 用于历史数据合并

步骤 2:RSpec 配置 spec_helper.rb

require 'allure-rspec'

AllureRspec.configure do |c|
  c.results_directory = 'allure-results'
  c.clean_results_directory = false   # 关键:不清除,才能累积历史
  c.logging_level = Logger::INFO
end

RSpec.configure do |config|
  config.include AllureRSpec::Adaptor
  # 自定义趋势维度
  config.before(:suite) do
    AllureRuby::Config.trend_scope = :suite
  end
end

步骤 3:生成历史数据脚本 scripts/build_trend.rb

require 'json'
require 'deep_merge'

RESULTS = 'allure-results'
HISTORY = 'allure-report/history'

Dir.mkdir(HISTORY) unless Dir.exist?(HISTORY)

old = Dir.glob("#{HISTORY}/*.json").each_with_object({}) do |file, hash|
  hash[File.basename(file)] = JSON.parse(File.read(file))
end

new = Dir.glob("#{RESULTS}/history/*.json").each_with_object({}) do |file, hash|
  hash[File.basename(file)] = JSON.parse(File.read(file))
end

merged = old.deep_merge(new) do |_, old_v, new_v|
  # 只保留最近 20 条构建记录,防止文件膨胀
  (old_v + new_v).uniq { |e| e['buildOrder'] }.sort_by { |e| -e['buildOrder'] }.first(20)
end

merged.each do |name, data|
  File.write("#{HISTORY}/#{name}", JSON.pretty_generate(data))
end

步骤 4:本地一键命令

# 运行测试
bundle exec rspec

# 合并历史
ruby scripts/build_trend.rb

# 生成报告
bundle exec allure generate allure-results -o allure-report --clean

# 打开交互式趋势图
bundle exec allure open allure-report

此时访问 http://localhost:63342/allure-report,Trend 标签页 即可看到随时间变化的通过率、耗时折线。

步骤 5:GitLab-CI 模板 .gitlab-ci.yml(关键片段)

variables:
  ALLURE_HISTORY: "$CI_PROJECT_DIR/allure-history"

rspec:
  stage: test
  image: ruby:3.2
  before_script:
    - bundle install
  script:
    - bundle exec rspec
    - ruby scripts/build_trend.rb
  artifacts:
    when: always
    paths:
      - allure-results/
      - allure-history/
    expire_in: 7 days
    reports:
      allure:
        paths:
          - allure-results/

pages:
  stage: deploy
  image: frankescobar/allure-docker-service:latest
  script:
    - cp -r allure-history allure-results/history 2>/dev/null || true
    - allure generate allure-results -o public --clean
  artifacts:
    paths:
      - public
  only:
    - main

解释:

  • 利用 GitLab Pages 托管静态 Allure 报告,history 目录跨 Pipeline 缓存,实现趋势累积。
  • 若公司使用私有 GitLab,可把 frankescobar/allure-docker-service 镜像推送到自建 Harbor,解决拉取超时问题。

步骤 6:离线化加速(国内必备)

  1. 下载 Allure 2.x 官方 release,解压后把 allure-*/plugins/trend 静态资源复制到 vendor/allure;
  2. 在 Rails 中新建引擎 AllureMountEngine,路由 mount AllureMountEngine => '/allure';
  3. 引擎内用 Rack::Static 指向 vendor/allure,彻底摆脱外网 CDN。

完成以上六步,即可在 无外网、高并发 MR 场景下,为团队提供 秒级交互式测试趋势图,面试官听到“离线化 + 历史缓存 + 跨 Pipeline 持久化”三连,基本确定你具备 高级 Ruby 测试架构 能力。

拓展思考

  1. 如果测试用例规模过万,history-trend.json 会膨胀到 10 MB+,如何基于 Redis 做流式聚合 再喂给 Allure?
  2. 在 多分支并行开发 模型下,如何让趋势图只展示当前分支的“增量”记录,而主干仍保持全量?(提示:重写 buildOrder 生成策略,用 CI_COMMIT_REF_SLUG 做命名空间)
  3. Allure 默认趋势维度只有 通过率、耗时、成功率 三项,如何注入自定义指标(比如 P99 接口延迟)?需要修改 allure-ruby-commons 的 TestResult 模型,并在 history-trend.json 里追加自定义字段,再配套写一套 React 组件 覆盖 Allure 原生 UI。
  4. 国内部分公司强制 等保三级,要求测试报告 脱敏,怎样在 Allure::Lifecycle 的 stop_test_case 阶段做 正则脱敏钩子,同时保证趋势数据不脱敏?
  5. 当团队同时存在 RSpec 与 Golang 的 Ginkgo 用例时,如何统一 Allure 趋势数据源,实现 多语言单视图?思路:在 CI 末端用 Ruby 脚本把 Ginkgo 生成的 allure-results/*.xml 转换成同构 JSON,再执行 build_trend.rb 合并。