【问题标题】:How to tell rspec to run without pending tests output?如何告诉 rspec 在没有挂起测试输出的情况下运行?
【发布时间】:2014-01-21 12:58:36
【问题描述】:

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

我有一些自动生成的测试,比如

pending "add some examples to (or delete) #{__FILE__}"

我运行“bundle exec rspec spec/models --format 文档”并得到如下内容:

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)
......

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

Rating
  allows to rate first time
  disallow to rate book twice

Finished in 0.14011 seconds
10 examples, 0 failures, 8 pending

【问题讨论】:

    标签: ruby rspec


    【解决方案1】:

    看看tags -

    你可以在你的测试文件中做这样的事情

    describe "the test I'm skipping for now" do     
      it "slow example", :skip => true do
        #test here
      end
    end
    

    并像这样运行您的测试:

    bundle exec rspec spec/models --format documentation --tag ~skip
    

    ~ 字符排除所有带有以下标记的测试,在本例中为skip

    【讨论】:

    • 谢谢。所以你不能跳过所有未决的?为每个添加标志的唯一方法?
    • 实际上,我不确定 - 尝试在不更改测试的情况下运行 bundle exec rspec spec/models --format documentation --tag ~skip,看看它是否有效,如果没有,只需在应该跳过的测试中添加标签
    • @GriwMF,您可以在 ~/.rspec 创建一个全局 rspec 配置文件,而不是在命令行中添加 '--tag "~skip"。作为奖励,这也将跳过它们。
    【解决方案2】:

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

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

    class HouseFormatter < RSpec::Core::Formatters::DocumentationFormatter
       RSpec::Core::Formatters.register self, :example_pending
       def example_pending(notification); end
    end
    

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

    --require spec/house_formatter
    

    现在我可以使用rspec --format HouseFormatter &lt;file&gt; 调用格式化程序。

    请注意,最后我仍然会看到“待定测试”部分。但就我而言,这是完美的。

    【讨论】:

    • 您可以添加def dump_pending(notification); end 来压制待处理的段落。并使用 ProgressFormatter 而不是 DocumentationFormatter。
    • 谢谢!我在 rspec github repo 上创建了一个问题,因为我认为应该有一个命令行选项来执行此操作。 github.com/rspec/rspec-core/issues/2377
    • @MarkoAvlijaš ...aaaand,他们关闭了它。 :( 不过,他们确实发布了比我更好的解决方案,所以我厚颜无耻地将该代码作为这个问题的新答案。也许这会对某人有所帮助......
    【解决方案3】:

    这是在 Github 上针对此问题发布的官方“修复”,以回应 issue Marko 提出的问题,因此值得单独回答。

    这也可能是更好的答案;我的很脆弱。这应该归功于 Rspec 团队的Myron Marston

    您可以很容易地自己实现它:

    module FormatterOverrides
      def example_pending(_)
      end
    
      def dump_pending(_)
      end
    end
    
    RSpec::Core::Formatters::DocumentationFormatter.prepend FormatterOverrides
    

    或者,如果您只想静音无块示例:

    module FormatterOverrides
      def example_pending(notification)
        super if notification.example.metadata[:block]
      end
    
      def dump_pending(_)
      end
    end
    
    RSpec::Core::Formatters::DocumentationFormatter.prepend FormatterOverrides
    

    或者,如果您只想过滤掉无块待处理示例(但是 仍然显示其他未决示例):

    RSpec.configure do |c|
      c.filter_run_excluding :block => nil
    end
    

    【讨论】:

    • 你在哪里添加这些模块?
    • @AustinRichardson 你可以把它放在 spec_helper.rb 中——看看上面“问题”这个词后面的 github 线程。
    • 把它放在我的规范助手中没有任何影响。有什么建议吗?我正在使用并行测试。也许这与它有关?
    • 其实只是在没有parallel_tests的情况下运行,仍然输出挂起。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-09
    • 2010-12-06
    • 1970-01-01
    • 2021-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多