MSTest, DeploymentItem, and the frustrating RelativePathRoot setting

MSTest, Microsoft’s unit-testing framework, has the ability to deploy files to a predefined test directory via the [DeploymentItem] attribute. The documentation, however, includes this vague reference:

  • [DeploymentItem("file1.xml")] Deploys an item named file1.xml located at the RelativeRootPath. The file is deployed to the deployment root directory.
  • [DeploymentItem("file2.xml", "DataFiles")] Deploys an item named file2.xml located at the RelativeRootPath. The file is deployed to the DataFiles subdirectory of the deployment root directory.

Sounds good, but what is RelativePathRoot? It wasn’t easy to find out, but eventually I came across a post on the MSDN forums, which stated:

… it is simply the directory of the solution containing your test project.

Hmm… This is great, so long as your test project only exists in one solution — or, if in multiple solutions, all solutions are in the same folder. Unfortunately for me (and likely many others), this isn’t the case.

How can we handle situations where our test projects have deployment items, but the test projects are included in multiple solutions in different folders?

The messy but effective solution is to have more than one [DeploymentItem] attributes for each deployment item, each having a path that is relative from the solution directory. Fortunately, if the source file in a [DeploymentItem] attribute doesn’t exist, the test doesn’t fail – otherwise this workaround wouldn’t work.

In other words, if you are starting with this:

[TestMethod]
[DeploymentItem("TestFiles\MyFile.txt")]
public void MyTest() { }

You would modify it like this:

[TestMethod]
[DeploymentItem("TestFiles\MyFile.txt")]
[DeploymentItem("path_from_other_solution\TestFiles\MyFile.txt")]
public void MyTest() { }

Replace “path_from_other_solution” with the path from your alternate solution to the solution folder for the test project.

Not pretty, but it works.

One thought on “MSTest, DeploymentItem, and the frustrating RelativePathRoot setting”

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>