12

给定以下 RSpec 配置(v2.12.0):

RSpec.configure do |config|
 config.treat_symbols_as_metadata_keys_with_true_values = true
 config.filter_run :focus => true
 config.run_all_when_everything_filtered = true
end

有时人们忘记从规范中删除标签,并且在我们希望运行所有规范的持续集成环境中,只有带有剩余标签:focus的规范才能运行。:focus

我试过了:

rspec --tag ~focus

...它运行所有规格,不包括带有 :focus 标记的规格

有没有办法使用 rspec 的命令行选项强制运行所有规范而忽略任何标签?

4

4 回答 4

13

我刚刚将它添加到一个项目中:

config.before :focused => true do
  fail "Hey dummy, don't commit focused specs." if ENV['FORBID_FOCUSED_SPECS']
end

在我们的持续集成服务器运行的脚本中:

export FORBID_FOCUSED_SPECS=true
于 2013-06-28T15:51:33.233 回答
8

You could remove the lines:

 config.filter_run :focus => true
 config.run_all_when_everything_filtered = true

and tell users to run focused tests with rspec --tag focus. That way the CI will always run the full test suite.

You might consider checking the environment in the configuration block and including/excluding the filter_run setting appropriately.

Another thought: if you are using git, set a pre-commit hook to prevent specs with :focus from creeping into the code base in the first place.

于 2013-02-18T22:08:29.127 回答
8

我想在设置焦点时自动在我们的持续集成服务器上失败。这是根据myronmarston的代码重写的,以便与 rspec-rails 3.2.0 一起正常工作:

  config.before(:example, :focus) do
    fail 'This example was committed with `:focus` and should not have been'
  end if ENV['CI']
于 2014-10-20T20:10:49.510 回答
5

尝试:rspec --tag focus --tag ~focus

于 2015-07-30T00:28:18.230 回答