title: Run Unit Tests from Build Output
body: <p>I need to run unit tests from an assembly where the assembly already exists. Visual Studio (2012) is copying that assembly to some generated deployment location and is testing from there. How can I prevent that?</p>
  
  <p>According to this page on <a href="http://msdn.microsoft.com/en-us/library/ms182475.aspx" rel="nofollow">How to: Deploy Files for Tests</a>, unit tests are only deployed to some directory when there are DeploymentItem attributes being used.</p>
  
  <blockquote>
    <p>If you run tests by using Visual Studio, the deployment folder is created under TestResults in your solution folder.
    A separate deployment folder is used if any of the test methods or classes in a test run has the DeploymentItem attribute, or if you use a .testsettings file.</p>
  </blockquote>
  
  <p>I don't have any DeploymentItem attributes, yet the following test is failing because the unit test is being run from some generated directory, not the build output directory. I've also tried creating a .runsettings file and ensuring "Enable Deployment" is unchecked, but Visual Studio seems to ignore that.</p>
  
  <pre><code>[TestClass]
  public class UnitTests
  {
      private void RunAssemblySelfTests(string assemblyName)
      {
          Assembly assembly = Assembly.LoadFrom(assemblyName + ".dll");
          //...load and run tests that use in-house testing framework
      }
  
      [TestMethod]
      public void MyAssemblyTest()
      {
          RunAssemblySelfTests("MyAssembly");
      }
  }
  </code></pre>
  
  <p>The UnitTests assembly's build output directory is the same directory as MyAssembly, and I've confirmed that they both exist there. When run, the test fails because of this exception:</p>
  
  <p>Result Message: </p>
  
  <blockquote>
    <p>Test method MyNameSpace.UnitTests.MyAssemblyTest threw exception: 
    System.IO.FileNotFoundException: Could not load file or assembly
    'file:///[path to solution]\SolutionName\TestResults[user
    name]_[computer name] 2013-08-28 13_35_10\Out\MyAssembly.dll' or one
    of its dependencies. The system cannot find the file specified.</p>
  </blockquote>
  
  <p>I can't deploy any test assemblies, even without this reflection stuff going on, because the dependencies result in several GB.</p>