k3b
k3b

Reputation: 14755

Why does MSTest DeploymentItem fail when running all tests in solution?

I have a Visual Studio 2010 solution with several MSTest projects. One of the test projects needs a file in a specific directory to run.

[TestClass]
[DeploymentItem("ReportEngine.config")]
[DeploymentItem("Report Files", "Report Files")]
public class MyReportTests { }

These tests pass when I run only the tests in this project (or test class). The report files are copied to the test execution directory. These tests fail when I run all the tests in the solution. The report files are not copied to the test execution directory.

Why is there a difference and how do I make the two runs deploy the same items?

Upvotes: 11

Views: 2712

Answers (4)

Manuel Faux
Manuel Faux

Reputation: 2457

Maybe a test case moves/deletes the file? According to the MSDN article "How to: Deploy Files for Tests" the files are deployed only once:

When you select a set of tests to run, all the items specified in their DeploymentItem attributes are copied before the test run starts.

Upvotes: 0

Anthony Mastrean
Anthony Mastrean

Reputation: 22394

I originally gave you instructions to ensure that Deployment was enabled in your test settings. You said that it was.

Make sure that you have deployment enabled for the test run.

  1. Edit your .testsettings in the Solution Items folder
  2. Select the Deployment category
  3. Check the "Enable deployment" option
  4. Click "Apply" and "Close"

However, since last time I answered, I learned that the DeploymentItem attribute only targets methods (and then, it seems only works on TestMethods). It could never have worked the way you have it decorating a class. I also noticed your comment on your question (edited for grammar)

Yes, the items are set to Copy Always, otherwise the DeploymentItem in the project test run wouldn't work.

There's a lot of discussion whether this is necessary or not to make DeploymentItem work. I suspect that something we haven't identified yet is making tests pass when running them from the project. Please remove the DeploymentItems completely and try your two test runs (from project and from solution) and see what results you get.


In Case Deployment Items are Working Despite the Documentation

Make sure that ReportEngine.config and ReportFiles\ are where you and MSTest expect them to be. Relative file paths are resolved starting at the "RelativeRootPath". By default, that's the $(SolutionDir). Unless you override it in the testsettings. Please check on that.

But, by default

[DeploymentItem("ReportEngine.config")]

is expanded to something like

[DeploymentItem("$(SolutionDir)\ReportEngine.config")]

then, for example, to

[DeploymentItem("D:\code\my-project\ReportEngine.config")]

Upvotes: 4

k3b
k3b

Reputation: 14755

Seems to be a microsoft won-t fix issue.

Upvotes: 0

Anthony Mastrean
Anthony Mastrean

Reputation: 22394

Deployment items are notoriously flakey... Have you considered using MSBuild and editing your test project's .csproj file directly?

<Target Name="AfterBuild">
  <CallTarget Targets="DeployReportFiles" />
</Target>

<Target Name="DeployReportFiles">
  <CreateItem Include="$(SolutionDir)\Report Files\**\*">
    <Output TaskParameter="Include" ItemName="OutputFiles" />
  </CreateItem>
  <Copy SourceFiles="@(OutputFiles)"
        DestinationFiles="@(OutputFiles->'$(???)\Report Files\%(RecursiveDir)%(Filename)%(Extension)')" />
</Target>

I don't know if MSBuild contains a variable for the current test directory... since it doesn't appear to be involved in the process.

Upvotes: 1

Related Questions