BDD 适当的给定设置
BDD Proper Given Setup
当前公司决定切换到使用 selenium、PHP 和 Behat 的 BDD,根据我对使用 Given 的小黄瓜语言的理解,这是 'pre-condition' 或场景的上下文,但是如果它超出了简单的条件,我对实现感到困惑:我在登录页面上。
所以如果我有一个像这样的功能文件:
Feature: Sign up for an account
Scenario: Register using a temporary email address
Given I register a temporary email address at https://getnada.com/
And I register an account
And I click the link in my email to confirm new account
When I complete the registration process by setting a password
Then I should be on the User Profile page
我感到困惑的是,在某些情况下,使用 Given,您可以在该步骤中启动网络浏览器,而我见过的其他示例,人们在给定语句下有明确的数据。
我不确定我是否应该自动执行给定的步骤,所以如果我 运行 相同的脚本,它可以获取一个新的电子邮件地址
或
我应该刚刚完成给定的步骤并明确传递电子邮件地址和注册信息,因为这些信息已经被假定为已提供?
没关系,它们只是关键字,可帮助您更好地了解场景的各个部分并帮助您理解场景。
Given some initial context
When I do something
Then result
在给定的情况下,您可以进行场景设置或只打开一个页面。
示例:
鉴于我有一个新帐户
鉴于我有一个无效的电子邮件,如 test@mail
鉴于我在主页上
You need to remember the focus of the scenario
Given I am on register page
When I fill register form with valid info
And I submit the register form
Then I should be on the User Profile page
Given I have a new account
When I login with the new account
Then I should be on the User Profile page
第一个场景是注册,重点是填写注册表单并提交,如果你需要也可以在填写步骤中使用一些数据。
第二种情况是登录,你专注于登录,Given包含了一步下的注册。
If something seems odd or to hard then you are doing something wrong
If the scenario is too complex then you are missing some points
The scenario should be easy to read and to understand without being lost in the details
Write is as natural as possible as you would do for a manual test
Finally, I recommend to read a book about BDD like BDD in action
场景上下文包含场景开始前发生的事情。一个简单的方法就是将它们置于过去时或连续现在时中。所以这些都是上下文:
Fred bought a microwave for £100
I have an account with Whosebug
Luke has the winning lottery ticket
I saved my document as "LostDog.docx"
Givens真的应该是相互独立的;它们发生的顺序无关紧要。(为了可读性,请随意妥协此规则。)
在您的场景中,两个 Givens 导致下一个 Given:
Given I register a temporary email address at https://getnada.com/
And I register an account
And I click the link in my email to confirm new account
所以可以用一个代表 end-state 的 Given 来概括它。
Given I've clicked to confirm a new account with a getnada.com email address
从场景来看,您可能有兴趣练习与单击 link 和完成注册相关的所有行为。如果是这样的话,我会改写如下:
Given I've requested a new account with a getnada.com email address
When I finish the registration by setting a password...
在任何一种情况下,那些较大的步骤都可以包含较小的步骤。
请注意,在 Givens 的情况下,您如何到达那里并不重要。也许你真的给自己发了一封电子邮件。也许你刚刚使用了 API。也许您只是将相关数据直接放入数据库(小心,这可能有点脆弱)。在这种情况下,您没有行使系统的行为。
如果你确实想练习任何行为,它会出现在 When 而不是 Given 中。
如果您与理解您要解决的问题的人交谈,并请他们举例说明问题应该如何工作,他们通常会相当有效地为您总结这些内容。这是对话成为 BDD 非常重要的一部分的另一个原因。
另请参阅页面对象模式,这将有助于在执行此操作时保持步骤的可维护性。
当前公司决定切换到使用 selenium、PHP 和 Behat 的 BDD,根据我对使用 Given 的小黄瓜语言的理解,这是 'pre-condition' 或场景的上下文,但是如果它超出了简单的条件,我对实现感到困惑:我在登录页面上。
所以如果我有一个像这样的功能文件:
Feature: Sign up for an account
Scenario: Register using a temporary email address
Given I register a temporary email address at https://getnada.com/
And I register an account
And I click the link in my email to confirm new account
When I complete the registration process by setting a password
Then I should be on the User Profile page
我感到困惑的是,在某些情况下,使用 Given,您可以在该步骤中启动网络浏览器,而我见过的其他示例,人们在给定语句下有明确的数据。
我不确定我是否应该自动执行给定的步骤,所以如果我 运行 相同的脚本,它可以获取一个新的电子邮件地址
或
我应该刚刚完成给定的步骤并明确传递电子邮件地址和注册信息,因为这些信息已经被假定为已提供?
没关系,它们只是关键字,可帮助您更好地了解场景的各个部分并帮助您理解场景。
Given some initial context
When I do something
Then result
在给定的情况下,您可以进行场景设置或只打开一个页面。
示例:
鉴于我有一个新帐户
鉴于我有一个无效的电子邮件,如 test@mail
鉴于我在主页上
You need to remember the focus of the scenario
Given I am on register page
When I fill register form with valid info
And I submit the register form
Then I should be on the User Profile page
Given I have a new account
When I login with the new account
Then I should be on the User Profile page
第一个场景是注册,重点是填写注册表单并提交,如果你需要也可以在填写步骤中使用一些数据。
第二种情况是登录,你专注于登录,Given包含了一步下的注册。
If something seems odd or to hard then you are doing something wrong
If the scenario is too complex then you are missing some points
The scenario should be easy to read and to understand without being lost in the details
Write is as natural as possible as you would do for a manual test
Finally, I recommend to read a book about BDD like BDD in action
场景上下文包含场景开始前发生的事情。一个简单的方法就是将它们置于过去时或连续现在时中。所以这些都是上下文:
Fred bought a microwave for £100
I have an account with Whosebug
Luke has the winning lottery ticket
I saved my document as "LostDog.docx"
Givens真的应该是相互独立的;它们发生的顺序无关紧要。(为了可读性,请随意妥协此规则。)
在您的场景中,两个 Givens 导致下一个 Given:
Given I register a temporary email address at https://getnada.com/
And I register an account
And I click the link in my email to confirm new account
所以可以用一个代表 end-state 的 Given 来概括它。
Given I've clicked to confirm a new account with a getnada.com email address
从场景来看,您可能有兴趣练习与单击 link 和完成注册相关的所有行为。如果是这样的话,我会改写如下:
Given I've requested a new account with a getnada.com email address
When I finish the registration by setting a password...
在任何一种情况下,那些较大的步骤都可以包含较小的步骤。
请注意,在 Givens 的情况下,您如何到达那里并不重要。也许你真的给自己发了一封电子邮件。也许你刚刚使用了 API。也许您只是将相关数据直接放入数据库(小心,这可能有点脆弱)。在这种情况下,您没有行使系统的行为。
如果你确实想练习任何行为,它会出现在 When 而不是 Given 中。
如果您与理解您要解决的问题的人交谈,并请他们举例说明问题应该如何工作,他们通常会相当有效地为您总结这些内容。这是对话成为 BDD 非常重要的一部分的另一个原因。
另请参阅页面对象模式,这将有助于在执行此操作时保持步骤的可维护性。