Gotchas: MSTest’s [DeploymentItem] attribute

October 2, 2007 – 3:15 pm

So we’ve been struggling with our usage of the DeploymentItem attribute within our unit tests on the Service Factory for pretty much the entire project. We had some odd requirements like copying dynamically loaded assemblies into our test deployment root in order to make them available during testing.

Essentially it seemed far too easy to create a test that worked fine on the local developer’s machine and failed either on the CI server or in someone else’s development environment. In fact 25% of our unit test related build breaks were caused by this issue and some of them took a while to track down!

A number of things have come to light. In order of general nastiness:

1) If the file referred to by DeploymentItem does not exist or cannot be copied the test doesn’t fail automatically. Your test code may fail because it cannot find the file but DeploymentItem just writes a warning into the MSTest log which you’ll probably never see.

Workaround:
Make DeploymentItem output visible in the build log. You can add the following to your build to parse the .TRX files and make DeploymentItem log entries into warnings or errors.

<CreateItem Include=$(MSTestOutputDir)\*.trx>
  <
Output ItemName=TestResultFiles TaskParameter=Include/>
</
CreateItem>
<Xml.GetValue
Path=%(TestResultFiles.Identity)
 
XPath=//Tests/TestRun/result/runInfoList/_items/element/text >
 
<Output ItemName=TestDeployErrorsTaskParameter=Results />
</Xml.GetValue>
<ErrorText=MSTest: %(TestDeployErrors.Identity)
 
Condition= ‘@(TestDeployErrors)’ != ” />

<UsingTask TaskName=Microsoft.Sdc.Tasks.Xml.GetValue
 
AssemblyFile=$(ToolsBinDir)Microsoft.SDC.Tasks.dll />

The SDC tasks, for those who haven’t used them are available on CodePlex.

2) If a file already exists then DeploymentItem will not copy it again. So if Test1 has a deploymentItem(”A.txt”) attribute and Test2 has the same DeploymentItem attribute A.txt will only get copied once. This is fine unless Test1 runs before Test2 and changes A.txt. In which case Test2 may fail because A.txt will contain unexpected data.

Workaround:
Use separate directories for each test. The way to get around this is always use OutputDirectory property on the attribute to give each test its own private working area. Like so:

[TestMethod]
[
DeploymentItem(@"A.txt", "Test1Files")]
public void Test1()
{
  //…
}
[
TestMethod]
[
DeploymentItem(@"A.txt", "Test2Files")]
public void Test2()
{
  //…
}

3) If a file gets deleted by a test DeploymentItem will not re-copy it. Subsequent tests will fail because the file they expect to be there will not exist.

Workaround:
You can use the same fix for issue #2 and give each test its own private working area.

4) The ordering of DeploymentItem attributes matters. The first DeploymentItem copies a file, others are ignored as per #2.

[TestMethod]
[
DeploymentItem(@"..\..\Data_2\Test.txt")]
[
DeploymentItem(@"..\..\Data_1\Test.txt")]
public void WhichItemGetsWrittenTest()
{
  string filePath = Path.Combine(
   
Directory.GetCurrentDirectory(), @”Test.txt”);
  string actual = File.ReadAllText(filePath);

 
 Assert.AreEqual(“1″, actual);
}

So this test fails. The “2″ version of Test.txt gets copied and the “1″ version does not. So the test reads “2″ from the file and fails. DeploymentItem does not error even though two attributes on the same test are trying to deploy the two files with the same name.

5) DeploymentItem is a method, not class attribute. If you want the same file present for all tests in a fixture then in principle you need to apply the attribute to every test method. MSTest seems to run test methods in the order they appear in the class you might be tempted to try and get away with adding the item to the first method. Relying on this is bad your tests are now order dependent! Do not do this.

How did I figure this out? Well I actually ended up writing a series of unit tests that used DeploymentItem. You can download them yourself from here.

I’m told much of this issue goes away in the upcoming Visual Studio release as MSTest doesn’t force you don’t have to “deploy” all your tests to a separate folder. If you do end up using DeploymentItem then do so with caution! My personal preference will be to move to storing data as resources and/or virtualizing the filesystem using a mock.

Share on:
  • Digg
  • description
  • del.icio.us
  • TwitThis
  • Live
  • Google
  1. 3 Responses to “Gotchas: MSTest’s [DeploymentItem] attribute”

  2. Google must have mis-interpreted the “Gotcha” and “Test” words, because I have advertisements for local STD testing centers.

    By Brian on Oct 3, 2007

  3. One other thing fixed in orcas — we have Class level deployment attributes, so you can put your dependancies there.

    However, the list of files is still collapsed into a flat list prior to test execution.

    One other — we dont execute tests in the order on the test class. While that maybe the behaviour your seeing, thats just luck… dont rely on it because it can change!

    By Dominic Hopton on Oct 4, 2007

  1. 1 Trackback(s)

  2. Nov 6, 2007: #2872 » Blog Archive » xUnit.net runs tests in random order

Post a Comment

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word