【问题标题】:Unable to bind my feature to Steps using Specflow with JetBrains Rider无法将我的功能绑定到使用 Specflow 和 JetBrains Rider 的步骤
【发布时间】:2019-06-22 12:53:39
【问题描述】:

按照我在网上找到的一些指导,我尝试使用 Specflow 设置 JetBrains Rider:

...感谢肯提供的文档。

但是,我无法让我的方案步骤链接到我的任何步骤文件。


设置

我相信我已经为最新版本的 SpecFlow 安装了所有必需的 NuGet 包

NuGet installed Specflow packages screenshot


我习惯了 Cucumber 和 IntelliJ,我们也让 SpecFlow C# Visual Studio 工作,但我无法让场景连接到 Rider 中的步骤。

NB - 我尝试在 Rider 中使用的项目正在使用 Specflow 在 Visual Studio 中工作。

还有其他人能够赢得这场战斗吗?

我很想听听如何。

谢谢


更新 @肯 感谢您的建议。

我尝试了以下两种方法:

  1. 在 .csproj 文件中为 feature.cs 手动添加了 include 操作,但在构建后仍然无法从功能中执行步骤。
  2. 包含一个 Scope(Feature="") 属性

但不幸的是,没有运气。

如果这不能解决您的问题,您能否发布您的 .feature 和 .steps.cs 文件的内容。

按照建议,以下是在 VS 中正确映射的功能和 step.cs 文件的内容:

.feature

Feature: sampleFeature
  In order to avoid silly mistakes
  As a math idiot
  I want to be told the sum of two numbers

@mytag
Scenario: Add two numbers
    Given I have entered 50 into the calculator
    And I have entered 70 into the calculator
    When I press add
    Then the result should be 120 on the screen

.steps

using System;
using TechTalk.SpecFlow;

namespace SpecFlowPoc.features.sample
{
    [Binding, Scope(Feature="sampleFeature")]
    public class SampleFeatureSteps
    {
        [Given(@"I have entered (.*) into the calculator")]
        public void GivenIHaveEnteredIntoTheCalculator(int p0)
        {
            ScenarioContext.Current.Pending();
        }

        [When(@"I press add")]
        public void WhenIPressAdd()
        {
            ScenarioContext.Current.Pending();
        }

        [Then(@"the result should be (.*) on the screen")]
        public void ThenTheResultShouldBeOnTheScreen(int p0)
        {
            ScenarioContext.Current.Pending();
        }
    }
}

谢谢


更新 - 已解决

好的,首先,感谢Ken 的帮助和指导。 按照 Ken 提供的步骤,创建一个新项目并抛出异常后,我可以确认 .feature 到 step.cs 绑定有效。

Ken,你是一位绅士和天才。谢谢。

其次,我错误地认为 Rider 会为我提供一种从 .feature 导航到 Steps.cs 代码(Cucumber JVM 风格)的方法。我现在了解到 Rider 尚不支持此功能。

  • 这就是为什么我认为绑定不起作用!呵呵。

如果有人找到将 Rider gherkin 映射到小黄瓜库的插件,我很想听听。


【问题讨论】:

  • 这很奇怪。我创建了一个 Sample.feature 文件,复制到您的内容中,构建后它向我展示了测试,我可以运行它并生成生成的步骤。然后我添加了一个步骤文件,复制到生成的内容中并将第一个ScenarioContext.Current.Pending(); 替换为throw new Exception("this is my exception");。当我运行时,它很好地抛出了异常。您的 SpecFlow 版本和 Rider 版本是什么?我使用 SpecFlow 3.0.220 和 Rider 2019.1.2。
  • 嗨,Ken,我认为我的 keyBoard 和 myChair(我)之间存在错误。在遵循您出色的说明和解释之后,我现在明白 Binding 可以工作,测试运行,并且没有 UI 方法可以从 .feature 到 steps。这很酷。太感谢了。你是个传奇。

标签: specflow rider


【解决方案1】:

我能想到的第一件事(我自己也做过多次)就是忘记 .steps.cs 文件中的[Binding] 属性。哦,您可能还想标记 [Scope(Feature="")] 属性,以避免歧义。

您可以做的另一件事(如果您使用 SpecFlow 3.0 或更高版本)是手动包含 .feature.cs 文件并查看是否可以解决您的问题。如果是这种情况,我会考虑检查 .csproj 文件是否包含正确的 .feature.cs 文件。

如果这不能解决您的问题,您能否发布您的 .feature.steps.cs 文件的内容。

编辑 我从头开始,这些是我采取的步骤:

  1. 创建新的解决方案
  2. 创建一个测试项目,选择NUnit作为测试框架
  3. 安装最新的 SpecFlow.NUnit 和 SpecFlow.Tools.MsBuild.Generation
  4. nuget 包(应该是 3.0.220 版)(这将自动安装正确的 SpecFlow nuget 包)
  5. 编辑 .csconfig 并添加
<Target Name="AfterUpdateFeatureFilesInProject">
    <!-- include any generated SpecFlow files in the compilation of the project if not included yet -->
    <ItemGroup>
        <Compile Include="**\*.feature.cs" Exclude="@(Compile)" />
    </ItemGroup>
</Target>
  1. 创建一个 .feature 文件并粘贴堆栈溢出问题中的内容
  2. 构建项目,这应该会在测试资源管理器中显示您的测试 运行测试以获取它们的定义
  3. 创建一个将定义放入的类(同样是 Binding 和 Scope 属性),从测试输出窗口复制这些定义
  4. 在给定方法中引发异常
  5. 重新运行测试,看看是否抛出异常

PS:你的秘密身份对我来说是安全的。 ;)

【讨论】:

  • 谢谢。按照建议提供更新。
  • 现在一切都清楚了,肯。我的声誉不足以注册一个可见的 PLUS 投票,但你肯定会得到一个帮助。谢谢。
  • 没问题,乐于助人。 :)
  • @KenBonny 感谢所有帮助。但我还有一个问题。您是从 .feature 文件还是 .feature.cs 文件运行测试? .feature.cs 文件是不可调试的,当我单击 .feature 文件时,我得到“请指定黄瓜项目的非空路径”。我不想启动项目中的所有测试只是为了调试一个,所以我真的想要启动特定的功能文件。
  • 我使用底部的8: Unit Tests 标签。它们在构建或Run All 后自动出现在那里。在那里我可以选择要运行的测试、在构建时设置自动运行以及启动/调试一个或多个测试。
猜你喜欢
  • 2018-08-15
  • 2020-08-19
  • 1970-01-01
  • 1970-01-01
  • 2021-07-25
  • 2021-07-13
  • 2020-08-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多