Writing Unit Tests: How to get folder with testfiles programmatically

33,710

Solution 1

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

Solution 2

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))

Solution 3

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

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

Solution 5

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 :)

Share:
33,710
hwcverwe
Author by

hwcverwe

Wilfred Verweij Bachelor of ICT since 2010 Mission Critical Engineer by Schuberg Philis Mainly .Net and Microsoft Azure oriented.

Updated on July 18, 2022

Comments

  • hwcverwe
    hwcverwe almost 2 years

    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>