Cucumber场景是否应包含特定的UI步骤?

时间:2011-12-07 15:39:51

标签: ruby testing cucumber automated-tests

大约一年前使用过Cucumber,我习惯用这种方式编写场景。

Scenario: user tries to vote on a comment
  Given I have a post
  And I have a comment on that post
  And I am logged in as a different user
  And I am on the post page
  When I click on "upvote" within the "comment"
  Then the comment score should raise
  And the comment author should get points

然而最近我发现,自从Cucumber 1.1以来,基本的步骤定义如

When I click on "upvote" within the "comment"

不再可用,因为它们被认为是一种不好的做法。我可以看出这是有道理的,但我现在不确定我应该如何抽象地编写我的功能。

想到的例子。

When I upvote on a comment
Then the comment should get points

或更不抽象

Given I have a post with a comment
And I'm not the author
When I upvote on the comment
Then the comment author should get points

甚至更不抽象

Given I have a post with a comment
And I'm not the author
When I upvote on the comment
Then the comment score should increase
And the comment author should get 1 point

Given部分

中非常具体
Given I have a post with a comment
And I'm not the author
And I am on the page for that post
When I upvote on the comment
Then the comment score should increase
And the comment author should get 1 point

我知道这是一个偏好的问题,但我想在这里寻求一些最佳实践。哪个是指定方案的最佳方式?

1 个答案:

答案 0 :(得分:1)

我喜欢制作更多的声明性场景,但不是很多,以至于不清楚UI的哪个部分涉及。我认为Cucumber团队在试图阻止第一种风格时,已经走得太远了,相反的极端。

我认为我最喜欢你的最后一个例子:Given通常应该非常具体。