Search code examples
mstestopencover

How to set dll search path of MSTest used with opencover?


My unit tests have dependencies which are from a pool of dlls in a directory outside of the project. We have the policy to generally place links to these dlls (from the pool) in a directory called "System" inside the project directory and set their CopyToOutputDirectory to PreserveNewest. Afterwards, in the project we add a reference to the dll in the pool but set it to be not a private copy which means it will not be output to the build dir. In the InitAssembly() method we have setup that the runtime searches also in the AppDomain.CurrentDomain.BaseDirectory, "System") by the means of AppDomain.CurrentDomain.AssemblyResolve.

[TestClass]
public class SomeUnitTest : OurUnitTestBase
{
  [AssemblyInitialize]
  public static void AssemblyInit(TestContext context)
  {
    Debug.WriteLine("in AssemblyInit");
    base.InitAssembly();
  }
}

This works nice when running our code from inside VisualStudio and also when running the unit tests from inside VisualStudio's Test-Explorer (vstest.executionengine.x86.exe). However MSTest fails to find the dlls in the System subdirectory when run from the following batch script which executes a coverage analysis with opencover:

REM Bring dev tools into the PATH.
call "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\Tools\VsDevCmd.bat"

mkdir .\CoverageAnalysisReport

REM Run unit tests through OpenCover
REM This allows OpenCover to gather code coverage results
.\..\..\Tools\CoverageAnalysis\opencover\OpenCover.Console.exe^
  -register:user^
  -target:MSTest.exe^  
  -targetargs:"/noresults /noisolation /testcontainer:..\bin\Debug\OurUnitTests.dll"^
  -filter:+[*]*^
  -output:.\CoverageAnalysisReport\output.xml

REM Generate the report
.\..\..\Tools\CoverageAnalysis\ReportGenerator\bin\ReportGenerator.exe^
  -reports:.\CoverageAnalysisReport\output.xml^
  -targetdir:.\CoverageAnalysisReport^
  -reporttypes:HTML^
  -filters:-OurUnitTests*

REM Open the report
start .\CoverageAnalysisReport\index.htm

The resulting log file says, that none of the unit tests could be run and failed therefore. Furthermore it states

Warning: The assembly "BaseLib" as a direct or indirect dependency of the test container "C:\MyProject\bin\debug\ourunittests.dll" was not found.

Yet I know [AssemblyInitialize] was called because a System.Windows.Form.MessageBox.Show(..) I placed there temporarily actually got displayed during the execution of the bat-file.

Is there a way to tell MSTest to search also for dlls in the System subdirectory also?


Solution

  • I solved it by placing the following as the content of a file named Local.testsettings in the same directory like the batch file (in which I modified the line -targetargs:"/testcontainer:\"..\bin\x86\Debug\OurUnitTests.dll\" /testSettings:\".\Local.testsettings\"):

    <?xml version="1.0" encoding="UTF-8"?>
    <TestSettings name="Lokal" id="2fa4344c-1f2f-4a04-86f3-41d223b10333" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
      <Description>Dies sind die standardmäßigen Testeinstellungen für einen lokalen Testlauf.</Description>
      <Deployment>
        <DeploymentItem filename="..\bin\Debug\System\" />
      </Deployment>
      <Execution>
        <TestTypeSpecific>
          <UnitTestRunConfig testTypeId="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b">
            <AssemblyResolution>
              <TestDirectory useLoadContext="true" />
            </AssemblyResolution>
          </UnitTestRunConfig>
        </TestTypeSpecific>
        <AgentRule name="Execution Agents">
        </AgentRule>
      </Execution>
    </TestSettings>