125

When I build a unit test project before the tests are executed the test output is copied to a TestResults folder and then the tests are executed. The issue I'm having is that not all the files in the Debug/bin directory are copied to the TestResults project.

How can I get a file that is copied to the Debug/bin directory to also be copied to the TestResults folder?

Stephan Schielke
  • 2,456
  • 7
  • 29
  • 39
Eric Schoonover
  • 44,080
  • 43
  • 148
  • 200

11 Answers11

123

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.

Mark Cidade
  • 94,042
  • 31
  • 216
  • 230
  • 15
    In VS2010 this is: Test/Edit Test Settings/local, then in the list, select "Deployment", check the "Enable..." box and add the file(s). – Marcel Jun 16 '10 at 12:54
  • 19
    You may have to close your solution (or even Visual Studio) and reopen before this change takes effect properly – RobV Aug 18 '10 at 14:06
  • RobV, you made my day. THANKS! – Ignacio Soler Garcia Feb 07 '12 at 17:07
  • 2
    If you use the [DeploymentItem] attribute in code, you don't need to specify the files in the dialog--see @tomfanning's code below. – Patrick Szalapski Jul 16 '12 at 19:21
  • This does not resolve my problem with copying the config output from bin or release folder because at design time i don't have information about if the file will be in debug or release folder and for me that is the file which is not getting copied to the Out folder in test results – Gurpreet Mar 08 '18 at 10:23
  • Another thing is how do you choose which .testrunconfig file to use if i want to publish my tests from teamcity to run of VSTS? – Gurpreet Mar 08 '18 at 10:27
62

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()
{



}
Sanjay10
  • 1,757
  • 12
  • 8
  • 3
    You still need to enable this. As Mercel wrote in his comment, In VS2010 this is: Test/Edit Test Settings/local, then in the list, select "Deployment", check the "Enable..." box. – Patrick Szalapski Jul 16 '12 at 19:22
  • I'm struggling with this problem in Visual Studio 2012 Express and because it's a limited edition it was hard to find clear documentation. A big thank you for this answer. – DavidHyogo Sep 11 '12 at 16:16
  • 2
    Works great. I actually think this is the better method & answer as it documents the required files right above the testmethod. – bu5hm4nn Mar 31 '14 at 08:49
  • 1
    I found that the DeploymentItem attribute also works when specified at the test *class* level (instead of the method level). – Jon Schneider Feb 10 '16 at 15:48
10

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

tomfanning
  • 9,330
  • 4
  • 48
  • 76
  • This is true... I just enabled it and now the attribute works. Thanks! – Miguel Angelo Dec 26 '11 at 18:16
  • As mentioned in my comment to another answer, I'm struggling with the same problem in visual studio 2012 express and the 2 answers together finally gave me the solution. – DavidHyogo Sep 11 '12 at 16:17
5

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.

GraehamF
  • 1,901
  • 23
  • 24
  • 2
    My issue was the Relative Path mentioned here, I thought the Xml should be in the directory the test was in, but it needed to be in the project root, or be prefaced by the directory that it was in. – Wes Grant Jun 30 '12 at 00:50
2

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

matthias_h
  • 11,162
  • 9
  • 19
  • 38
2

Would like to just enhance the accepted answer by mentioning a way to get it to deploy specifically for dll's rather then the normal method of using it for data or config etc, for the circumstances where CopyLocal doesn't work:

[DeploymentItem("bin\\release\\iRock.dll")]
[DeploymentItem("bin\\debug\\iRock.dll")]
JamesDill
  • 1,709
  • 16
  • 22
2

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.

Simon
  • 21
  • 1
0

In Visual Studio 2012 you don't need a DeploymentItem attribute for the simple case. See my answer here

Community
  • 1
  • 1
acarlon
  • 14,908
  • 7
  • 66
  • 87
0

Try out the Post-Build event command line from within Visual Studio (if you are using that IDE).

Kasper
  • 1,704
  • 2
  • 17
  • 31
  • 3
    I am, but that seems like a bit of a hack. This has to be a fairly common scenario and I hope there is just some option or property I am not properly setting. – Eric Schoonover Oct 22 '08 at 21:23
0
[TestMethod]
[DeploymentItem("ProjectName/Folder/SubFolder/file.xml", "Folder/Subfolder")]
public void YourTestMethod()
{
   // in the method you are testing you should have this:
   var filePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase) + "/Folder/Subfolder/file.xml";
}
Nina
  • 887
  • 10
  • 16
0

The accepted answer is correct and so are most of the other answers. However, over the years I have found that the Deploment system of Visual Studio Unit Tests using DeploymentAttribtue and Copy to Output to be cumbersome if you have a large number of data files. I have found that keeping the files in their original location worked better.

Full details in my other answer here. https://stackoverflow.com/a/53004985/2989655

Hope this helps.

Sau001
  • 844
  • 10
  • 19