Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
will get you the path of the running unit test DLL itself.
In order to access a file in a project subfolder, the simplest thing to do is mark the relevant files as Build Action = Content
and Copy to Output Directory = Copy if newer
. If you do that, then they will be copied to relative folders underneath the running assembly folder whenever you build the project. You can then calculate the physical path relative to the running assembly.
For example:
- You have a text file located at
<project root>\TestData\Data1.txt
.
- When you build the project in Debug mode, it will be copied to
<project root>\bin\Debug\TestData\Data1.txt
.
- The physical path in your code will be
Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "TestData\Data1.txt")
.