0

对于初学者:我一般是 Visual Studio 和 SpecFlow 的新手。

根据 SpecFlow 的入门文档,根据我开始的项目类型,我会得到 2 种不同的结果。

单元测试项目:在这种类型的项目中,运行者根本无法识别测试。只有“SpecRun 评估”测试运行。

MSTest 测试项目(.NET Core):在这种类型的项目中,测试总是通过,但我在测试输出中确实收到错误:“值不能为空。参数名称:消息”,这是由“生成器”引起的

我在典型的 Windows 10 Pro 上完成所有这些工作。我在 VS 2017 和 2019 上都试过了。我试过重新做整个过程,以防我错过了什么。还尝试更新软件包。可能应该注意的一点是,单元测试项目不会生成 App.config 文件。

using System;
using TechTalk.SpecFlow;

namespace Tests.Steps
{
    [Binding]
    public class CalculatorSteps
    {
        [Given(@"I have entered (.*) into the calculator")]
        public void GivenIHaveEnteredIntoTheCalculator(int p0)
        {
            Console.WriteLine(p0);
        }

        [Given(@"I have also entered (.*) into the calculator")]
        public void GivenIHaveAlsoEnteredIntoTheCalculator(int p0)
        {
            Console.WriteLine(p0);
        }

        [When(@"I press add")]
        public void WhenIPressAdd()
        {
            Console.WriteLine("add pressed");
        }


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

    }
}

当我故意在测试中抛出异常时,我非常希望测试能够运行并获得某种反馈。

所有这一切都只是工作申请的一项任务,但我提供的是生成的默认功能文件,因为两种情况都有这样的结果。我已经知道如何完成任务,在这里我被困在设置上几个小时......我真的希望这只是我的愚蠢/缺乏经验。

.csproj:https ://codeshare.io/5zxk0W

4

1 回答 1

3

此答案适用于 SpecFlow 2.*。使用 SpecFlow 3,您只需将SpecFlow.Tools.MSBuild.GenerationNuGet 包添加到您的项目中。

您错过了启用 MSBuild 集成的一步。

您必须将以下代码放在标签中 csproj 的末尾。

<Target Name="AfterUpdateFeatureFilesInProject">
    <ItemGroup>
    <Compile Include="**\*.feature.cs" Exclude="@(Compile)" />
    </ItemGroup>
</Target>

它在https://specflow.org/2019/generating-code-behind-files-using-msbuild/ - 在生成经典项目系统后启用 MSBuild 代码 - 步骤 2中进行了描述

于 2019-04-04T09:14:55.040 回答