Unit Tests with Dependencies in Visual Studio and Team Foundation Server

When running unit tests as part of the build process on TFS (such as with continuous integration) the tests are executed on a folder dedicated to the test run, which contains the needed assemblies and will hold the test results. This folder is not the build output folder; instead, the assemblies are copied to that folder after the build. If you have some additional items that your tests depend on (e.g. a sample text file) will need to instruct the build to copy those items to the test run folder.

Suppose that we have a super useful class that has a method to check if a file is a txt file. In our test project we need some sample files, as illustrated bellow.

One way of doing this is through the test settings file (look for it under Solution Items and add one if not present). One can specify the additional files that should be deploy with the tests:

This file can then be selected as the settings file on the build definition for TFS:

When using this option, the files are deployed to the root of the folder, i.e. they will not keep the folder structure, even if you add the parent directory. This means that you can’t have files with the same name.  Also, the tests have to be written assuming that their dependencies are on the “current” folder:

        [TestMethod]
        public void TestIsTextFileWithTextFile()
        {
            bool res = FileUtils.IsTxtFile("TextFile.txt");
            Assert.IsTrue(res);
        }

        [TestMethod]
        public void TestIsTextFileWithXmlFile()
        {
            bool res = FileUtils.IsTxtFile("XMLFile.xml");
            Assert.IsFalse(res);
        }

Note that you’ll also need a local test settings file that includes the needed items so that local test runs are ok. If you don’t want to use test settings files (personally, I think that the test dependencies are not so clear that way) you can use the DeploymentItem attribute. This attribute enables the copy of an item at a given path to the test deployment folder (or another child folder). It can be used as illustrated bellow (note that the paths are relative to the output directory; to use them as is, enable the “Copy to output directory” option on each of them).

        [TestMethod]
        [DeploymentItem(@"TestFiles\TextFile.txt")]
        public void TestIsTextFileWithTextFile()
        {
            bool res = FileUtils.IsTxtFile("TextFile.txt");
            Assert.IsTrue(res);
        }

        [TestMethod]
        [DeploymentItem(@"TestFiles\XMLFile.xml", "Samples")]
        public void TestIsTextFileWithXmlFile()
        {
            bool res = FileUtils.IsTxtFile(@"Samples\XMLFile.xml");
            Assert.IsFalse(res);
        }

Note that to use the attribute you still have to check the “Enable Deployment” option on test settings.

Additional information:

DeploymentItemAttribute Class

Create Test Settings to Run Automated Tests from Visual Studio

How to: Configure Test Deployment

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s