如何使单元测试在bin文件夹中运行 [英] How to make unit test run in bin folder

查看:123
本文介绍了如何使单元测试在bin文件夹中运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在单元测试期间访问解决方案结构中的文件.我的单元测试项目将bin\Debug\作为输出目录.因此,我编写的代码假定单元测试中的Path.GetFullPath(".")将给我这个bin文件夹.但是它的作用是为我提供了一个临时位置作为路径.

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

因此,很明显,我的单元测试无法访问我的解决方案中的文件.如果有人知道如何在单元测试项目的bin文件夹中运行单元测试,请提供帮助.

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.

推荐答案

您可以通过使用.runsettings文件并设置<DeploymentEnabled>false</DeploymentEnabled>来完成此操作.请参见备注"部分此处.但是,如果您使用的是.testsettings文件,则无法执行此操作,并且如果您希望能够检查测试在运行失败后读取或写入的任何文件,则可能无法执行,因为它们可能是受到进一步测试等的污染.

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.

另一种选择是使用部署项目,可以通过DeploymentItemAttribute或通过.testsettings文件来完成.属性机制是首选的,基本上,在需要部署文件的测试方法上,请执行以下操作:

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() {}

其中 source 是相对于构建输出文件夹的路径,还是绝对路径,而 target 是相对于测试运行位置的路径,或者绝对路径.您可以省略目标参数,在这种情况下,它将假定目标为.",即运行测试的文件夹.此文档位于此处

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

这篇关于如何使单元测试在bin文件夹中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