【问题标题】:Can you tag individual examples in a scenario outline in SpecFlow?您可以在 SpecFlow 的场景大纲中标记各个示例吗?
【发布时间】:2020-01-19 09:10:26
【问题描述】:

场景大纲对于创建数据驱动的测试非常方便,但是场景的数量会随着示例的数量而增加。我已经养成了标记场景的习惯,以便更容易过滤我们应用程序的主要功能。

我想设置一个适用于所有主要用例的“冒烟测试”。其中一些用例是在对日期或数字执行边界测试的场景大纲中捕获的,但我只想在示例中找到一个原型案例。

例如,假设我们有一个功能允许我们在工作中添加职位空缺(基本上是“招聘机会”而不是“我们有温暖的身体填补这个职位”)。

在屏幕上,我们有两个最低经验的表单域:年和月。用户在月份字段中输入的时间不应超过 11 个月,否则他们应该在年份字段中输入一些内容(例如,18 个月实际上应该是 1 年零 6 个月)。

@job-openings
Scenario Outline: Adding a job opening with experience
    Given a job exists
    When I add a job opening requiring <years> years and <months> months experience
    Then a job opening should exist requiring <years> years and <months> months experience

Examples:
    | years | months |
    | 0     | 1      |
    | 0     | 11     |
    | 1     | 0      |
    | 2     | 6      | # <-- the "prototypical" example I want to tag
    | 99    | 0      |
    | 99    | 11     |
    | 100   | 0      |

从回归测试的角度来看,让这些示例达到几年和几个月的可接受值的界限肯定是有用的,但在执行系统的“冒烟测试”时则不然。最好在代表典型用例的场景大纲中运行一个示例。作为一些背景信息,我们有一个 PowerShell 脚本,开发人员可以使用它来运行各种自动化测试,并且针对所有主要功能的一般“冒烟测试”会很有用。

有没有办法在场景大纲中标记单个示例?

【问题讨论】:

    标签: specflow gherkin


    【解决方案1】:

    这就是我的做法:

    @job-openings
    Scenario Outline: Adding a job opening with experience
        Given a job exists
        When I add a job opening requiring <years> years and <months> months experience
        Then a job opening should exist requiring <years> years and <months> months experience
    
    @smoketest @regression
    Examples:
        | years | months |
        | 2     | 6      | # <-- the "prototypical" example I want to tag
    
    @regression
    Examples:
        | years | months |
        | 0     | 1      |
        | 0     | 11     |
        | 1     | 0      |
        | 99    | 0      |
        | 99    | 11     |
        | 100   | 0      |
    

    有两个示例部分都属于该场景。 Smoketest 有自己的示例部分。运行时

    dotnet test --filter "TestCategory=job-opening&TestCategory=smoketest"
    

    它只会运行带有smoketest 标签的示例。运行时

    dotnet test --filter "TestCategory=job-opening&TestCategory=regression"
    

    它将运行所有示例。它也会运行smoketest,因为它也有回归标签。

    user1207289 的方法也有效。当测试中断并且我想稍后重新测试时,我有时会这样做。生成测试后,您要运行的特定示例将获得一个名称(例如,AddingAJob_ExampleYears2Months6)。您可以在带有-t 标志的场景中找到生成的单元测试的名称,该标志列出了所有测试:

    dotnet test --filter "TestCategory=job-opening" -t
    

    要运行一项特定的测试(从技术上讲,所有测试名称中都带有 AddingAJob_ExampleYears2Months6):

    dotnet test --filter AddingAJob_ExampleYears2Months6
    

    我在上面的示例中使用了官方的 dotnet cli 工具,但对于其他测试运行器来说非常相似。

    【讨论】:

    • 对于使用vstest.console的人使用yourTest.exe --testcasefilter:TestCategory=regression
    • 完美运行,谢谢!在 Visual Studio 测试资源管理器中使用 trait:smoketest 进行过滤也同样有效。
    【解决方案2】:

    我可以通过以下命令从场景大纲中运行单个示例

    C:\Users\..\..\bin\Debug&gt;"C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\Extensions\TestPlatform\vstest.console.exe" yourTests.exe /Tests:yourTestName

    其中yourTestName 是构建时在测试资源管理器中生成的测试名称,yourTests.exe/bin/debug 中生成的exe。我正在使用 MSTest

    有关生成名称的更多信息,请查看here

    【讨论】:

      猜你喜欢
      • 2014-10-12
      • 1970-01-01
      • 2018-03-10
      • 2017-03-28
      • 2021-04-29
      • 2016-01-25
      • 1970-01-01
      • 2013-01-22
      • 1970-01-01
      相关资源
      最近更新 更多