如何在specflow c#中使用MSTest Annotations?
我们可以在钩子中使用SpecFlowC#中的MSTest注释。钩子是事件绑定,用于在某些步骤添加更多自动化逻辑。例如,对于需要在特定场景之前运行的任何步骤。要在代码中引入钩子,我们必须添加[Binding]属性。
Hook具有全局访问权限。但是可以通过声明作用域绑定使其对功能和场景可用。可以使用标签过滤范围绑定。
SpecFlow+Runner限制
如果我们使用SpecFlow+Runner从多个线程执行测试,则After和Before钩子(例如BeforeTestRun和AfterTestRun)对每个线程仅运行一次。
钩子属性
下面列出了钩子属性-
BeforeTestRun/AfterTestRun-用于在完整测试执行之前/之后运行自动化逻辑。
BeforeFeature/AfterFeature-用于在单个功能执行之前/之后运行自动化逻辑。
BeforeScenario或Before/AfterScenario或After-这用于在单个场景或场景大纲执行之前/之后运行自动化逻辑。
BeforeScenarioBlock/AfterScenarioBlock-用于在单个场景块执行之前/之后运行自动化逻辑。(在When和Given步骤之间)
BeforeStep/AfterStep-这用于在单个场景步骤执行之前/之后运行自动化逻辑。
钩子执行顺序
类似类型的钩子,例如两个AfterScenario钩子,以随机顺序运行。要执行特定的序列,我们必须在hook属性中添加Order属性。
示例
[AfterScenario(Order = 1)] public void CloseBrowser() { //我们需要先执行这个方法... } [AfterScenario(Order = 2)] public void VerifySessionIdAfterBrowserClose() { //...所以我们要求这个方法在CloseBrowser之后执行 //方法运行 }
数字表示顺序,这意味着数字最小的钩子首先运行。如果省略该数字,则默认值为10000。依赖它而提及单个钩子的顺序并不是一个好习惯。
此外,如果抛出未处理的异常,则将跳过以下所有类似类型的钩子。为了防止这种情况,我们应该处理所有异常。