Nunit 如何使用BDD构造CRUD测试

Nunit 如何使用BDD构造CRUD测试,nunit,automated-tests,bdd,crud,specflow,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

我在试图找出构建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,当然在运行时会崩溃和燃烧

我尝试在场景名称的开头添加字符,结果出现了类似“场景:第一阶段创建…”、“场景:第二阶段读取…”之类的情况。但当我做出这一改变时,我忍不住想这感觉有多么“黑客”。我已经做了一些研究,但大部分都没有找到更好的方法来控制执行顺序

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

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


因此,我建议使用一个场景,或者,如果需要单独的场景,只需将它们独立于执行顺序即可
例如,对于删除场景,您应该在此场景中执行CRU先决条件,然后执行带有验证的删除步骤。
有关最佳实践(IMHO)-请参阅Kent Beck文章:

将完整的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
您绝对不能也不应该依赖场景的执行顺序。你迟早会遇到麻烦的(至少我遇到过)


然而,经常发生的情况是,一个功能文件只包含一个场景。:-)

经过一番内部辩论后,我决定尝试减少我已经写入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语法,但您应该知道,在保持各个场景的所有精度和可读性的同时,它执行得非常好