【问题标题】:Specflow - GenerateFeatureFileCodeBehindTask has failed unexpectedlySpecflow - GenerateFeatureFileCodeBehindTask 意外失败
【发布时间】:2020-06-30 11:35:22
【问题描述】:

我对 SpecFlow 有疑问。我们正在使用 Azure Devops,当我在本地计算机上构建解决方案时,它运行良好,但在 Azure Devops 构建期间出现以下错误:

[error]C:\Windows\ServiceProfiles\NetworkService\.nuget\packages\specflow.tools.msbuild.generation\3.1.86\build\SpecFlow.Tools.MsBuild.Generation.targets(93,5): Error MSB4018: The "GenerateFeatureFileCodeBehindTask" task failed unexpectedly.
System.IO.FileNotFoundException: Could not load file or assembly 'TechTalk.SpecFlow, Version=3.1.0.0, Culture=neutral, PublicKeyToken=0778194805d6db41'. The system cannot find the file specified.
File name: 'TechTalk.SpecFlow, Version=3.1.0.0, Culture=neutral, PublicKeyToken=0778194805d6db41'
   at System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
   at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
   at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(MetadataToken caCtorToken, MetadataImport& scope, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, ListBuilder`1& derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& ctorHasParameters, Boolean& isVarArg)
   at System.Reflection.CustomAttribute.AddCustomAttributes(ListBuilder`1& attributes, RuntimeModule decoratedModule, Int32 decoratedMetadataToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, ListBuilder`1 derivedAttributes)
   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType)
   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeAssembly assembly, RuntimeType caType)
   at System.Reflection.RuntimeAssembly.GetCustomAttributes(Type attributeType, Boolean inherit)
   at System.Attribute.GetCustomAttributes(Assembly element, Type attributeType, Boolean inherit)
   at System.Attribute.GetCustomAttribute(Assembly element, Type attributeType, Boolean inherit)
   at System.Attribute.GetCustomAttribute(Assembly element, Type attributeType)
   at TechTalk.SpecFlow.Generator.Plugins.GeneratorPluginLoader.LoadPlugin(PluginDescriptor pluginDescriptor)
   at TechTalk.SpecFlow.Generator.GeneratorContainerBuilder.LoadPlugins(ObjectContainer container, GeneratorPluginEvents generatorPluginEvents, UnitTestProviderConfiguration unitTestProviderConfiguration, IEnumerable`1 generatorPlugins)
   at TechTalk.SpecFlow.Generator.GeneratorContainerBuilder.CreateContainer(SpecFlowConfigurationHolder configurationHolder, ProjectSettings projectSettings, IEnumerable`1 generatorPluginInfos, IObjectContainer parentObjectContainer)
   at SpecFlow.Tools.MsBuild.Generation.WrappedGeneratorContainerBuilder.BuildGeneratorContainer(SpecFlowConfigurationHolder specFlowConfigurationHolder, ProjectSettings projectSettings, IReadOnlyCollection`1 generatorPluginInfos, IObjectContainer rootObjectContainer)
   at SpecFlow.Tools.MsBuild.Generation.GenerateFeatureFileCodeBehindTaskExecutor.Execute()
   at SpecFlow.Tools.MsBuild.Generation.GenerateFeatureFileCodeBehindTask.Execute()
   at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
   at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask)
        <PackageReference Include="FluentAssertions" Version="5.10.2" />
        <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
        <PackageReference Include="MSTest.TestAdapter" Version="2.1.0" />
        <PackageReference Include="MSTest.TestFramework" Version="2.1.0" />
        <PackageReference Include="NUnit" Version="3.12.0" />
        <PackageReference Include="Selenium.Support" Version="3.141.0" />
        <PackageReference Include="Selenium.WebDriver" Version="3.141.0" />
        <PackageReference Include="SpecFlow" Version="3.1.86" />
        <PackageReference Include="SpecFlow.MsTest" Version="3.1.86" />
        <PackageReference Include="SpecFlow.Tools.MsBuild.Generation" Version="3.1.86" />

我尝试将它与nUnitMsTestxUnit 一起使用,但总是失败。 目标框架:.NET Core 3.1。 构建代理:vs2019-win2019

【问题讨论】:

  • 你能把整个错误信息贴出来吗?
  • 您使用的是哪个版本的 SpecFlow NuGet 包?
  • @GregBurghardt 我正在使用 3.1.86 版本的 Specflow NuGet,我尝试过使用多个版本的 Specflow NuGet 包
  • 嗨,这个案子怎么样?以下解决方案是否解决了问题?
  • @LeviLu-MSFT - 实际上,建议的解决方案没有任何效果。我们创建了删除 SpecFlow.Tools.MsBuild.Generation 文件的任务,然后再继续在 Azure Devops 上进行构建。

