8

I'm new with unit testing in visual studio and I want to load a physical xml file. This file is in the unit testing project as a Content and it's copied in the output directory.

So, when I compile the project, the xml file is in the output directory. But when I execute the test, a new directory is created with all dependent DLL, but the xml file isn't copied.

The content of the Xml is needed to execute the test. I execute this code to retrieve the path of the Xml file in the execution folder :

private static string GetXmlFullName()
{
    // GetApplicationPath use the current DLL to find the physical path
    // of the current application
    string executeDirectory = AssemblyHelper.GetApplicationPath();
    return Path.Combine(executeDirectory, "content.xml");
}

The exception is :

System.IO.DirectoryNotFoundException: 'd:\***\solutiondirectory\testresults\*** 2012-06-13 17_59_53\out\content.xml'.

How can I add this file in the execute folder?

Thanks in advance. (and sorry for my english ...)

Hyralex
  • 990
  • 1
  • 8
  • 25
  • looks like a duplicate of this post: [sample data in unit test][1] [1]: http://stackoverflow.com/questions/5383987/how-can-i-include-sample-data-files-in-vs-unit-tests – Clueless Jun 13 '12 at 16:10
  • @Clueless : Indeed. I didn't found this post. Thank you. – Hyralex Jun 13 '12 at 16:16
  • Also related - https://stackoverflow.com/questions/5581628/c-sharp-test-resources – sashoalm May 16 '17 at 14:20

2 Answers2

6

You need to place a DeploymentItemAttribute on the test class.

For example to include all files in the Data folder

[TestClass()]
[DeploymentItem("Data")]
public class MyTestClass
{
    ...
}
Matthew Lock
  • 13,144
  • 12
  • 92
  • 130
Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • The DeploymentItem attribute will only work if Build Action on the item is set to Content and the Copy to Output Directory is set to Copy if Newer or Copy always. http://stackoverflow.com/a/9107244/74585 – Matthew Lock Oct 03 '13 at 03:14
  • I also had to enable Deployment in Solution Items -> Local.testsettings and add the directory I wanted to deploy. Phew! – Matthew Lock Oct 03 '13 at 03:36
0

As mentioned by Matthew Lock, the only thing I needed to do to get this working was to add the file under Deployment in Local.testsettings within the solution items.

Test Settings