Table of Contents

IT:AD:MSTest:HowTo:Map Paths to Content Files

Summary

Some tests involve finding files in the BaseDirectory.

IT:AD:NUnit can do it just fine. MSTest can't.

Process

If you are relying on AppDomain.CurrentDomain.BaseDirectory to get the root directory, your test will break.

There are two fixes.

The Hack

The fix is explained at http://www.ademiller.com/blogs/tech/2008/01/gotchas-mstest-appdomain-changes-in-vs-2008/.

Basically, you need to set your BaseDirectory in your MSTest TestClass constructor like this:

string currDir = Environment.CurrentDirectory.Substring(0, Environment.CurrentDirectory.IndexOf("TestResults"));
AppDomain.CurrentDomain.SetData("APPBASE", currDir);

The Official mechanism

which is:

     /// <summary>
        ///   An Example Test.
        /// </summary>
        [TestMethod]
        [DeploymentItem(@"Templates\NV\Parts\SubHeader.nv",@"Templates\NV\Parts")]
        [DeploymentItem(@"Templates\NV\body.nv", @"Templates\NV")]
        [DeploymentItem(@"Templates\NV\Header.nv", @"Templates\NV")]
        public void UnitTest_TransformDIrectlyUsingVendorService()
        {

            string path = environmentService.MapPath(@"Templates\NV\body.nv");

            Assert.IsTrue(System.IO.File.Exists(path));
        }

References

* http://www.stormbase.net/converting-from-nunit-to-mstest * http://www.ademiller.com/blogs/tech/2008/01/gotchas-mstest-appdomain-changes-in-vs-2008/ * http://www.sidesofmarch.com/index.php/archive/2008/12/29/mstest-deploymentitem-and-the-frustrating-relativepathroot-setting/