21

I have a unit test project which contains a few XML files for data driven tests. These files are set to Build Action Content and Copy if newer or Copy always.

For the most part, this works fine. However, when I change one of the data files without changing any code, Visual Studio will not trigger a build action at all, and no XML files will be copied. Effectively, I'm running my unit tests with the old data file.

I can trigger a manual build, but I'd like to prevent such mistakes if possible. Is there some way to make Visual Studio (2008) perform the copy operation every time someone changes the content files only?

To clarify: The XML file is inside the unit test project, not in the production code project. I do have a relevant [DeploymentItem] attribute for the file set on the test being run.

Thorarin
  • 47,289
  • 11
  • 75
  • 111
  • What are you running the unit tests in? Visual Studio (mstest)? NUnit? If it is `mstest` then have a look at [How can I get "Copy to Output Directory" to work with Unit Tests?](http://stackoverflow.com/questions/227545/how-can-i-get-copy-to-output-directory-to-work-with-unit-tests) – Devendra D. Chavan Sep 10 '11 at 06:39

4 Answers4

8

I don't know if it'll help in your situation but to get around this I changed Build Action of my XML files to Embedded Resource and it seemed to do the trick.

Adam Ruth
  • 3,575
  • 1
  • 21
  • 20
1

This seems to simple of an answer, so please forgive me if you've already done it. However I used to have this issue after editing text file data sources.

To fix this I had to clean the project after every change which was annoying on a large project I was working on.

This stopped being needed once I ended up installing VS2008 on a fresh copy of Windows so it may be a configuration issue.

Kirk
  • 16,182
  • 20
  • 80
  • 112
1

Before running the tests, Visual Studio invokes MSBuild on your project with the Build and Test target. If you look into Microsoft.CSharp.targets (see http://msdn.microsoft.com/en-us/library/ms164312.aspx), you can trace how files from the Content ItemGroup get copied around as part of the bulild process. AFAIK, they are declared inputs and outputs of one of the Bulild sub-targets, so MSBuild should really consider copying them if their timestamps changed.

Can you ensure that:

  • changes to the files are saved
  • you are using NTFS
  • when you run the build from the msbuild cmd-line, does it still not rebuild when an xml file changed?

Visual Studios MSBuild integration is pretty broken, it does obivously not do the straightforward thing of invoking The MSBuild cmd-line. (It has issues with imports being cached and all sorts of crazy, non-intuive things).

sam hocevar
  • 11,853
  • 5
  • 49
  • 68
Johannes Rudolph
  • 35,298
  • 14
  • 114
  • 172
1

Visual Studio 2022 V17.5 - Build Acceleration

Be aware that of v17.5 (Feb 2023) a new feature called Build Acceleration was introduced.

In the past, VS called MSBuild to copy files that had changed, even if a recompile was not needed. With the enhancement VS now handles file copying and only calls MSBuild if a recompile is needed. So if you have a resource with Build Action: None (e.g. on appsettings.json) then editing a setting won't get picked up when debugging, if you have made no code changes.

To fix this: set Build Action: Embedded Resource (as per the accepted answer above). You might need to update the build action for all files the compiler references, such as JSON scripts.

Reference: https://github.com/dotnet/project-system/blob/main/docs/build-acceleration.md

enter image description here

To check Build Acceleration in VS: enter image description here

AndyS
  • 750
  • 5
  • 14