Visual Studio - Unit tests loading resources in the project

26,699

Solution 1

In the unit test project add a post-build event that copies the XML file to the output directory. Then, you can use your original code to get the XML file.

The post build event will look like something like this:

copy $(SolutionDir)file.xml $(ProjectDir)$(OutDir)file.xml

You may also need this to add to your path:

Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)

Solution 2

There is a Visual Studio Unit Testing feature for this: DeploymentItemAttribute

I use this feature to copy all xml files in a given project folder to the unit test output folder, before testing if all required files are present.

You can use this attribute with your unit tests to copy specific files from the Project folder (or anywhere else) to the Unit Test output folder. Like so:

[TestMethod()]
[DeploymentItem("MyProjectFolder\\SomeDataFolder\\somefile.txt", "SomeOutputSubdirectory")]
public void FindResourcefile_Test()
{
    string fileName = "SomeOutputSubdirectory\\somefile.txt";
    Assert.IsTrue(System.IO.File.Exists(fileName));
}

You can also copy the contents of whole folders:

[TestMethod()]
[DeploymentItem("MyProjectFolder\\SomeDataFolder\\", "SomeOutputSubdirectory")]
public void FindResourcefile_Test()
{
    string fileName = "SomeOutputSubdirectory\\someOtherFile.txt";
    Assert.IsTrue(System.IO.File.Exists(fileName));
}

The first parameter is the source, the second the destination folder. The source is relative to your solution folder (so you can access the Unit Test project of the project being tested) and the destination is relative to the output folder of the unit test assembly.

UPDATE:

You need to enable Deployment in the Test Settings for this to work. This MSDN page explains how (it's real easy): http://msdn.microsoft.com/en-us/library/ms182475(v=vs.90).aspx#EnableDisableDeploy

Solution 3

You can build those files into your executable (set their "Build Action" property to "Embedded Resource") and then get them using the Assembly.GetManifestResourceStream method.

Solution 4

I use a helper class to deal with getting basic paths I might want to access in my Unit Tests.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Brass9.Testing
{
    public static class TestHelper
    {
        public static string GetBinPath()
        {
            return System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
        }

        public static string GetProjectPath()
        {
            string appRoot = GetBinPath();
            var dir = new DirectoryInfo(appRoot).Parent.Parent.Parent;
            var name = dir.Name;
            return dir.FullName + @"\" + name + @"\";
        }

        public static string GetTestProjectPath()
        {
            string appRoot = GetBinPath();
            var dir = new DirectoryInfo(appRoot).Parent.Parent;
            return dir.FullName + @"\";
        }

        public static string GetMainProjectPath()
        {
            string testProjectPath = GetTestProjectPath();
            // Just hope it ends in the standard .Tests, lop it off, done.
            string path = testProjectPath.Substring(0, testProjectPath.Length - 7) + @"\";
            return path;
        }
    }
}

Sometimes my interactions with paths are more complex; I often use a central class I name "App" to indicate some basic details about the application, like its root folder, its root namespace and module, etc. Classes will sometimes depend on App's existence, and so instead I'll place an init method on App that uses code like the above to initialize itself for test harnesses, and call that method from the Init command in a Unit Test.

(Updated)

Old Answer

I found this helps for getting arbitrary paths to access files in the project folder you intend to test (as opposed to files in the Test project folder, which can make busywork if you need to copy things over).

DirectoryInfo projectDir = new DirectoryInfo(@"..\..\..\ProjectName");
string projectDirPath = projectDir.FullName;

You can then use either of those variables to access whatever you need from the related project. Obviously swap "ProjectName" out for the actual name of your project.

Solution 5

Resources are just resources and that's it, no need to complicate. If you don't want to embed them then you could add these files as "Content" resources to your project and set them to Copy always. Then specify the sub-folder in your code:

var xmlDoc = XElement.Load("ProjectSubFolder\\Resource.xml");

This will automatically load the resources from the project output (running assembly location) bin\$(Configuration)\ResourceSubfolder\

This works for all types of projects, not just unit tests.

Share:
26,699
p.campbell
Author by

p.campbell

Developer in the Microsoft .NET & SQL Server stack. I am focused on delivering great applications in small iterations. I've developed solutions in marketing, healthcare, manufacturing, and transportation verticals. My open source projects on GitHub. Continuously learning.

Updated on August 01, 2022

Comments

  • p.campbell
    p.campbell almost 2 years

    The goal is to run some tests given some data in those Xml files.

    How would you easily load a given Xml file into an XmlDoc within the unit test methods?

    Current state is:

      XmlDocument doc = new XmlDocument();
      string xmlFile = "4.xml";
      string dir = System.IO.Directory.GetCurrentDirectory() + @"\Msgs\" 
    
      //dir is then the value of the current exe's path, which is
      //d:\sourcecode\myproject\TestResults\myComputer 2009-10-08 16_07_45\Out
    
      //we actually need:
      //d:\sourcecode\myproject\Msgs\ 
      doc.Load( dir + fileName); //should really use System.IO.Path.Combine()!
    

    Is it just a simple matter of putting that path in an app.config? I was hoping to avoid that, given the possibility of different paths on developer machines.

    Question: How would you write the algorithm to load a given Xml file into an XmlDocument in the unit test method?