24

I am writing unit tests in visual studio 2010.
For test some functionality, I have added a folder with testfiles.
I need to get this folder programmatically without a hard path in a string.

The folder contains in <projectDirectory>/TestFiles

I have tried to use AppDomain.CurrentDomain.BaseDirectory. This will only work if I run my unit tests with resharper.
result is <projectDirectory>/bin/debug so I can easily go to TestFiles.

If I am running test with visual studio, the BaseDirectory is:
<sameFolderAsSolutionFile>\TestResults\<username>_<pcname> <datatime>\Out

I have moved my solution file to another folder. So my projects aren't in the same folder as my solution file.
Example:
<sameFolderAsSolutionFile> = C:\SolutionFiles
<projectDirectory> = C:\Projects\MyProject

Can someone tell me how to get the path to my test-files without using a hardcoded string?

EDIT
I haven't found a solution yet.
Visual Studio is using another build folder for testing. So everything what is normally builded into the bin folder will be builded into another folder for the test.

MY TEMP-SOLUTION
I have added a App.config file in my test project. In this configuration file I have added a setting with the required path to the test files:

<appSettings>
  <add key="TestFiles" value="C:\Projects\MyProject\TestFiles"/>
</appSettings>
David Gardiner
  • 16,892
  • 20
  • 80
  • 117
hwcverwe
  • 5,287
  • 7
  • 35
  • 63

6 Answers6

21

You can copy this folder into the deployment folder of the unit tests with the DeploymentItem attribute.

Wim Coenen
  • 66,094
  • 13
  • 157
  • 251
13

Even the question is already answered, I want to contribute with a solution that worked for me, as did not the others. You will need to copy the file to the bin directory of the test project marking the file in its properties Copy to ouput directory = "Copy always", then in the test you can read the file with this code:

var baseDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var testFile = Path.Combine(baseDir, "TestFiles", "file.txt");

var file = File.OpenRead(testFile))
Ferran Salguero
  • 516
  • 10
  • 22
11

The most reliable way is to use TestContext. This object has whole bunch of properties for deployment path, results path etc. See TestContext Properties.

Shital Shah
  • 63,284
  • 17
  • 238
  • 185
4

You can use Path.Combine(Assembly.GetExecutingAssembly().Location,"TestFiles") to get to that Folder without hard coding anything. You have to make sure that you added the folder and files to your test project and have set its CopyToOutput directory appropriately.

Otiel
  • 18,404
  • 16
  • 78
  • 126
  • Location returns the full path including the name of the test DLL. You need to use Path.GetDirectoryName() to get just the path. – Eric J. May 12 '13 at 05:18
2

We do it this way:

var assembly = Assembly.GetAssembly(this.GetType());
var codebase = assembly.CodeBase.Replace("file:///", "");
var baseDir = Path.GetDirectoryName(codebase);

This C# code snippet comes from the unit test setup routine and retrieves the directory of the unit test code. From here you can navigate ... I do not think its very elegant...but :)

fausto
  • 67
  • 3
  • thanks for answer but this code will refer to the C:\Projects\TestResults\_ \Out folder instead of the project folder (C:\Projects\UnitTest\CompressionTest). so I need the project folder where I develop my unit test, not the folder where visual studio copy the assemby files only for unit tests. – hwcverwe Dec 24 '10 at 12:21
  • 1
    Probably you should note the difference between Location and CodeBase? Anyway look at: http://stackoverflow.com/questions/52797/c-how-do-i-get-the-path-of-the-assembly-the-code-is-in – fausto Dec 24 '10 at 13:08
  • 1
    Good gravy that's a lot of different approaches! Shouldn't there be one simple, authoritative way to do this? – Dan Csharpster May 30 '13 at 15:04
1

In case someone like me comes along, if you're using a .testsettings file and the DeploymentItem attribute on the class doesn't work even though you've set your files as Content and to Always Copy, it's because either you already have a Deployment section in your .testsettings file or you need to use DeploymentItem in the .testsettings file. Here's how ours looks now:

<Deployment>
    <DeploymentItem filename="Tests\Unit Tests\ConnectionStrings.config" />
    <DeploymentItem filename="Tests\Unit Tests\<project dir>\bin\Debug\TestFiles\" outputDirectory="TestFiles" />
</Deployment>

One does a file, the other does 13 files in a directory. Note the trailing slash for the directory's "filename"!

Solution and more info was found at:

MsTest DeploymentItem OutputDirectory in testsettings

Community
  • 1
  • 1
jgerman
  • 1,143
  • 8
  • 14