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>
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.
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.
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.
You can copy this folder into the deployment folder of the unit tests with the DeploymentItem attribute.
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With