Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing Unit Tests: How to get folder with testfiles programmatically

Tags:

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> 
like image 388
hwcverwe Avatar asked Dec 24 '10 12:12

hwcverwe


People also ask

Which directory is used to store unit tests in your Android project *?

By default, the source files for local unit tests are placed in module-name/src/test/ . This directory already exists when you create a new project using Android Studio.

Where do I put unit 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. There is no single place that would make sense, so it's best to have them in a /tests directory.

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.


2 Answers

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

like image 199
Wim Coenen Avatar answered Oct 13 '22 22:10

Wim Coenen


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)) 
like image 40
Ferran Salguero Avatar answered Oct 13 '22 22:10

Ferran Salguero