Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add content file in unit testing output directory

I'm new with unit testing in visual studio and I want to load a physical xml file. This file is in the unit testing project as a Content and it's copied in the output directory.

So, when I compile the project, the xml file is in the output directory. But when I execute the test, a new directory is created with all dependent DLL, but the xml file isn't copied.

The content of the Xml is needed to execute the test. I execute this code to retrieve the path of the Xml file in the execution folder :

private static string GetXmlFullName()
{
    // GetApplicationPath use the current DLL to find the physical path
    // of the current application
    string executeDirectory = AssemblyHelper.GetApplicationPath();
    return Path.Combine(executeDirectory, "content.xml");
}

The exception is :

System.IO.DirectoryNotFoundException: 'd:\***\solutiondirectory\testresults\*** 2012-06-13 17_59_53\out\content.xml'.

How can I add this file in the execute folder?

Thanks in advance. (and sorry for my english ...)

like image 503
Hyralex Avatar asked Jun 13 '12 16:06

Hyralex


People also ask

Where do I put unit test files?

Where to put test files. Unit tests run against specific lines of code. So it makes sense to place them right next to that code. Integration tests run against many lines of code in many files.

How do we define unit test within a text file?

A unit test is a way of testing a unit - the smallest piece of code that can be logically isolated in a system. In most programming languages, that is a function, a subroutine, a method or property. The isolated part of the definition is important.

How do you write unit test cases for file upload?

For file upload related testing we need to Mock HTTPContext, HTTPContext. Server and HttpPostedFileBase. These classes handle the file uploading and saving.


1 Answers

You need to place a DeploymentItemAttribute on the test class.

For example to include all files in the Data folder

[TestClass()]
[DeploymentItem("Data")]
public class MyTestClass
{
    ...
}
like image 112
Eric J. Avatar answered Oct 05 '22 23:10

Eric J.