Specflow + MSTest:测试失败并出现错误 "Could not load file or assembly 'MyPlugin.SpecFlowPlugin' or one of its dependencies"

Specflow + MSTest: tests fails with error "Could not load file or assembly 'MyPlugin.SpecFlowPlugin' or one of its dependencies"

我正在 运行 使用 MSTest 和命令行进行 specflow 测试。 项目构建没有错误,但测试失败并出现错误:

    Class Initialization method MyProject.SpecFiles.MyFeature.FeatureSetup threw exception. TechTalk.SpecFlow.SpecFlowException: TechTalk.SpecFlow.SpecFlowException: Unable to load plugin: MyPlugin. Please check http://go.specflow.org/doc-plugins for details. ---> System.IO.FileNotFoundException: Could not load file or assembly 'MyPlugin.SpecFlowPlugin' or one of its dependencies. The system cannot find the file specified.WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].
.
+++++++++++++++++++
STACK TRACE:
    at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean forIntrospection)
   at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
   at System.Reflection.Assembly.Load(String assemblyString)
   at TechTalk.SpecFlow.Infrastructure.RuntimePluginLoader.LoadPlugin(PluginDescriptor pluginDescriptor)
 --- End of inner exception stack trace ---
    at TechTalk.SpecFlow.Infrastructure.RuntimePluginLoader.LoadPlugin(PluginDescriptor pluginDescriptor)
   at TechTalk.SpecFlow.Infrastructure.TestRunContainerBuilder.<>c__DisplayClass3.<LoadPlugins>b__1(PluginDescriptor pd)
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.<ConcatIterator>d__71`1.MoveNext()
   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at TechTalk.SpecFlow.Infrastructure.TestRunContainerBuilder.LoadPlugins(IRuntimeConfigurationProvider configurationProvider, ObjectContainer container)
   at TechTalk.SpecFlow.Infrastructure.TestRunContainerBuilder.CreateContainer(IRuntimeConfigurationProvider configurationProvider)
   at TechTalk.SpecFlow.TestRunnerManager.CreateTestRunner(TestRunnerKey key)
   at TechTalk.SpecFlow.TestRunnerManager.GetTestRunner(TestRunnerKey key)
   at TechTalk.SpecFlow.TestRunnerManager.GetTestRunner(Assembly testAssembly, Boolean async)
   at TechTalk.SpecFlow.TestRunnerManager.GetTestRunner()
   at MyProject.SpecFiles.MyFeature.FeatureSetup(TestContext testContext) in d:\Jenkins\workspace\MyProject\SpecFiles\MyFeature.feature.cs:line 0

有人可以帮助我吗?

问题可能是在 specflow 期望加载它的地方找不到您的插件 dll(或其依赖项之一)。

您可以在应用程序 .config 中指定一个位置来查找插件,如 explained in the documentation:

The following example is used to load a plugin assembly called "MyNewPlugin.SpecFlowPlugin.dll" that is located in a folder called "Binaries" that is at the same level of the current project.

<specFlow>
    <plugins>
        <add name="MyNewPlugin" path="..\Binaries" />
    </plugins>
</specFlow>

检查生成服务器上的输出以查看正在创建插件 dll 的位置,并适当地调整生成或配置

您似乎在尝试在项目中加载程序集。为了加载程序集,您必须在 app.config.

中添加以下代码
<stepAssemblies>
     <stepAssembly assembly="{name-of-assembly-containing-bindgins}" /> 
</stepAssemblies>

问题已解决。我添加了 .testsettings 文件,并选中了 Deployment 复选框。部署项目 - 包含所有必要 dll 的 bin 文件夹。