由于无法找到“TechTalk.SpecFlow”文件,SpecFlow单元测试失败

时间:2012-06-27 07:06:11

标签: unit-testing tfs mstest tfs2008 specflow

我有一个VS2010单元测试项目设置为使用SpecFlow 1.8.1和mstest。为了让SpecFlow单元测试工作,我已经完成了以下工作: -

  1. 我在项目中添加了对以下文件的引用: -

    Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll
    TechTalk.SpecFlow.dll

    请注意,TechTalk.SpecFlow.dll已添加到我的项目中,引用指向该文件。

  2. 我已将TechTalk.SpecFlow.dll引用的“Copy Local”属性设置为True。

  3. 我还添加了一个App.Config,指定“MsTest.2010”作为提供程序,并重新生成SpecFlow功能的所有代码隐藏。

  4. 一切都在我的VS2010中运行,测试在SpecFlow testrunner和mstest测试运行器中成功运行。但是当我尝试在TFS 2008中运行mstests(使用.vsmdi测试列表文件)时,它失败并出现以下异常: -

    Class Initialization method MyNamespace.MyTestFeature.FeatureSetup threw exception.
    System.Configuration.ConfigurationErrorsException:
    System.Configuration.ConfigurationErrorsException: An error occurred creating the
    configuration section handler for specFlow: Could not load file or assembly
    'TechTalk.SpecFlow' or one of its dependencies. The system cannot find the file
    specified. (D:\Projects\TestProject\TestResults\administrator_MYPC 2012-06-27
    18_30_05_Any CPU_Debug\Out\TestProject.DLL.config line 4) --->
    System.IO.FileNotFoundException: Could not load file or assembly 'TechTalk.SpecFlow'
    or one of its dependencies. The system cannot find the file specified.
    

    请注意,TFS构建项目很好,它在同一项目中运行其他单元测试(正常mstests,而不是SpecFlow)没有问题。它只对SpecFlow测试运行失败。

    那么我做错了什么?

    编辑:我的App.Config文件的内容如下所示:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <section
           name="specFlow"
           type="TechTalk.SpecFlow.Configuration.ConfigurationSectionHandler, TechTalk.SpecFlow"/>
      </configSections>
      <specFlow>
        <unitTestProvider name="MsTest.2010" />
    
        <runtime detectAmbiguousMatches="true"
             stopAtFirstError="false"
             missingOrPendingStepsOutcome="Inconclusive" />
    
        <trace traceSuccessfulSteps="true"
               traceTimings="false"
               minTracedDuration="0:0:0.1" />
      </specFlow>
    </configuration>
    

5 个答案:

答案 0 :(得分:2)

按照this sitethis site上的说明操作:

命令Tools > Library Package Manager > Package Manager Console允许您输入PM> Install-Package SpecFlow

当提示返回“已成功安装”时,SpecFlow程序集现在出现在项目的引用中。 MSTest项目现在成功编译(至少对我而言)。

答案 1 :(得分:1)

我也遇到了这个错误,在我的情况下问题是我使用\ ... \ _对象\ Debug || Release \文件夹作为目标而不是\ ... \ bin \ Debug || Release \文件夹。查看这些文件夹,我看到前者缺少TechTalk.dll程序集。只需切换我的.bat文件,问题就解决了。

答案 2 :(得分:1)

有时VS2013正在寻找不在项目文件夹中但在C:\Users\**YOUR_USER**\AppData\Local\Temp\VisualStudioTestExplorerExtensions\SpecRun.Runner.1.3.0\tools中的SpecRun dll。所以你只需要将所有必要的SpecFlow库放在那里

答案 3 :(得分:0)

我发现让它工作的一个hack是为我在项目中创建的每个SpecFlow功能添加另一个类。该课程如下: -

[DeploymentItem(@"TechTalk.SpecFlow.dll")]
partial class MyTestFeature { }

// The above class-name needs to come from the auto-generated code behind
// (.feature.cs) for each SpecFlow feature.

我认为这是一个非常讨厌的黑客,但它确实提供了为什么它不起作用的线索。如果有人想出更优雅的解决方案,那将是件好事。

答案 4 :(得分:0)

我终于找到了更适合这个问题的方法。我只需要添加一个post-build事件来从构建输出中删除.config文件。 (App.config文件仅用于在设计时生成代码隐藏。在运行时期间根本不使用它,因此可以将其删除。)

构建后事件的命令如下所示: -

del /f /q "$(TargetDir)$(TargetFileName).config"

更正:.config文件用于生成不确定的结果,因此更好的构建后事件命令如下: -

if "$(IsDesktopBuild)"=="false" del /f /q "$(TargetDir)$(TargetFileName).config"