首页 > 解决方案 > Specflow - GenerateFeatureFileCodeBehindTask 意外失败

问题描述

我对 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" />

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

标签: seleniumazure-devopsnunitspecflowazure-automation

解决方案


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

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

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

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

这在我们的案例中有所帮助。


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


推荐阅读