How to make unit test run in bin folder

13,536

Solution 1

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

Solution 2

Another option that may suit you is using NUnit instead of MSTest. In that case the tests are executed in the bin\debug directory

Solution 3

This is an update for anybody who might be running into this problem using Visual Studio 2015. Congruent to @guysherman, I have a Solution Items folder under the Solution file in my Solution explorer, and there should be a .testrunconfig file. If you open it using Visual Studios, Enable Deployment is a checkbox at the top that you can uncheck.

I had the same problem where my tests were deploying .dll's to a TestRun folder every time I ran the unit tests, but the .config file that was included with the executable references another common.config, which did not deploy with everything else, so I never connected to my SQL server because that was specified in the common.config. Unchecking the Enable Deployment option ran my tests right from the bin folder specified in the project.

Share:
13,536
Geethanga
Author by

Geethanga

.NET Developer

Updated on June 03, 2022

Comments

  • Geethanga
    Geethanga almost 2 years

    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.

  • Geethanga
    Geethanga almost 11 years
    Thanks for the reply. May be I haven't made my question clear. I want to run the unit test in the bin\debug folder, not in that temporary location. Because during the unit test I will be creating a file which will be used by my application. So for that I need to access one of the folders in solution during the unit test. Because the unit test is running in a dynamic temp location it's really difficult to achieve.
  • guysherman
    guysherman almost 11 years
    See the first part... "<DeploymentEnabled>false</DeploymentEnabled>", and the documentation about the run settings file. That will allow you to run in the bin\Debug folder.
  • Igor Lankin
    Igor Lankin almost 11 years
    Thanks a lot! Disabling DeploymentEnabled in my .testsettings and deploying via [DeploymentItem(TestDataFolder, "TestData")] worked.
  • IbrarMumtaz
    IbrarMumtaz almost 11 years
    This is not true, I tried this and i fond the same behaviour. Love to be proven wrong. Doesn't matter if you use MsTest or NUnit.
  • Ron Sher
    Ron Sher almost 11 years
    I suggest you create a sample solution (like I did) to prove it (I'd be happy to mail you the source code if you need any help). I ran 2 versions - 1 in mstest and 1 in nunit. When running in nunit this is what I got - "D:\work\rampup\nunit-test\NunitTest\NunitTest\bin\Debug" while in mstest this is what I got - "C:\Users\ron\AppData\Local\Temp\TestResults\ron_RON-PC 2013-07-29 16_38_27\Out"
  • IbrarMumtaz
    IbrarMumtaz over 10 years
    that looks like a good spot! Will have to try maybe my home dev environment is not setup properly perhaps i dont know.
  • Jahmic
    Jahmic over 8 years
    You're both right. If you use NUnit console to run the tests, it will use the default folder. If you are running your NUnit tests from VS Test Explorer, it will use an alternate location.
  • Ariel Popovsky
    Ariel Popovsky almost 6 years
    This solution worked for me but I cannot persist it in the solution. Basically, if I commit the files and someone else gets it, they need to manually select the runsettings file. I want this to be persistent for this solution.