【问题标题】:Can Cucumber hooks differentiate tags黄瓜钩能区分标签吗
【发布时间】:2015-08-26 17:11:45
【问题描述】:

Ruby 2。我有一个作为模块实现的 API。它定义了一个类,就像

class Foo
  include API
end

但它也用于扩展另一个模块通过

module Bar
  class << self
    include API
  end
end

我想测试 Foo 实例的行为(例如Foo.new.methname)以及模块函数(例如Bar.方法名)。

我的一个Before 挂钩将@exemplar 定义为应应用测试的默认对象。那么,问题来了:我的Before 钩子如何判断它应该使用@exemplar = Bar 还是@exemplar = Foo.new

或者,在玩了一些标签之后,让我尝试一种不同的方法。如果一个场景用@a@b 标记,我有Around('@a')Around('@b') 钩子,并且用-t @a 调用黄瓜,两个 钩子都会被调用。有没有办法让钩子代码告诉

  1. Around('...') 参数是什么('...' 的值),以及
  2. 实际应用的集合中有哪些标签?

Around('@b') 钩子有什么方法可以判断它是针对@b 标记表达式的,并且 @b 不是 在被应用的标签列表中?

谢谢!

【问题讨论】:

    标签: ruby testing cucumber bdd


    【解决方案1】:

    如果您标记了您的功能/场景...

    @some_tag
    Feature: My feature
    
      @some_other_tag
      Scenario: My scenario
        Given ......
    

    当给定的钩子被...应用时,您可以检索标签名称列表

    Around('@some_tag') do |scenario|
      tags = scenario.source_tag_names
    end
    

    这将返回一个数组中的标签名称列表

    $ tags
    => ['@some_tag', '@some_other_tag']
    

    场景执行时调用的标签,位于 Cucumber::Cli::Options 对象中。我找到了找回它的方法,但是发布它有点伤我的灵魂......

    如果您使用 cucumber -t @some_tag 运行上述代码,则可以通过执行以下操作来获取钩子中的标签...(尽管,我相信一定有更好的方法)

    Around('@some_tag') do |scenario|
      tags = scenario.source_tag_names
      options = Array.new puts
      executed_tag = options.last.config.tag_expressions
    end
    
    $ tags
    => ['@some_tag', '@some_other_tag']
    $ executed_tag
    => ['@some_tag']
    

    希望这会有所帮助。

    【讨论】:

    • 我明白你所说的灵魂痛苦是什么意思! :-O 这对我来说是一个巨大的飞跃,尽管我仍然遇到多个Around() stanzata 的问题。我正在为-t 命令行选项使用@test_instance@test_module 值,但是如果一个场景同时标记了两者,那么选择执行哪个Around() 节是很奇怪的。而且我不认为可以在一个钩子中多次调用场景块。 :-/
    猜你喜欢
    • 1970-01-01
    • 2018-09-23
    • 2017-04-17
    • 2021-06-09
    • 2022-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多