2014-04-29 58 views
3

我陷入了一个窘境,试图找出构建我的CRUD测试的方式最佳。在我的应用程序中,用户可以创建多种类型的“任务”。我目前实现看起来像下面这样:如何使用BDD构建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,这在运行时当然会崩溃和燃烧。

我试图在场景名称的开头添加字符,导致类似于“场景:阶段1创建...”,“场景:阶段2读取...”等内容。但是,当我做出这个改变时,我忍不住想到它的感觉如何“黑暗”。我已经做了一些研究,但大部分都是用空白来更好地控制执行顺序。

我确实有多个这些CRUD测试要写入;每种类型的“任务”都有一个,我想知道是否最好将整个CRUD堆栈压缩到一个场景中,这样我就不必担心执行顺序,还是有更好的方法来控制执行?

回答

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语法,但你应该知道这个执行完美的罚款,同时保持单个方案的所有精度和可读性。

5

依靠场景的执行顺序是反模式,应该避免。出于同样的原因,测试运动员通常不会提供任何控制执行顺序的机制。这也违背了可执行规范的概念:该方案应该是可以理解的(并且可执行)。
来源:Specflow - State between "scenarios"

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

3

将完整的CRUD序列放入一个场景中。在情景级别选择智能场景名称和富有表现力的目标,但要保持单个CRUD序列的外观。我对这个原则做了很好的体验,即使对我来说也很难。

确保场景在执行后让您的“待测系统”保持干净并“尽可能不变”。

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

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 

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

然而,很多情况下,功能文件只能保存一个场景。:-)