如何使用BDD构建CRUD测试

Aus*_*tin 3 bdd nunit automated-tests 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
Run Code Online (Sandbox Code Playgroud)

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

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

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

dre*_*ets 5

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

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


rea*_*ime 5

将完整的 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
Run Code Online (Sandbox Code Playgroud)

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

然而,一个特征文件经常只包含一个场景。:-)