How to access project files from NUnit tests

I have some tests that I run using the ReSharpers function “Run all tests from a solution”. One of the tested classes depends on the file in the same folder as the assembly containing it. This file is copied to the output directory via MSBuild (set "Copy to output directory" to "Always copy").

Problem: Tests do not run from the normal build output directory, but instead have some temporary location in my user profile.

Therefore, I do not know where to look for the file - the test runner does not copy it there. Can I make him?

+5
source share
2 answers

It looks like you are running your tests with the Shadow Copy option enabled.

Go to the "Restarper->" tab and select the "Testing device" tab (bottom right of the list). Uncheck the box next to "Test shadow copy" and try again.

+4
source

The NUnit site recommends using the Assembly.CodeBase property in this particular case, which necessitates the use of bin / debug.

"Note: If you are tempted to disable the shadow copy to access files in the same directory as your assembly, you should be aware that there are alternatives. Consider using the Assembly.Codebase property rather than Assembly.Location."

.Location Uri "file:////D://Projects...", , ,

string applicationDirectory = new Uri(Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase)).LocalPath;
+6

All Articles