SpecFlow unit test failed due to not able to find "TechTalk.SpecFlow" file

11,993

Solution 1

I finally found the more proper fix for this issue. I just need to add a post-build event to remove the .config file from the build output. (The App.config file is used only to generate the code-behind during design time. It is not used at all during runtime, so it can be removed.)

The command for the post-build event looks like this:-

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

Correction: The .config file is used for generating inconclusive results, so a better post-build event command is as follows:-

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

Solution 2

Following the instruction on this site and this site:

the command Tools > Library Package Manager > Package Manager Console allows you to type in PM> Install-Package SpecFlow

when the prompts returns "installed successfully", the SpecFlow Assembly now appears in the references of your project. And the MSTest project now compiles succesfully (at least for me).

Solution 3

I got this error as well, in my case the problem was that I was using the \...\obj\Debug||Release\ folder as target and not the \...\bin\Debug||Release\ folder. Looking in these folders I saw that the TechTalk.dll assembly was missing from the former. Simply switching in my .bat file the problem was fixed.

Solution 4

Sometimes VS2013 is looking for SpecRun dlls not in project folder, but in C:\Users\**YOUR_USER**\AppData\Local\Temp\VisualStudioTestExplorerExtensions\SpecRun.Runner.1.3.0\tools. So you just need to put all necessary SpecFlow libraries therel

Share:
11,993
SF Lee
Author by

SF Lee

Just a regular software developer in an NZ company.

Updated on June 04, 2022

Comments

  • SF Lee
    SF Lee almost 2 years

    I have a VS2010 unit test project set to using SpecFlow 1.8.1 and mstest. In order to get the SpecFlow unit tests working, I've done the following:-

    1. I added the references to the following files in my project:-

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

      Note that the TechTalk.SpecFlow.dll has been added into my project and the reference points to that file.

    2. I've set the "Copy Local" property of the TechTalk.SpecFlow.dll reference to True.

    3. I've also added an App.Config that specifies "MsTest.2010" as the provider, and regenerated all code-behinds for the SpecFlow features.

    Everything works in my VS2010, the tests run successfully in both the SpecFlow testrunner and the mstest test runner. BUT when I try to run the mstests in TFS 2008 (using a .vsmdi test list file), it failed with the following exception:-

    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.
    

    Note that the TFS built the project fine and it runs other unit tests in the same project (normal mstests, not SpecFlow) without problems. It only failed for the SpecFlow test runs.

    So what am I doing wrong?

    Edit: The contents of my App.Config file looks like this:

    <?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>
    
    • t3hn00b
      t3hn00b almost 12 years
      Can you copy your app.config file too? Mine looks like this: pastebin.com/exKSSdPm
    • SF Lee
      SF Lee almost 12 years
      I've added the App.Config file contents as above.
    • SF Lee
      SF Lee almost 12 years
      Note that I'm using VS2010, so my provider has to be "MsTest.2010", not "MsTest".
    • t3hn00b
      t3hn00b almost 12 years
      Maybe that's the cause - I'm using VS2010 too.
    • t3hn00b
      t3hn00b almost 12 years
      Not a clue. The other thing that's different is the platform - you've set it up for Any CPU, mine is x86.
    • SF Lee
      SF Lee almost 12 years
      No, I don't have any 'x86' platform on my solution.
  • SF Lee
    SF Lee almost 12 years
    The only problem with this method is that it cause all "inconclusive" tests to be flagged as errors. This is because the ScenarioContext.Current.Pending() needs to know the provider (at runtime) in order to know which Assert class to use for throwing inconclusive exceptions.
  • SF Lee
    SF Lee almost 12 years
    A better way is to check $(IsDesktopBuild) so that the .config file is not deleted when running from Visual Studio.