Unit Tests and Deployment Items

Sometimes one need an external file when running Unit Tests. The purpose can vary according to the situations:

Get information from a configuration file; Use the file as a source of data; …

Here are several possibilities to deploy those files when running Unit Tests :

Use The DeploymentItem Attribute on the TestClass. This is usefull for configuration files.

1
2
3
4
5
6
7
8
9
    /// <summary>
    /// Summary description for Test with files to deploy
    /// </summary>

    [TestClass, DeploymentItem("assembly.dll.config")]
    public class AssemblyTest
    {
        public AssemblyTest()
    {

Use the DeploymentItem Attribute on TestMethod. This is usefull for data files.

1
2
3
4
5
6
    [TestMethod, DeploymentItem("mydata.csv")]
    public void TestMyData()

    {
        . . .
    }

Set the CopyToOutputDirectory Property to always on the desired file (config or data).

Picture : Properties Build Action

Open the LocalTestRun.testrunconfig file and Add Deployment Items (the Enable deployment check box must be checked ) : Picture : TestRunConfig file

In all cases the build engine will copy the files to the test run directory. For instance, from the Solution directory , TestResults\User_Computer 2009-07-13 16_11_00\Out .

Note: I wrote this using Visual Studio 2008 SP1.