Preserve variable in cucumber?

时光总嘲笑我的痴心妄想 提交于 2019-12-21 07:20:07

问题


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 = 123 # I want to preserve foo
end

Then(#something) do
  # how to access foo at this point??? 
end

回答1:


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.



来源:https://stackoverflow.com/questions/18962439/preserve-variable-in-cucumber

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!