MSTest copy file to test run folder

Visual StudioMstest

Visual Studio Problem Overview


I've got a test which requires an XML file to be read in and then parsed. How can I have this file copied into the test run folder each time?

The XML file is set to "Copy if newer" and a compile mode of "none" (since it's not really a compile-able thing)

Visual Studio Solutions


Solution 1 - Visual Studio

use a DeploymentItem attribute

using System;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using CarMaker;

namespace DeploymentTest
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod()]
        [DeploymentItem("testFile1.xml")]
        public void ConstructorTest()
        {
            string file = "testFile1.xml";
            Assert.IsTrue(File.Exists(file), "deployment failed: " + file +
                " did not get deployed");
        }
    }
}

Solution 2 - Visual Studio

It seems that if you provide a TestSettings file for the Solution then you can uncheck the "Enable deployment" option and stop mstest from trying to run from the ...TestResults\...\out folder where it doesn't copy your extra files (unless you make them a deployment option).

This is also useful if you depend on the extra files being in a preserved folder structure because Deployment items all seem to be copied directly (flat) into the temporary run folder (out) if you use the Deployment, Add Folder option in the TestSettings (answers above suggest you can keep the structure if you add each item as its own DeploymentItem).

For me it worked fine running tests directly in Visual Studio (i.e. my extra files in their structure were found and used by tests) because I had created a TestSettings file for another reason long ago (which has Enable deployment unchecked), but not when TeamCity ran mstest to run tests because I hadn't specified that the TestSettings file should be used.

To create a TestSettings file in Visual Studio, right click on the Solution and choose New Item, and select the TestSettings template. To use the TestSettings file at the command prompt of mstest.exe add the option, /testsettings:C:\Src\mySolution\myProject\local.testsettings (or add as an extra command line option in TeamCity with appropriate path)

Solution 3 - Visual Studio

The Preet answer is used to deploy items for a single test. If you want to do it at solution level, use the .testrunconfig settings.

Solution 4 - Visual Studio

Best solution to me is using testsettings, especially if multiple tests need the same datafiles.

First create a testsettings file, and add the deployment items you need (file or folder name):

<TestSettings name="Local" id="00ebe0c6-7b64-49c0-80a5-09796270f111" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
  <Description>These are default test settings for a local test run.</Description>
  <Deployment>
    <DeploymentItem filename="Folder1\TestScripts\test.xml" outputDirectory="TestScripts"/>
    <DeploymentItem filename="Folder2\TestData\" outputDirectory="TestData"/>
  </Deployment>
<...../>
  • Running in visual studio, use "select Test Settings File" from "Test\Test Settings" menu to select new testsettings

  • Running mstest, use the /testsettings parameter to have mstest use your testsettings.

Solution 5 - Visual Studio

You can define DeploymentItem in a class that holds a method with AssemblyInitialize attribute. Then you're sure that the files are copied regardless of which test you run.

Unfortunately DeploymentItem attribute is executed only on classes which contain tests you're running. So if you have 10 test classes which use the same set of files, you have to add the attribute to all of them.

Also found out that changes in *.testsettings files are not automatically refreshed in Visual Studio. Therefore after adding files / folders into deployment in testsettings, you have to reopen solution file and then run the tests.

Solution 6 - Visual Studio

In Visual Studio 2012, vstest.console.exe (the built-in test runner) runs with the output dir as the current path. This means that you only need to include the items in your solution with the 'Copy always' or 'Copy if newer' property for them to be used by your test. You don't need the DeploymentItem attribute for the general case. The same applies when running vstest.console.exe from the command line inside your output/test directory.

There are some cases where a separate folder is used, one of them being when you are using the DeploymentItem attribute. See here for more information.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAaron PowellView Question on Stackoverflow
Solution 1 - Visual StudioPreet SanghaView Answer on Stackoverflow
Solution 2 - Visual StudioTamWView Answer on Stackoverflow
Solution 3 - Visual StudioEric Bole-FeysotView Answer on Stackoverflow
Solution 4 - Visual StudioFrankyHollywoodView Answer on Stackoverflow
Solution 5 - Visual StudioSieluView Answer on Stackoverflow
Solution 6 - Visual StudioacarlonView Answer on Stackoverflow