【问题标题】:Specflow - Correct way to test grid dataSpecflow - 测试网格数据的正确方法
【发布时间】:2013-10-28 09:43:37
【问题描述】:

所以在我的应用程序中,我有一个网格,其中包含注册到站点的所有用户的列表。现在我想使用 specflow 和 WatiN 对其进行测试。

我想出了以下场景:

Scenario: List of users
    Given I am logged in as "Admin" user
    And There exists following users
        | Username | First name | Last Name | Registration date |
        | alice    | Alice      | LAlice    | 2013-10-28        |
        | bob      | Bob        | LBob      | 2013-10-27        |
    When I go to all users page
    Then There should be following users in table
        | Username | First name | Last Name | Registration date |
        | alice    | Alice      | LAlice    | 2013-10-28        |
        | bob      | Bob        | LBob      | 2013-10-27        |

问题是该表在步骤中重复,我不确定这是否是正确的方法。有没有更好的方法来测试数据是否加载到网格中?

【问题讨论】:

    标签: gridview datagrid watin specflow scenarios


    【解决方案1】:

    您可以以不同的方式进行此测试,尽管虽然以下方法会从您的场景中删除重复数据,但它不一定更有效,因为测试将单独检查每个用户。

    您可以做的是重新编写测试以利用 Specflow 的Scenario Outline 功能。那么如果你要重写你的场景如下,你只需要定义一次用户数据:

    Scenario Outline: List of users
     Given I am logged in as "Admin" user
     And There exists <Username> with <First name> <Last Name> and <Registration date>
     When I go to all users page
     Then There should be <Username> with <First name> <Last Name> and <Registration date>
    
       examples:
        | Username | First name | Last Name | Registration date |
        | alice    | Alice      | LAlice    | 2013-10-28        |
        | bob      | Bob        | LBob      | 2013-10-27        |
    

    【讨论】:

    • +1 这是查看此测试的一种非常有用的替代方法。正如您所说,它对于测试稍微不同的方面很有用:即“我可以在我的页面上呈现以下每个数据行吗” - 如果您担心诸如 unicode 格式或 html/javascript 注入之类的事情,那就太好了。如果测试要检查整个数据集的完整性或自动排序等内容,那么这不太合适。
    • @perfectist 是的,你是对的,很高兴你能欣赏这个答案。我的回答向您展示了如何从功能文件中删除重复数据,但正如您所说,您无法验证所有条目是否都出现在当前屏幕上。
    【解决方案2】:

    为了检查我是否正确理解了问题,您担心的是您在一个场景的 Given 和 Then 步骤中重复了表的定义(即,您没有运行 100 个都需要设置相同表的类似场景)。

    看起来你拥有的一切都很好。

    考虑一下如果 UI 需要进行少量数据转换会发生什么。 例如

    Given I am logged in as "Admin" user
    And There exists following users
        | Username | First name | Last Name | Registration date |
        | alice    | Alice      | Wonderland| 2013-10-28        |
        | bob      | Bobby      | Tables    | 2013-10-27        |
    When I go to all users page
    Then There should be following users in table
        | Username | Full Name        | Registration date |
        | alice    | Alice Wonderland | 2013-10-28        |
        | bob      | Bobby Tables     | 2013-10-27        |
    

    这种情况很清楚,您无法在此处节省任何空间。

    正如您所做的那样,通过准确地重复该表,您可以在您的测试/实时文档中明确说明您在页面上看到的内容应该与内存中的数据表示完全匹配。这值得在测试中表达出来。

    可以节省一些空间的其他选项:

    Given I am logged in as "Admin" user
    And there exists the following users
        | Username | First name | Last Name | Registration date |
        | alice    | Alice      | LAlice    | 2013-10-28        |
        | bob      | Bob        | LBob      | 2013-10-27        |
    When I go to all users page
    Then I should be able to see all the users
    

    这不是很清楚,需要设置 Then 步骤以与 Given 步骤共享数据,或者实际查询模拟数据上下文本身(一个非常糟糕的计划,因为测试可能会因为错误的原因通过) .

    如果你问是否有任何一行 C++ 的 #define 关键字来设置一个可重用的多行字符串 const - 据我所知,没有,如果有,你不应该使用它的原因上面的清晰度。

    总之 - 我喜欢你的测试。 :)

    ---- 编辑 ---- 事后思考:

    测试失败的原因只有一个。 可以说,您的测试是在测试“表格数据完全正确”。

    但有时您只是想测试一些更简单的东西。例如

    Then user 'alice' should appear in the users table
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-06
      • 2017-08-14
      • 2016-03-25
      • 1970-01-01
      • 1970-01-01
      • 2011-08-09
      相关资源
      最近更新 更多