C# 如何让“复制到输出目录”与单元测试一起使用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/227545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 18:59:47  来源:igfitidea点击:

How can I get "Copy to Output Directory" to work with Unit Tests?

c#.netvisual-studio-2008unit-testingmstest

提问by Eric Schoonover

When I build a unit test project before the tests are executed the test output is copied to a TestResults folder and then the tests are executed. The issue I'm having is that not all the files in the Debug/bin directory are copied to the TestResults project.

当我在执行测试之前构建单元测试项目时,测试输出将复制到 TestResults 文件夹,然后执行测试。我遇到的问题是并非 Debug/bin 目录中的所有文件都被复制到 TestResults 项目。

How can I get a file that is copied to the Debug/bin directory to also be copied to the TestResults folder?

如何将复制到 Debug/bin 目录的文件也复制到 TestResults 文件夹?

采纳答案by Mark Cidade

The standard way to do this is by specifying the deployment itemsin the .testrunconfigfile, which can be accessed via the Edit Test Run Configurationsitem in the Visual Studio Testmenu or in the Solution Itemsfolder.

执行此操作的标准方法是在文件中指定部署项.testrunconfig,可以通过Visual Studio测试菜单或解决方案项文件夹中的编辑测试运行配置项访问该文件。

回答by Kasper

Try out the Post-Build event command line from within Visual Studio (if you are using that IDE).

从 Visual Studio 中尝试构建后事件命令行(如果您使用的是该 IDE)。

回答by Sanjay10

You can specify deployment attribute like an example shown below; Also you need to set "Content" & "Copy if newer" property ( there is no documentation on the later settings, but you have set those to make it work.

您可以像下面显示的示例一样指定部署属性;您还需要设置“内容”和“如果较新则复制”属性(没有关于以后设置的文档,但您已经设置了它们以使其工作。

[TestMethod]
[DeploymentItem("mytestdata.xml")]
public void UploadTest()
{



}

回答by GraehamF

All three answers are correct, depending on your needs.

根据您的需要,所有三个答案都是正确的。

Adding files to deploy in the .testrunconfig(.testsettings in VS2010) will copy all of those files to every test output folder, even for unrelated tests run in isolation. If you run one test, all the test data files listed in the deployment section of .testssettings will be copied to the test output folder.

添加要在.testrunco​​nfig 中部署的文件(VS2010中的.testsettings)会将所有这些文件复制到每个测试输出文件夹,即使是独立运行的无关测试也是如此。如果您运行一个测试,则 .testssettings 的部署部分中列出的所有测试数据文件都将复制到测试输出文件夹中。

In my tests I need to copy an expected XML file to the test output folder to compare with the actual test output XML. I use the DeploymentItem attribute to only copy the XML file related to the test(s) being run. In VS2010 I had to enable deployment in the .testsettings file (but not add any paths) and then reference the XML file path relative to the TestProject in the DeploymentItem.

在我的测试中,我需要将预期的 XML 文件复制到测试输出文件夹,以与实际的测试输出 XML 进行比较。我使用 DeploymentItem 属性仅复制与正在运行的测试相关的 XML 文件。在 VS2010 中,我必须在 .testsettings 文件中启用部署(但不添加任何路径),然后在 DeploymentItem 中引用相对于 TestProject 的 XML 文件路径。

Hope this helps.

希望这可以帮助。

回答by tomfanning

I had to turn on "Enable Deployment" under Test -> Edit Test Settings -> Local -> Deploymentfor the [DeploymentItem]attribute to work.

我必须打开“启用部署”下Test -> Edit Test Settings -> Local -> Deployment[DeploymentItem]属性才能工作。

回答by Simon

I had a similar problem but mine had to do with pointing to the TraceAndTestImpact.testsettings file instead of the Local.testsettings file. You can change from one to the other under the Test/Select Active Test Settings menu.

我有一个类似的问题,但我的问题是指向 TraceAndTestImpact.testsettings 文件而不是 Local.testsettings 文件。您可以在“测试/选择活动测试设置”菜单下从一种更改为另一种。

回答by acarlon

In Visual Studio 2012 you don't need a DeploymentItem attribute for the simple case. See my answer here

在 Visual Studio 2012 中,对于简单情况,您不需要 DeploymentItem 属性。在这里看到我的答案

回答by Richard Morris

The following works in VS2012 for test projects included in multiple solutions without using a testsettings file:

以下在 VS2012 中适用于包含在多个解决方案中的测试项目,而无需使用 testsettings 文件:

1) Arrange the files and folders you wish to deploy into a folder in the test project directory.

1) 将要部署的文件和文件夹整理到测试项目目录中的一个文件夹中。

