Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make unit test run in bin folder

I'm trying to access a file in my solution structure during the unit test. My unit test project has the bin\Debug\ as the output directory. So I have written the code assuming that Path.GetFullPath(".") in my unit test will give me this bin folder. But what it does is it gives me a temporary location as the path.

C:\Users\[username]\AppData\Local\Temp\TestResults\[username]_[machine_name] 2013-05-16 08_31_07\Out

So obviously my unit test couldn't access the files in my solution. If anyone knows how to make unit test run in the bin folder of the unit test project please help.

like image 297
Geethanga Avatar asked May 16 '13 03:05

Geethanga


1 Answers

You can do this by using a .runsettings file, and setting <DeploymentEnabled>false</DeploymentEnabled>. See the "Remarks" section here. However, you can't do this if you are using a .testsettings file, and if you want to be able to inspect any files that your tests read or write after a failed run, you might not be able to, because they could be tainted by further tests etc.

Another option is to use deployment items, which can be done through the DeploymentItemAttribute or through your .testsettings file. The attribute mechanism is preferred, and basically, on test methods that you need to deploy files for you do the following:

[DeploymentItem(@"source", @"target")]
public void Test1() {}

Where source is either a path relative to the build output folder, or an absolute path, and target is either a path relative to where the tests run from, or an absolute path. You can leave the target parameter out, in this case it will assume a target of ".", ie the folder where the tests are running from. The docs for this are here

like image 186
guysherman Avatar answered Oct 06 '22 10:10

guysherman