NUnit read file from source Bin
I've a class library in visual studio with a method that just checks if specified file exists or not. If I pass just file name (without full path) of some text file which exists in the bin directory, it works fine by identifying its existence.
Hence File.Ex开发者_如何学Goists("myfile.txt") works if myfile.txt is in bin directory.
But when I load a test case from NUnit GUI which executes this method, it fails to read the file. Likely because bin directory executing NUnit is different than original bin where dll and myfile.txt reside. How can I tackle this in my NUnit without resorting to hardcoded full path?
In your tests pass a relative path to the method of the class under test. This avoids resorting to a hard coded full path and as long as your test project is always in the same location relative to your source project it'll work.
e.g. if you have your source set up like this:
\Solution\src\Project\bin\debug\myFile.txt
\Solution\test\TestProject\bin\debug\TestAssembly.dll
The relative path will be @"..\..\..\..\Project\bin\debug\myfile.txt"
Update
I'm not quite sure why your tests are running from a temporary folder. I either use a test runner such as Resharper or set up my test project as follows:
- Open the project properties for the project containing your tests.
- Go to the Debug tab and set the following values:
- Start external program: Enter the location of nunit.exe, e.g. on my PC it's installed to C:\Program Files\NUnit 2.5.5\bin\net-2.0\nunit.exe.
- Command line arguments: Enter the name of your assembly containing your tests followed by the run argument, e.g. TestProject.dll /run.
- Set the project containing your tests as the StartUp Project.
- Hit F5.
This way your tests will always run from bin\debug
(depending on how your build is configured), so you can rely on projects always being in the same relative location.
精彩评论