Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get "Copy to Output Directory" to work with Unit Tests?

People also ask

What does Copy to Output Directory do?

"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.

Should unit tests read files?

Strictly speaking, unit tests should not use the file system because it is slow. However, readability is more important. XML in a file is easier to read, and can be loaded in an XML friendly editor.

How do you add references to unit testing?

In your unit test project, add a reference to the code under test. To add a reference to a code project in the same solution: Select the test project in Solution Explorer. On the Project menu, select Add Reference.


The standard way to do this is by specifying the deployment items in the .testrunconfig file, which can be accessed via the Edit Test Run Configurations item in the Visual Studio Test menu or in the Solution Items folder.


You can specify deployment attribute like an example shown below; Also you need to set "Content" & "Copy if newer" property ( there is no documentation on the later settings, but you have set those to make it work.

[TestMethod]
[DeploymentItem("mytestdata.xml")]
public void UploadTest()
{



}

I had to turn on "Enable Deployment" under Test -> Edit Test Settings -> Local -> Deployment for the [DeploymentItem] attribute to work.


All three answers are correct, depending on your needs.

Adding files to deploy in the .testrunconfig (.testsettings in VS2010) will copy all of those files to every test output folder, even for unrelated tests run in isolation. If you run one test, all the test data files listed in the deployment section of .testssettings will be copied to the test output folder.

In my tests I need to copy an expected XML file to the test output folder to compare with the actual test output XML. I use the DeploymentItem attribute to only copy the XML file related to the test(s) being run. In VS2010 I had to enable deployment in the .testsettings file (but not add any paths) and then reference the XML file path relative to the TestProject in the DeploymentItem.

Hope this helps.


I had a similar problem but mine had to do with pointing to the TraceAndTestImpact.testsettings file instead of the Local.testsettings file. You can change from one to the other under the Test/Select Active Test Settings menu.


The following works in VS2012 for test projects included in multiple solutions without using a testsettings file:

1) Arrange the files and folders you wish to deploy into a folder in the test project directory.

2) In the project properties, create a post build step

xcopy /Y /S /i "$(ProjectDir)<Project_Folder_Name>\*" "$(TargetDir)<Deployment_Folder_Name>"

$(ProjectDir) and $(TargetDir) are macros that will be interpreted by VS and should be included as such.

<Project_Folder_Name> is the name of the folder created in step 1.

<Deployment_Folder_Name> is the name of the folder in which the test files will be deployed and should be named so that it will be unique when multiple test projects are deployed to the same directory, e.g. <Project_Name>_TestInputs.

Test files in shared locations should also be copied to the target directory deployment folder to limit test interactions. Provide the source path relative to the $(ProjectDir) macro. For example "$(ProjectDir)..\..\Common Files\C1219TDL-2008.xml".

3) Add a [DeploymentItem(source, destination)] property to either each test method that uses a deployment file (best practice) or to the test class (easier practice for the lazy or hurried, and the easiest way to update a project the previously used relative paths or a testsettings file).

On a test method, source is the path to the file or directory used in the test method relative to the target directory as created by the xcopy and destination is the path to the directory in which it will be created relative to the deployment directory. So that tests run consistent in either the target directory or a deployment directory. The destination path should be the same as the source path without a file reference. Example: [DeploymentItem("Example_TestInputs\C1219TDL-2008.xml","Example_TestInputs")]. The DeploymentItem should be included on every method that uses that file or directory.

On a class, source and destination are both the name of the folder created in the target directory by the xcopy; this will copy the entire folder to the deployment directory when any test in the class is run. Example: [DeploymentItem("Example_TestInputs","Example_TestInputs")]

4) In the test methods, you can now access files and directories with confidence they will be in the working directory regardless of where Visual Studio has decided to put it that day, e.g. File.Exists(".\Example_TestInputs\C1219TDL-2008.xml").