如何判断rspec在没有挂起测试输出的情况下运行?

Gri*_*wMF 16 ruby rspec

有没有办法(可能是一些关键)告诉rspec跳过挂起的测试并且不打印有关它们的信息?

我有一些自动生成的测试

pending "add some examples to (or delete) #{__FILE__}"
Run Code Online (Sandbox Code Playgroud)

我运行"bundle exec rspec spec/models --format documentation"并得到这样的东西:

Rating
  allows to rate first time
  disallow to rate book twice

Customer
  add some examples to (or delete) /home/richelieu/Code/first_model/spec/models/customer_spec.rb (PENDING: No reason given)

Category
  add some examples to (or delete) /home/richelieu/Code/first_model/spec/models/category_spec.rb (PENDING: No reason given)
......
Run Code Online (Sandbox Code Playgroud)

我想保留这些文件,因为我稍后会更改它们,但是现在我想输出如下:

Rating
  allows to rate first time
  disallow to rate book twice

Finished in 0.14011 seconds
10 examples, 0 failures, 8 pending
Run Code Online (Sandbox Code Playgroud)

dax*_*dax 12

看看标签 -

您可以在测试文件中执行类似的操作

describe "the test I'm skipping for now" do     
  it "slow example", :skip => true do
    #test here
  end
end
Run Code Online (Sandbox Code Playgroud)

并像这样运行你的测试:

bundle exec rspec spec/models --format documentation --tag ~skip
Run Code Online (Sandbox Code Playgroud)

其中~字符排除有以下标签的所有测试,在这种情况下,skip


And*_*nes 6

对于后代:您可以通过创建自定义格式化程序来抑制文档输出主体中待处理测试的输出.

(对于RSpec 3).我在我的spec目录中创建了一个house_formatter.rb文件,如下所示:

class HouseFormatter < RSpec::Core::Formatters::DocumentationFormatter
   RSpec::Core::Formatters.register self, :example_pending
   def example_pending(notification); end
end
Run Code Online (Sandbox Code Playgroud)

然后我将以下行添加到我的.rspec文件中:

--require spec/house_formatter
Run Code Online (Sandbox Code Playgroud)

现在我可以用格式化程序调用rspec --format HouseFormatter <file>.

请注意,我仍然在最后获得"待定测试"部分.但就我而言,那是完美的.

  • 你可以添加`def dump_pending(通知); 结束`以压制未决的段落.并使用ProgressFormatter而不是DocumentationFormatter. (3认同)

And*_*nes 5

This is the official "fix" posted for this problem on Github in response to the issue Marko raised, and as such it deserves a seperate answer.

This is probably the better answer, too; mine is pretty fragile. Credit for this should go to Myron Marston on the Rspec team.

You can implement this for yourself pretty easily:

module FormatterOverrides
  def example_pending(_)
  end

  def dump_pending(_)
  end
end

RSpec::Core::Formatters::DocumentationFormatter.prepend FormatterOverrides
Run Code Online (Sandbox Code Playgroud)

Or if you only want to silence block-less examples:

module FormatterOverrides
  def example_pending(notification)
    super if notification.example.metadata[:block]
  end

  def dump_pending(_)
  end
end

RSpec::Core::Formatters::DocumentationFormatter.prepend FormatterOverrides
Run Code Online (Sandbox Code Playgroud)

Or, if you just want to filter out block-less pending examples (but still show other pending examples):

RSpec.configure do |c|
  c.filter_run_excluding :block => nil
end
Run Code Online (Sandbox Code Playgroud)