2014-01-14 59 views
0

我提出了一个类似的问题,但措辞严重,因此没有真正得到我以前的答案。这是另一种尝试:在定义Gherkin Given和When语句时避免代码重复

所以我很欣赏黄瓜的嫩黄给出的声明类似于测试用例的前提条件。我很欣赏有些人认为这些不应该涉及用户交互,但为了这个问题,我会不同意这种观点。

这里有三种情况:

Scenario: Test a song can be played 
Given I setup a new account and default user 
When I add a "2nd" user 
And the "2nd" user starts playing a song 
Then I should see a song is playing 

Scenario: Test a playing song being stopped (version A) 
Given I setup a new account and default user 
And I add a "2nd" user 
And the "2nd" user starts playing a song 
When the "2nd" user stops playing a song 
Then I should see a song is not playing 

Scenario: Test a playing song being stopped (version B) 
Given a "2nd" user is playing a song 
When the "2nd" user stops playing a song 
Then I should see a song is not playing 

所以我很感激以上版本B是不是版本A的商业用户好点。然而,从一个代码复用点肯定版本B的需要Given语句重复第一个场景中大多数场景中使用的代码?

干杯,

查理

回答

1

因此版本B是一个去。

如果给定的步骤定义(例如版本B)具有由其他步骤定义(例如第1个场景)中涵盖的步骤组成的操作,则只需在步骤定义中创建私有方法文件)Given和When语句都可以根据需要调用。这取消了复制和粘贴代码的需要:-)

+0

你是对的,重新使用这些步骤在编写场景时是必不可少的。我猜你还没有听说过页面对象模式?这种模式将所有可以在页面上执行的动作封装到它自己的类中。使用这种模式将极大地帮助你。看到这篇文章将使问题更清晰http://blog.josephwilk.net/cucumber/page-object-pattern.html –

+1

嗨新鲜。是的,我正在使用相当于POM,因为我正在测试服务而不是浏览器对象。然而,我仍然不赞同为每一步复制和粘贴3行左右的代码,因此将私有方法分开,其中的POM代码是一个额外的提取层,但值得一提:-) –

+0

啊我明白。是的,你在做什么很有意义。 –