Specflow测试步骤inheritance导致“不明确的步骤定义”

我想拥有以下测试步骤类结构:

[Binding] public class BaseStep { [Given(@"there is a customer")] public void GivenThereIsACustomer(Table table) { HandleCustomer(table); } protected virtual void HandleCustomer(Table table) { } } [Binding] public class FeatureOneStep : BaseStep { protected override void HandleCustomer(Table table) { // feature one action } [Given(@"feature one specific step")] public void GivenFeatureOneSpecificAction(Table table) { // do something } } [Binding] public class FeatureTwoStep : BaseStep { protected override void HandleCustomer(Table table) { // feature two action } [Given(@"feature two specific step")] public void GivenFeatureTwoSpecificAction(Table table) { // do something } } 

“鉴于有客户”是FeatureOne和FeatureTwo中使用的常见步骤,但它在两个function中将具有不同的处理逻辑。 因此,我决定将此步骤定义放入基类中,并分别覆盖两个派生类中的受保护方法。

但是,当我运行测试时,我有以下错误:

 TechTalk.SpecFlow.BindingException: Ambiguous step definitions found for step 'Given there is a customer': CustomerTestBase.GivenThereIsACustomer(Table), CustomerTestBase.GivenThereIsACustomer(Table) 

谁能告诉我如何解决这个问题?

现在我自己想出这个,所以有几个笔记(希望有人可以在将来使用它):

  • 不要在基类上包含[Binding]属性
  • 为每个要素文件创建派生类
    • 将[Binding]属性添加到派生类(将自动包括基类中的所有步骤定义)
    • 将[Scope]属性添加到派生类中; 指定命名参数Feature的要素名称

答案很简单; 不要使用inheritance来定义绑定。

在运行时,SpecFlow通过在所有公共类中全局扫描来查找其方法,以查找具有匹配[Given]属性的方法。 这意味着您不能拥有两个不同的实现。 Given there is a customer声明,如果您考虑它是一个非常明智的设计决策,将减少歧义。