How do I get a Unit Test to copy my DLLs and other files when I run a test?

≡放荡痞女 提交于 2019-12-10 02:11:33

问题


I'm working on an application and I have created a number of unit tests for it. The project with the test class depends upon 3 third party DLLs. When I go to the bin\Debug folder for the test project, the Dlls are there. But when I run the test, the DLLs are not being copied into the TestResult\\Out folder.

There is also a log4net.config file from another project that I would like to have copied. This one is not showing up in the test project's bin\Debug folder, so that's another issue I have to fix.

How do I get these files to copy when I run the unit test?

Tony


回答1:


We have a bin folder containing 3rd-party DLL's that have to be part of builds. They are flagged with the 'copy local' attribute in the reference.

As for individual files, you can do the same - Set 'Copy to output directory' to true.




回答2:


You can use a DeploymentItemAttribute to copy files to the bin (or other) directory.

[TestMethod()]
[DeploymentItem("log4net.config")]
public void SomeTest()
{
   ...
}



回答3:


I have found if your tests are being deployed to the test area (true by default), copy local won't work in some circumstances such as dynamic assembly loading.

You can either turn this deployment off by using a runsettings file (https://msdn.microsoft.com/en-us/library/ms182475.aspx) and

<DeploymentEnabled>False</DeploymentEnabled>

Or, a small hack (slightly ugly as it requires manual/hard coding the assembly), by using a DeploymentItem for the binary (mentioned in other answers, but, not specific to handling dlls as per the OP):

[DeploymentItem("bin\\release\\iRock.dll")]
[DeploymentItem("bin\\debug\\iRock.dll")]

Recommend doing both debug/release, depending on what is used on your CI/Dev.




回答4:


Such dll copying ( apart from referencing them - where you can say Copy Local) and putting them in the out folder should not be part of your tests, but part of your build / packaging process. Have build scripts which do the necessary copying of the dlls.




回答5:


When you debug from studio, use Deployment attribute on class or testmethod to copy the required DLLs and config files to the Out folder from where MSTests are run. If you run from command line, use a TestSettings file and disable the Deployment option and set your BIN folder as the working directory. Use/refer this TestSettings file in your command line for running mstest. This way, you can run mstest right in your BIN folder without dumping the DLLs into a out directory. Again, use deployment attribute to debug from studio, there testsettings will not work.



来源:https://stackoverflow.com/questions/7796268/how-do-i-get-a-unit-test-to-copy-my-dlls-and-other-files-when-i-run-a-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!