2) In the project properties, create a post build step

2)在项目属性中,创建一个后期构建步骤

xcopy /Y /S /i "$(ProjectDir)<Project_Folder_Name>\*" "$(TargetDir)<Deployment_Folder_Name>"

$(ProjectDir)and $(TargetDir)are macros that will be interpreted by VS and should be included as such.

$(ProjectDir)$(TargetDir)是将由 VS 解释的宏,应该被包括在内。

<Project_Folder_Name>is the name of the folder created in step 1.

<Project_Folder_Name>是在步骤 1 中创建的文件夹的名称。

<Deployment_Folder_Name>is the name of the folder in which the test files will be deployed and should be named so that it will be unique when multiple test projects are deployed to the same directory, e.g. <Project_Name>_TestInputs.

<Deployment_Folder_Name>是将部署测试文件的文件夹的名称,并且应该命名以便在将多个测试项目部署到同一目录时它是唯一的,例如<Project_Name>_TestInputs.

Test files in shared locations should also be copied to the target directory deployment folder to limit test interactions. Provide the source path relative to the $(ProjectDir)macro. For example "$(ProjectDir)..\..\Common Files\C1219TDL-2008.xml".

共享位置中的测试文件也应复制到目标目录部署文件夹以限制测试交互。提供相对于$(ProjectDir)宏的源路径。例如"$(ProjectDir)..\..\Common Files\C1219TDL-2008.xml"

3) Add a [DeploymentItem(source, destination)]property to either each test method that uses a deployment file (best practice) or to the test class (easier practice for the lazy or hurried, and the easiest way to update a project the previously used relative paths or a testsettings file).

3)[DeploymentItem(source, destination)]为每个使用部署文件的测试方法(最佳实践)或测试类(懒惰或匆忙的人更容易实践,以及更新项目先前使用的相对路径或测试设置文件的最简单方法)添加一个属性)。

On a test method, sourceis the path to the file or directory used in the test method relative to the target directory as created by the xcopyand destinationis the path to the directory in which it will be created relative to the deployment directory. So that tests run consistent in either the target directory or a deployment directory. The destination path should be the same as the source path without a file reference. Example: [DeploymentItem("Example_TestInputs\C1219TDL-2008.xml","Example_TestInputs")]. The DeploymentItemshould be included on every method that uses that file or directory.

在测试方法上,source是测试方法中使用的文件或目录相对于由 所创建的目标目录的路径xcopydestination是将在其中创建的目录相对于部署目录的路径。以便测试在目标目录或部署目录中运行一致。目标路径应该与没有文件引用的源路径相同。例子: [DeploymentItem("Example_TestInputs\C1219TDL-2008.xml","Example_TestInputs")]。该DeploymentItem应包括对使用该文件或目录的每一个方法。

On a class, sourceand destinationare both the name of the folder created in the target directory by the xcopy; this will copy the entire folder to the deployment directory when any test in the class is run. Example: [DeploymentItem("Example_TestInputs","Example_TestInputs")]

在一个类上,sourcedestination都是由xcopy;在目标目录中创建的文件夹的名称。当运行类中的任何测试时,这会将整个文件夹复制到部署目录。例子:[DeploymentItem("Example_TestInputs","Example_TestInputs")]

4) In the test methods, you can now access files and directories with confidence they will be in the working directory regardless of where Visual Studio has decided to put it that day, e.g. File.Exists(".\Example_TestInputs\C1219TDL-2008.xml").

4) 在测试方法中,您现在可以放心地访问文件和目录,它们将位于工作目录中,而不管 Visual Studio 当天决定将其放在何处,例如File.Exists(".\Example_TestInputs\C1219TDL-2008.xml").

回答by JamesDill

Would like to just enhance the accepted answer by mentioning a way to get it to deploy specifically for dll's rather then the normal method of using it for data or config etc, for the circumstances where CopyLocal doesn't work:

在 CopyLocal 不起作用的情况下,只想通过提及一种使其专门针对 dll 进行部署的方法而不是将其用于数据或配置等的正常方法来增强已接受的答案:

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

回答by Nina

[TestMethod]
[DeploymentItem("ProjectName/Folder/SubFolder/file.xml", "Folder/Subfolder")]
public void YourTestMethod()
{
   // in the method you are testing you should have this:
   var filePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase) + "/Folder/Subfolder/file.xml";
}