Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does `DeploymentItem` attribute mean?

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.

like image 841
Nam G VU Avatar asked Oct 18 '10 10:10

Nam G VU


3 Answers

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.

like image 177
Pieter van Ginkel Avatar answered Nov 18 '22 05:11

Pieter van Ginkel


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.

like image 31
vaitrafra Avatar answered Nov 18 '22 06:11

vaitrafra


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

like image 40
Thomas Weller Avatar answered Nov 18 '22 05:11

Thomas Weller