Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing: how to access a text file?

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?

like image 241
Pablote Avatar asked Nov 26 '09 18:11

Pablote


People also ask

How do you read a resource file in JUnit?

File class to read the /src/test/resources directory by calling the getAbsolutePath() method: String path = "src/test/resources"; File file = new File(path); String absolutePath = file. getAbsolutePath(); System. out.

How do I run a unit test file?

To run all the tests in a default group, choose the Run icon and then choose the group on the menu. Select the individual tests that you want to run, open the right-click menu for a selected test and then choose Run Selected Tests (or press Ctrl + R, T).


1 Answers

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.

like image 181
Alexander Egger Avatar answered Oct 12 '22 01:10

Alexander Egger