在Specflow中,step和step Defination 一般是多对一的关系,也就是说同一个step Defination可以被step多次复用,区别只是step中的参数不同, 比如:
step Defination
[Then(@"I have see (.*) on UI")]
public void GivenIHaveSeeOnUI(string text)
{
}
多个step
Then I have see a button on UI
Then I have see a form on UI
但是,有时step和step Defination也需要做到一对多 ,这时可以使用Scope和Tag来进行局部绑定。
也就是说,在step上加上Tag,它只会调用对应scope的step Defination,形式如下:
step
@Button
Then I have see a button on UI
step Defination
[Then(@"I have see (.*) on UI")]
[Scope(Tag = "Button")]
public void GivenIHaveSeeOnUI(string text)
{
}
[Then(@"I have see (.*) on UI")]
[Scope(Tag = "Form")]
public void GivenIHaveSeeOnUI(string text)
{
}
这样,特点Tag的step只会和对应的Defination绑定。
尤其注意,项目中一些比较容易重复的step Defination如果不加Scope,或者Step的scenario上忘记加Tag,很容易造成Multiple bingding。
补充:
Scope除了可以限定Tag范围,也可以限定Feature和Scenario范围。比如:
针对scenario
Feature: Scope
Scenario: Dial in HK
Given I have dialed number 132098
Scenario: Dial in mainland
Given I have dialed number 132098
可以使用如下所示的Scope
[Scope(Scenario = "Dial in HK")]
[Scope(Scenario = "Dial in mainland")]
转自:https://blog.csdn.net/qq_42412416/article/details/80686748
原文:https://www.cnblogs.com/dongdongtest/p/15354635.html