[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:
Have you solved it then, or is it still not working?

Edit: never mind then :)

Edit 2: you can also just change the build action to "None", and it'll then copy to the output directory.
 
Last edited:
I was going to suggest that you mightn't need the .xml at the end of the file name.

When loading content in XNA you do not use the file extension, it might have been the same.
 
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