Visual-studio – Unit testing: how to access a text file

unit testingvisual studiovisual-studio-2008

I'm using Visual Studio 2008 with Microsoft test tools. I need to access a text file from within the unit test.

I've already configured the file with build action set to 'Content' and copy to output directory to 'Copy Always', but the file is not being copied to the output directory, which according to System.Environment.CurrentDirectory is

'{project_path}\TestResults\Pablo_COMPU 2009-11-26 15_01_23\Out'

This folder contains all the DLL dependencies of the project, but my text file is not there.

Which is the correct way to access a text file from a unit test?

Best Answer

You have to add the DeploymentItem attribute to your test class. With this attribute you specify the files which are copied into the out directory for the test run.

For example:

[TestMethod]
[DeploymentItem(@"myfile.txt", "optionalOutFolder")]
public void MyTest()
{
    ...
}

See also: http://msdn.microsoft.com/en-us/library/ms182475.aspx.