簡體   English   中英

在運行測試之前,找到Rspec中的所有標簽

[英]Find all the tags in Rspec before running the tests

我正在使用rspec(3.5)來運行測試並以此構建CLI。 目前正在考慮耙。 在我的CLI中,我希望它能夠在我的spec文件夾中找到所有可用的標簽,並提供它作為運行RSPEC之前作為測試運行的選項。

我一直在瀏覽spec_helper,但無法找到標簽信息。 我的理解是,rspec CLI本身接受所有標記作為輸入,然后遍歷所有文件以查看每個文件是否與標記匹配,如果匹配,rspec將運行它們。

我想扭轉這個工作流程-

  1. 在我的spec文件夾中找到所有標簽。
  2. 在我的rake cli中提供它作為選項。
  3. 允許用戶選擇標簽並運行它。

現在,我陷入了第一步,找到了所有標簽。 假設,如何在不運行測試的情況下掃描所有spec文件夾,並將標簽暴露在某個地方?

我的代碼示例摘錄。 “:api”是我指的標簽。

describe 'Oh Title: Oh after title', :api, :data_key => "Oh my meta_data", :feature => "Oh my feature" do
    it 'oh my explanation' :data_key=> 'oh my meta_data' do |e|
    #blah blah#
    end
end

如果您遵循rspec文檔中列出的哈希火箭語法,則可能會執行以下操作:

grep -ri "it \\"" * | grep "=>" | awk -F'=>' '{print $1}' | awk '{print $NF}' | uniq

分解:

  • grep -ri "it \\"" *將提取包含it "所有行it "
  • grep "=>"將僅過濾標記的測試
  • awk -F'=>' '{print $1}'將在哈希火箭之前提取所有內容
  • awk '{print $NF}'將僅提取標記
  • uniq將僅打印唯一標簽

我知道這大約晚了2年,但是我和您在同一地點。這就是我所做的。

自定義rspec格式化程序

# spec/support/formatters/ci_rspec_run_files.rb
module Formatters
  class CiRspecRunFiles
    RSpec::Core::Formatters.register self, :example_started

    def initialize(output)
      @output = output
    end

    def example_started(notification)
      @output << "#{notification.example.id}\n"
    end
  end
end

 $ rspec --dry-run --tag ci:flakey --require ./spec/support/formatters/ci_rspec_run_files.rb -f Formatters::CiRspecRunFiles --out tmp/flakey_specs.txt

結果

$ cat tmp/flakey_specs.txt 
./spec/models/flakey_spec.rb[1:3:6:4:1]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM