Preserve variable in cucumber?

后端 未结 1 629
天涯浪人
天涯浪人 2021-02-19 16:48

I want to access variables in difference Given/Then/When clauses. How to preserve variables so that they are accessible everywhere?

Given(#something) do
  foo =         


        
1条回答
  •  一整个雨季
    2021-02-19 17:45

    To share variables across step definitions, you need to use instance or global variables.

    Instance variables can be used when you need to share data across step definitions but only for the one test (ie the variables are cleared after each scenario). Instance variables start with a @.

    Given(#something) do
      @foo = 123
    end
    
    Then(#something) do
      p @foo
      #=> 123
    end
    

    If you want to share a variable across all scenarios, you could use a global variable, which start with a $.

    Given(#something) do
      $foo = 123
    end
    
    Then(#something) do
      p $foo
      #=> 123
    end
    

    Note: It is usually recommended not to share variables between steps/scenarios as it creates coupling.

    0 讨论(0)
提交回复
热议问题