I've got unit tests I want to run against a sample XML file. How should I go about exposing these files to the unit tests? I've tried playing with the content build action, but I don't have access to an application context so GetContentStream is out.
I'm aware I can use a DataContext to a SQL Database, but I feel this is the wrong approach.
If you are looking to deploy the XML file with your tests, you have a few options:
Embedded Content
You can embed the Xml file as content within the assembly.
Example:
[TestMethod]
public void GetTheFileFromTheAssembly()
{
Stream fileStream = Assembly.GetExecutingAssembly()
.GetManifestResourceStream("MyNamespace.File.xml");
var xmlDoc = new XmlDocument();
xmlDoc.Load(fileStream);
Assert.IsNotNull( xmlDoc.InnerXml );
}
DeploymentItemAttribute
You can annotate the test method or class with a [DeploymentItemAttribute]. The file path is relative to the solution.
[DeploymentItem("file.xml")] // file is at the root of the solution
[TestMethod]
public void GetTheFileDeployedWithTheTest()
{
var xmlDoc = new XmlDocument();
xmlDoc.Load("file.xml");
Assert.IsNotNull(xmlDoc.InnerXml);
}
Test Settings
You can deploy individual files or entire directories using the Deployment configuration inside the Test Settings file. (Tests -> Edit Settings -> Filename.testsettings)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With