views:

40

answers:

4

Let's say we have a short program:

namespace ConsoleTryIt
{
    static class Program
    {
        static void Main(string[] args)
        {
            var sum = Add(1, 2);
        }

        private static int Add(int p, int p2)
        {
            return p + p2;
        }
    }
}

When create the unit test class for this class, Visual Studio create a test method with the attribute DeploymentItem. I read MSDN about this attribute but still don't get what it means.

/// <summary>
///A test for Add
///</summary>
[TestMethod()]
[DeploymentItem("ConsoleTryIt.exe")]
public void AddTest()
{
    var expected = 122;
    var actual = Program_Accessor.Add(1, 121);
    Assert.AreEqual(expected, actual);
}

If you get the idea, please share!

Edit

Thanks everyone for your answers. So the idea is to copy the item given in the argument to the testing evironment's folder. My next question is: why does this method need this attribute while others don't?
I guess it's related to the private members on the tested class but nothing clear to me.

Please continue to discuss.

+2  A: 

This specifies files that are required by the specific test. The test system creates a new directory where the tests are run from. With this attribute, you can make the test system copy specific files to that new directory.

Pieter
+1  A: 

Is used deploy files that are not necessary present in the Output directory to the folder used for that particular TestRun.

in the example you posted above, the test environment makes sure that "consoleTryIt.exe" is copied(and therefore present) in the test folder. If the file is not found, the test is not even run, and a FileNotFound Exception is returned.

vaitrafra
+1  A: 

This means that the item is copied to the 'TestResults\Out' folder and is basically an artifact/necessary item for the test run. Thus, it is stored apart from the bin directory and does not get overwritten.
This is especially useful when running the tests in different environments (build server, no hard-coded paths...) and of course it is necessary to make the tests repeatable.

HTH.
Thomas

Thomas Weller
A: 
  • Ensures the files required for the test are copied over to the folder where MSTest runs its tests TestResults\Out.

  • Files in your solution should have "Copy Always" set for the files to be first copied to bin folder and then to MSTest's folder.

  • Make sure you have "Enable deployment" checked in testrunconfig.

anivas