标签: selenium azure-devops nunit specflow azure-automation


【解决方案1】:

我们遇到了同样的问题,经过更长时间的挖掘,我发现在他们的 git repo https://github.com/SpecFlowOSS/SpecFlow/issues/1912 中为此创建了一个问题。 引用 git 问题:

MSBuild 16.5 为可能有副作用的任务提供了一种新的程序集加载方法。 (多年来一直是一个问题,但在加载依赖项时它的行为有点不同) 您可以尝试设置 MSBUILDSINGLELOADCONTEXT=1 (env var) 以获取旧行为。

尝试在您的构建管道中将其设置为:

 variables:
    ...
    MSBUILDSINGLELOADCONTEXT: '1'
    ...

这对我们的案例很有帮助。


编辑: 此问题似乎已在较新版本的 SpecFlow (3.3.15+) 中得到修复。

【讨论】:

  • 你如何在管道中设置它,把这个变量放在哪里?我没有看到任何可以将其放入 .NET Core 构建任务的地方。
  • 你把它放在 azure-pipeline.yml 文件中。
  • 帮我修好了。
【解决方案2】:

遇到了同样的问题,原来是.net core SDK和Specflow之间的版本问题。我们将 .net core sdk 降级到 3.1.102,这很有帮助

【讨论】:

    【解决方案3】:

    从 SpecFlow 2 迁移后 SpecFlow 3.4.8 遇到了同样的问题。

    After version SpecFlow 3.3.30 don’t need to add the SpecFlow.Tools.MSBuild.Generation package anymore

    所以我已经删除了SpecFlow.Tools.MSBuild.Generation 包,但是问题仍然存在。

    之后我添加了specflow.json 来测试项目并且问题消失了

    【讨论】:

    • 这太棒了!不知道为什么 dotnet new specflow 没有最新版本。
    【解决方案4】:

    卸载和重新安装 specflow 和 SpecFlow.Tools.MsBuild.Generation 对我有用

    【讨论】:

      【解决方案5】:

      specflow runner 安装后我遇到了这个问题。删除它解决了问题

      【讨论】:

        【解决方案6】:

        错误信息说:

        无法加载文件或程序集“TechTalk.SpecFlow,版本=3.1.0.0,文化=中性,PublicKeyToken=0778194805d6db41”。该系统找不到指定的文件。 文件名:'TechTalk.SpecFlow,版本=3.1.0.0

        您已安装 SpecFlow v3.1.86。我希望它加载与 NuGet 包版本匹配的 TechTalk.SpecFlow 3.1.86。

        要么:

        • 使用包管理器控制台卸载并重新安装 SpecFlow 包:

          uninstall-package SpecFlow -force -project NameOfYourTestProject
          install-package SpecFlow -version 3.1.86 -project NameOfYourTestProject
          
        • 为您的测试项目编辑 .csproj 文件并确保导入 DLL 文件的 &lt;Reference&gt; 元素指向正确的文件

          <Reference Include="TechTalk.SpecFlow, Version=3.0.0.0, Culture=neutral, PublicKeyToken=..., processorArchitecture=MSIL">
            <HintPath>..\packages\SpecFlow.3.1.86\lib\FRAMEWORK_VERSION\TechTalk.SpecFlow.dll</HintPath>
          </Reference>
          

          注意:将 FRAMEWORK_VERSION 替换为 net45、netstandard2.0 等,具体取决于您的测试项目的 .NET 框架版本。

        您始终可以打开 Windows 文件资源管理器并浏览到 DLL 文件以找到正确的路径。

        最后的努力,尝试从命令行手动恢复 NuGet 包,假设您的 %PATH% 中有 NuGet.exe(请参阅 https://docs.microsoft.com/en-us/nuget/install-nuget-client-tools 了解更多信息)

        nuget restore Path/To/YourSolution.sln
        

        【讨论】:

          【解决方案7】:

          很高兴这篇文章存在。测试将在我的机器上运行,但不会在 Azure 管道中运行。

          我升级到了最新的 specflow 和 SpecFlow.NUnit,这解决了这个问题......从 3.7.13 更新到 3.9.8。我不明白,但很高兴它起作用了。

          【讨论】:

            猜你喜欢
            • 2020-11-10
            • 1970-01-01
            • 2021-11-09
            • 2013-09-18
            • 1970-01-01
            • 1970-01-01
            • 2021-12-04
            • 2018-01-02
            • 2017-09-20
            相关资源
            最近更新 更多