17

I have a problem with SpecFlow. We are using Azure Devops and when I build solution on my local machine it works perfectly but during Azure Devops build I am getting following error:

[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" />

I have tried using it with nUnit, MsTest, xUnit and it alway fails. Target framework: .NET Core 3.1. Build agent: vs2019-win2019

cashmere
  • 885
  • 3
  • 12
  • 37
aj_2017
  • 171
  • 1
  • 1
  • 5

8 Answers8

15

We've encountered same problem, after longer digging I found that there is an issue created for this in their git repo https://github.com/SpecFlowOSS/SpecFlow/issues/1912. Quoting from git issue:

MSBuild 16.5 has a new assembly loading approach for tasks that may have side effects. (which has been an ask for years, but it behaves a bit differently when loading dependencies) You can try to set MSBUILDSINGLELOADCONTEXT=1 (env var) to get the old behavior.

Try to set it in your build pipeline as:

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

This helped in our case.


EDIT: This issue seems to be fixed in newer version of SpecFlow (3.3.15+).

ESG
  • 8,988
  • 3
  • 35
  • 52
jurcheese
  • 151
  • 4
5

Had the same problem for SpecFlow 3.4.8 after migration from SpecFlow 2.

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

so i have removed SpecFlow.Tools.MSBuild.Generation package, however issue still there.

After that i have added specflow.json to test project and issue gone

kk.
  • 3,747
  • 12
  • 36
  • 67
4

Had the same problem, turned out that it was version issues between .net core SDK and Specflow. We downgraded .net core sdk to 3.1.102 and that helped

3

Uninstalling and reinstalling specflow and SpecFlow.Tools.MsBuild.Generation worked for me

Jack Burke
  • 101
  • 1
  • 6
1

I had this issue after specflow runner installation. Removing it solved the problem

Bohdan
  • 133
  • 1
  • 11
0

The error message says:

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

You have SpecFlow v3.1.86 installed. I would expect it to be loading TechTalk.SpecFlow 3.1.86, which matches the NuGet package version.

Either:

  • Uninstall and reinstall the SpecFlow package using the Package Manager Console:

    uninstall-package SpecFlow -force -project NameOfYourTestProject
    install-package SpecFlow -version 3.1.86 -project NameOfYourTestProject
    
  • Edit the .csproj file for your test project and ensure the <Reference> element importing the DLL file is pointing to the correct file

    <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>
    

    Note: Replace FRAMEWORK_VERSION with net45, netstandard2.0, etc depending on the .NET framework version of your test project.

You can always open Windows File Explorer and browse to the DLL file in order to discover the correct path.

Last ditch effort, try restoring NuGet packages manually from the command line, assuming you have NuGet.exe available in your %PATH% (See https://learn.microsoft.com/en-us/nuget/install-nuget-client-tools for more info)

nuget restore Path/To/YourSolution.sln
Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92
0

So glad this post exists. The tests would run on my machine but not in azure pipeline.

I upgraded to the latest specflow and SpecFlow.NUnit and this solved the issue ... updated from 3.7.13 to 3.9.8. I don't understand but glad it worked.

Fractal
  • 1,748
  • 5
  • 26
  • 46
0

Deleting this Specflow legacy option from app.config helped me to sort this error.

  <specFlow>
    <unitTestProvider name="MsTest"/>
  </specFlow>
CharithJ
  • 46,289
  • 20
  • 116
  • 131