Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio - Unit tests loading resources in the project

Tags:

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?

like image 798
p.campbell Avatar asked Oct 08 '09 23:10

p.campbell


People also ask

How do you do a load test in Visual Studio?

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.

How do I run a unit test project in Visual Studio?

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.

How do I create a load test project in Visual Studio 2019?

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#.

How do I stop a test case from running in Visual Studio?

You need to close the Test Explorer Window to prevent automatic running.


2 Answers

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

like image 176
Pieter Müller Avatar answered Oct 16 '22 21:10

Pieter Müller


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.

like image 32
ChrisW Avatar answered Oct 16 '22 22:10

ChrisW