简体   繁体   中英

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.

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

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

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.

We ran into this issue with VS 2017, but our problem was with log4net.config not getting copied to the TestResults folder (even though Copy Always was true).

Our solution was to delete the LocalTestRun.testrunconfig file from the Solution Items folder (directly under the solution). When we did that it started using the \\bin\\debug\\ folder as it should and found our log4net.config file.

It seems like a Visual Studio bug. Here's what I did to fix it (which magically worked!):

  1. Moved/Renamed the local.testsettings file.
  2. Tried to run the test again (Which will not run)
  3. Put local.testsettings file back again.
  4. It should run the failing test now!

If you are trying to access files or resources that you are assuming are in the default folder, you could try giving them the explicit directory of the assembly location. An example:

string dataSource = AppDomain.CurrentDomain.BaseDirectory + "TestDb.mdb";

AppDomain.CurrentDomain.BaseDirectory usually resolves to the actual assembly location, which is what you were planning on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM