【发布时间】:2018-12-20 10:30:44
【问题描述】:
我们有一个用 RSpec 编写的单元测试套件。我们有一些失败的测试实际上很多。
我正在寻找的是一个脚本或一个魔术命令,用于将所有失败的测试标记为已跳过,因此我不必一一检查并将它们标记为已跳过。
【问题讨论】:
标签: ruby-on-rails-4 rspec rspec-rails rspec3
我们有一个用 RSpec 编写的单元测试套件。我们有一些失败的测试实际上很多。
我正在寻找的是一个脚本或一个魔术命令,用于将所有失败的测试标记为已跳过,因此我不必一一检查并将它们标记为已跳过。
【问题讨论】:
标签: ruby-on-rails-4 rspec rspec-rails rspec3
应该相对简单。 RSpec 列出了这样的失败规范
rspec ./spec/models/user.rb:67 # User does this thing
rspec ./spec/models/post.rb:13 # Post does another thing
rspec ./spec/models/rating.rb:123 # Rating does something else entirely
文件名和行号指向测试的开始行,即it ... do。
写一个脚本
it with xit。【讨论】:
我发现这个很棒的脚本可以满足我的需要: https://gist.github.com/mcoms/77954d191bde31d4677872d2ab3d0cd5
复制这里的内容,以防原主旨被删除:
# frozen_string_literal: true
class CustomFormatter
RSpec::Core::Formatters.register self, :example_failed
def initialize(output)
@output = output
end
def example_failed(notification)
tf = Tempfile.new
File.open(notification.example.metadata[:file_path]) do |f|
counter = 1
while (line = f.gets)
if counter == notification.example.metadata[:line_number]
line.sub!('it', 'skip')
line.sub!('scenario', 'skip')
@output << line
end
tf.write line
counter += 1
end
end
tf.close
FileUtils.mv tf.path, notification.example.metadata[:file_path]
end
end
【讨论】:
xit 和 xscenario 来保留信息,以防您想使用类似的脚本返回。如果 xscenario 不存在 - 将其定义为别名 relishapp.com/rspec/rspec-core/v/3-7/docs/configuration/…