在编写Scenario的时候,我们为了让场景描述足够简练,可能会隐含一些前置条件,比如下面这个场景
Feature: Hook
Scenario: Add goods to shopping car
Given I have searched for goods with keywords 'toothbrush'
And I have opened the first result
When I add it to shopping car
Then it should show the total price
第一步表示用户在页面上搜索关键字toothbrush,其实隐含了用户为已经登陆的状态。但这个登陆的过程虽然没有在场景中用Step显示描述,但是这个登陆的操作是不可缺少的。而如果将每个场景前都要加上表示登陆的Step,就难以突出场景描述的重点,也会导致场景步骤太多。为了解决这个问题,我们引入BeforeScenario。
添加Specflow Hooks
在给出的函数中编写登陆代码
[BeforeScenario]
public void BeforeScenario()
{
//TODO: implement logic that has to run before executing each scenario
}
这样,上面场景生成的case中就会先执行登陆,就达到了隐含该登陆过程的目的。
按照这样的思路&#