11

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?

Anthony Mastrean
  • 21,850
  • 21
  • 110
  • 188
k3b
  • 14,517
  • 7
  • 53
  • 85
  • If you right click on those items in your solution are they set to copy? I'm wondering if you need to set them to copy always... just a guess. – Brian Dishaw May 25 '11 at 01:39
  • yes the items are set to "copy Always". otherwise the [DeploymentItem] in projectscope wouldn-t work, too – k3b May 25 '11 at 07:46
  • "Report files" is relative to the Solution root, correct? Is that the case in your project? – Anthony Mastrean May 27 '11 at 18:06

4 Answers4

4

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")]
Community
  • 1
  • 1
Anthony Mastrean
  • 21,850
  • 21
  • 110
  • 188
  • Unfortunately this is **not** the solution to my problem. I alrady had these settings. Otherwise the test would not succeed on projectscope. I have several test-projects when i execute them all together (on solution scope) some fail because [DeploymentItem] is ignored. when i execute one test-project the tests succeed, Deployment works because the settings in the answer were made – k3b May 27 '11 at 14:17
  • Oh, sorry about that... the bounty stuff is a little awkward in this situation :( – Anthony Mastrean May 27 '11 at 17:38
  • +1, I needed to create a TestSettings file and add it to my unit test folder in my solution - then select that TestSettings file in Visual Studio. It wasn't apparent from other documentation that I saw that I would need to create one to enable `DeploymentItem`s. – Jon Peterson Dec 18 '13 at 20:17
1

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.

Anthony Mastrean
  • 21,850
  • 21
  • 110
  • 188
0

Seems to be a microsoft won-t fix issue.

k3b
  • 14,517
  • 7
  • 53
  • 85
  • 2
    Do you have a link to the KB or Connect issue that specifies this? – Anthony Mastrean Nov 29 '12 at 16:36
  • I asked the question in may 2011 and after 16 months and a bounty i was frustrated that there was no solution. So i made my own answer accepted. – k3b May 15 '13 at 15:46
0

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.

Manuel Faux
  • 2,317
  • 5
  • 24
  • 35