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?
To start the New Load Test Wizard In Solution Explorer, open the shortcut menu for the Bank solution node, choose Add, and then choose New Project. The Add New Project dialog box displays. In the Add New Project dialog box, expand Visual C# and choose Test.
Run tests in Test Explorer If Test Explorer is not visible, choose Test on the Visual Studio menu, choose Windows, and then choose Test Explorer (or press Ctrl + E, T). As you run, write, and rerun your tests, the Test Explorer displays the results in a default grouping of Project, Namespace, and Class.
You can also create a Visual Basic load test project, if you prefer. Open Visual Studio. On the start window, choose Create a new project. On the Create a new project page, type web test into the search box, and then select the Web Performance and Load Test Project [Deprecated] template for C#.
You need to close the Test Explorer Window to prevent automatic running.
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
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
.
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