Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xml file not copying to test output directory

Visual Studio 2010, x64 machine, using the built-in web server to host a WCF service with a set of unit tests using the built-in test framework.

I have an XML file that my tests need to load to run. I've included this file in the test project, and have the file set to 'content' and 'always copy to output directory'. This file copies to the bin\debug directory fine.

When I execute the tests, however, the xml file is not there. Instead of looking in the project's bin\debug folder, it looks for it in the test's working directory, C:\Projects\SAP Reapprovals\TestResults\name_machine 2010-12-06 13_45_43\Out". The file hasn't been copied there.

Is there a way to force this file to copy, or do I need to fully qualify the reference from within the test?

TIA!
James


Update
I set the DeploymentItem attribute, but the file still doesn't copy. But that sure looks like what I want... any ideas why that isn't working?

My test code:

[TestMethod] [DeploymentItem("SAP - GS3 format.xml")] public void TestProcessSapRoles() {      //  I get a 'file not found' error here, and when      //  I check the output directory, it isn't there     XElement rolesRoot = XElement.Load("SAP - GS3 format.xml");  } 


ANSWERS:
Thanks go out to CPedros, with his help I've zoomed in on this a bit. I ran SysInternals' Process Monitor, to see where it was looking for my xml file. Here's what I found:

When I ran the tests using ctrl+r,ctrl+t (debug tests in current context), visual studio ignored the DeploymentItem attribute completely and did not even try to copy the file anywhere. In this case I got a "File Not Found" exception when I tried to open it for reading. Visual studio created a temporary working directory for the tests, but there was only one file in it, AgentRestart.dat.

When I ran the tests using the 'Run Unit Tests' button in my toolbar (not sure what test option that is), Visual Studio did not copy the file over, but referenced it directly from the project directory. The test passed, and no temporary working directory was created.

When I ran the test from the menu option "run -> tests in current context" (run, not debug), a temporary working directory was created, and the xml file and all executables were copied to it. The test passed.

When I edited Local.testsettings (under a Solution Items folder under my tests folder), I chose 'Deployment' from the left menu, and added the xml file. It was added as [solution directory]\[project directory]\file.xml. I removed the DeploymentItem attribute. Now I was able to debug the tests; the xml file and all executables were copied to the temporary directory created for the test.

TLDR: Visual Studio is ignoring the DeploymentItem attribute for certain ways of running the test. The solution is to edit Local.testsettings, the Deployment menu, and add the file by hand.

Thanks for the help! I'm giving CPedros credit for his answer, as it was the most helpful in resolving this.

like image 678
James King Avatar asked Dec 06 '10 18:12

James King


People also ask

What is Copy to Output Directory?

"Copy to Output Directory" is the property of the files within a Visual Studio project, which defines if the file will be copied to the project's built path as it is. Coping the file as it is allows us to use relative path to files within the project.

Where are XML files stored?

The product XML file location property defines the location where all the product XML configuration files are stored. This property is set by default to the following locations: For 32-bit Windows operating systems—<Installation location>\Program Files\ArcGIS\MaritimeCharting\Desktop<version>\Common.


1 Answers

Try annotating your test with the DeploymentItem attribute: http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.deploymentitemattribute(v=VS.100).aspx

Here's a code snippet from the documentation:

  [TestClass]     public class UnitTest1     {         [TestMethod()]         [DeploymentItem("testFile1.txt")]         public void ConstructorTest()         {             // Create the file to deploy             Car.CarInfo();             string file = "testFile1.txt";             // Check if the created file exists in the deployment directory             Assert.IsTrue(File.Exists(file), "deployment failed: " + file +                 " did not get deployed");         }     } 
like image 100
Pero P. Avatar answered Sep 19 '22 03:09

Pero P.