在Specflow步骤中组织代码

时间:2014-08-15 17:43:34

标签: specflow

我从Techtalk了解到,对特征的耦合步骤定义是一种反模式。 但是我想知道如何组织我的步骤定义,以便我可以在代码中轻松地看到哪些步骤在一起。

例如,我应该为每个功能创建一个代码文件,为共享的步骤创建一个单独的文件吗?或者我应该有一组功能的代码文件吗?

1 个答案:

答案 0 :(得分:4)

我倾向于以几种方式组织步骤定义,具体取决于步骤定义文件的大小。

单独的数据和用户界面步骤

我有很多Given some thing exists in the database个步骤,所以我经常将它们放入一个文件调用DataSteps.cs中。我还有很多Then something exists in the database步骤,这些步骤进入DataAssertionSteps.cs。

我的所有When I fill in some form field步骤都在FormSteps.cs中。无论何时我需要Then something in the form is enabled|disabled或声明表单字段具有特定值,我都会将它们放入FormPresentationSteps.cs。

按型号分开步骤

有时我的步骤定义文件变得非常大,我开始将步骤定义移动到与某个模型相关的文件中。假设我有Person模型。我可能会创建一个分为三个区域的PersonSteps.cs文件:

[Binding]
public class PersonSteps
{
    #region Given
    // Given a person exists with the following attributes:

    #endregion

    #region When
    // When something something about a Person
    #endregion

    #region Then
    // Then a person should exist with the following attributes:
    #endregion
}

基本上我从这些文件开始:

  • DataSteps.cs - 用于设置测试数据的Given
  • DataAssertionSteps.cs - 用于声明数据在数据库中正确存在的Then
  • PresentationAssertionSteps.cs - 确保UI看起来像是
  • 的一般内容
  • FormSteps.cs - 操作表单的步骤
  • FormPresentationAssertionSteps.cs - 确保表单字段具有正确的值,正确启用/禁用。验证消息显示等。
  • GeneralSteps.cs - 一个名字不太好的catch-all,用于不适合上述文件的内容。也许我应该重命名它" AndIShallCallItSomethingManagerSteps.cs"?