如何使用BDD构建CRUD测试

时间:2014-04-29 16:13:28

标签: nunit automated-tests bdd crud specflow

我陷入了困境,试图找出构建我的CRUD测试的最佳方式。在我的应用程序中,用户可以创建几种类型的“任务”。我目前的实现类似于以下内容:

Scenario:  Create Task-Type A
Given I am on a user's profile page
And Have access to create tasks
When I create a new task with a unique title and description
Then The confirmation prompt should display

Scenario:  Read the Task-Type A
Given A new task was created
When I search the text on the page for the unique title
Then I should find the task
And All the details of the task should match what was created

Scenario:  Update the Task-Type A
Given A task exists
And I have opened the edit dialog
When I make the following changes:
| title | description | date | save |
| ""    | ""          | ""   | yes  |
Then all the saved changes should match the task details

Scenario: Delete the Take-Type A
Given A task exist
When I select the option to delete
And I confirm deletion process
Then The Task should no longer exist in the list

我在这里寻求帮助的原因是因为我无法控制CRUD步骤的执行顺序。我正在使用Specflow和NUnit,它按字母顺序执行场景,而不是它们在特征文件中出现的顺序。这导致该顺序C> D> R> U,跑步时当然会崩溃并烧伤。

我尝试在场景名称的开头添加字符,从而产生类似“Scenario:Phase 1 Create ...”,“Scenario:Phase 2 Read ...”等内容。但是当我做出这个改变时,我忍不住想着它感觉到了什么'hack-ish'。我已经完成了一些研究,但大多数情况下都是空的,以更好的方式来控制执行顺序。

我确实要编写多个这些CRUD测试;每种类型的“任务”都有一个,我想知道将整个CRUD堆栈压缩成单个场景会更好吗所以我不必担心执行顺序或者是否有更好的方法来控制执行?

3 个答案:

答案 0 :(得分:5)

  

依赖于场景的执行顺序是一种反模式,应该避免。由于同样的原因,测试运行器通常不提供任何控制执行顺序的机制。它也违背了可执行规范的概念:该场景本身应该是可理解的(并且是可执行的)。
来源:Specflow - State between "scenarios"

所以,我建议使用一个场景,或者,如果你想要单独的场景,只需让它们独立于执行顺序。
例如,对于Delete场景,您应该在此场景中执行CRU前置条件,然后执行删除步骤并进行验证。
对于最佳实践(恕我直言) - 请参阅Kent Beck文章:https://www.facebook.com/notes/kent-beck/decompose-run-on-tests/555371804495688

答案 1 :(得分:3)

将完整的CRUD序列放在一个场景中。在场景级别选择智能场景名称和表达目标,但保持单个CRUD序列的自由度。我对这个原则有很好的体验,即使对我来说也很难。

确保Scenario让您的系统保持正常运行状态"在执行完毕后,尽可能保持不变和#34;

如果您想知道增加功能文件:阅读您的方案标题,我认为您有下一级别测试的步骤名称。这意味着:

Feature: Example
    Scenario: Process Task A
        Given I create Task A
        When I read Task A
        Then I update Task A
        Then I delete Task A

您绝对不能也不应该依赖于您的方案的执行顺序。迟早你会遇到麻烦(至少,我做过)。

然而,很多时候,一个特征文件只包含一个场景。 : - )

答案 2 :(得分:0)

经过一些内部辩论后,我认为尝试减少我已经写入BDD语法的测试是徒劳的:

    [scenario name]
    [pre-condition]
    [action]
    [observation] 

我最终得到的是这样的:

    [scenario name]
    [pre-condition]
    [action]
    [observation] 

    [pre-condition]
    [action]
    [observation] 

    ...
    [end]

这是原始代码的样子。

Scenario:  Create Task-Type A
Given I am on a user's profile page
And Have access to create tasks
When I create a new task with a unique title and description
Then The confirmation prompt should display

Given A new task was created
When I search the text on the page for the unique title
Then I should find the task
And All the details of the task should match what was created

Given A task exists
And I have opened the edit dialog
When I make the following changes:
| title | description | date | save |
| ""    | ""          | ""   | yes  |
Then all the saved changes should match the task details

Given A task exist
When I select the option to delete
And I confirm deletion process
Then The Task should no longer exist in the list

我确信有些人会不同意我的方法,因为它打破了BDD语法,但是你应该知道这一点非常精确,同时保持了各个场景的所有精确性以及可读性。

相关问题