Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access project files from NUnit tests

I have some Tests that I run with ReSharpers "Run All Tests from Solution" feature. One of the classes being tested has a dependency on a 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 "Copy always").

Problem: The tests are not being run from the normal assembly output directory, but instead some temporary location in my user profile.

Therefore, I don't really know where to look for the file - the test runner does not copy it there. Can I force it to?

like image 567
Daren Thomas Avatar asked May 27 '10 13:05

Daren Thomas


2 Answers

NUnit website recommends in this exact case to use Assembly.CodeBase property, that leads to the bin/debug I needed.

"Note: If you are tempted to disable shadow copy in order 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."

The .Location returned Uri style address "file:////D://Projects ... ", so the actual code I used was

string applicationDirectory = new Uri(Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase)).LocalPath;
like image 133
Roman Avatar answered Oct 20 '22 15:10

Roman


Sounds like you're running your tests with the Shadow Copy option turned on.

Go to Resharper->Options and select the Unit Testing tab (right at the bottom of the list). Uncheck "Shadow-copy assemblies being tested" and try again.

like image 32
Samuel Jack Avatar answered Oct 20 '22 15:10

Samuel Jack