Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm trying to access a file in my solution structure during the unit test. My unit test project has the binDebug 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]AppDataLocalTempTestResults[username]_[machine_name] 2013-05-16 08_31_07Out

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
671 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...