[C#] Test Project, include Xml file

Caporegime
Joined
18 Oct 2002
Posts
29,493
Location
Back in East London
I have a Test project, that has an xml file included. But when I run the test, it does not 'include' the file in the test project working directory, ala.. there is no Xml file available.

e.g:
Code:
[TestClass]
public class MyTest
{
  private testData = new XmlDocument();

  [TestInitialize]
  public void Initialize()
  {
    testData.Load("Example.xml"); //FileNotFoundException
  }
}

I've added the Example.xml file to the project via "Add" > "Xml file", but I'm always getting FileNotFoundException... :/

Any advice on ensuring the file is included? No, I cannot use absolute paths. This test and project is run on many different machines. :)

EDIT: Already changed "Copy to output directory" as "Copy always"
 
Last edited:
Found the solution..
Code:
[TestClass]
public class MyTest
{
  private testData = new XmlDocument();

  [TestInitialize]
  public void Initialize()
  {
    testData.Load("Example.xml");
  }

  [TestMethod]
  [DeploymentItem("Example.xml")] // this line resolves problem
  public void TestFoo()
  {
    // some test
  }
}
 
Last edited:
Edited my post to make it a bit clearer :)

Yes I did solve the problem by applying the [DeploymentItem("Example.xml")] attribute to my test method - note not the Initialize() method!
 
Back
Top Bottom