Trouble with NUnit when determining the assembly's directory
Use can use TestContext.CurrentContext.TestDirectory
as mentioned by Charlie Poole from NUnit here:
The need to access files in the same directory as the test assembly is the most commonly cited reason for disabling shadow copy. However, this is spurious.
NUnit doesn't copy any assemblies: shadow copy is a function of .NET itself. Consequently, the problem needs to be viewed as "How can I access the file where it is?" rather than "How can I get the file copied to where I think it should be?"
There are three ways to locate a file that is located in the same directory as the assembly:
1) Use Assembly.Codebase - this will give you the location as a URI, which you must then tranform to an appropriate path.
2) Use the current directory, which NUnit has historically set to directory containing the executing test assembly. However, this may not be true for future releases.
3) Use NUnit's TestContext.CurrentContext.TestDirectory, which is the available in the most recent releases.
All of these approaches actually use Assembly.Codebase under the covers, with NUnit doing the work of tranforming the URI correctly in #2 and #3. The common approach of using Assembly.Location is incorrect, unless you actually want the location of the shadow copy cache.
For using reference files in my Unit Tests, I use Assembly.Codebase which works even if Shadow Copying is turned on. You might want to give it a try..
It returns a string in the Uri format.. so you'd need to create a Uri instance from the codebase string and use Uri.LocalPath to get the actual folder path.
However for production code, the BaseFolder should be retrieved from a well known place (e.g. a Registry key set via the installer on Windows). All file lookups should be rooted from this baseFolder.
Even if shadow copying is active, AppDomain.CurrentDomain.BaseDirectory
points to the original location of the test DLLs.
However, if you can embed your test data as resources in your DLL it's much safer, since there's no extra files that can get lost.