如何获得“复制到输出目录” 使用单元测试?

发布于 2024-07-07 00:49:04 字数 166 浏览 6 评论 0原文

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

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

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

青朷 2024-07-14 00:49:04

您可以指定部署属性,如下所示; 您还需要设置“内容”和“内容”。 “如果较新则复制”属性(没有关于后续设置的文档,但您已设置这些设置以使其正常工作。

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



}

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



}
慵挽 2024-07-14 00:49:04

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

The standard way to do this is by specifying the deployment items in the .testrunconfig file, which can be accessed via the Edit Test Run Configurations item in the Visual Studio Test menu or in the Solution Items folder.

风情万种。 2024-07-14 00:49:04

我必须在 Test -> 下打开“Enable Deployment” 编辑测试设置 -> 本地-> 部署 使[DeploymentItem] 属性发挥作用。

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

但可醉心 2024-07-14 00:49:04

这三个答案都是正确的,具体取决于您的需求。

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

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

希望这可以帮助。

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.

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.

Hope this helps.

我是有多爱你 2024-07-14 00:49:04

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

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

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

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

$(ProjectDir)$(TargetDir) 是将由 VS 解释的宏,因此应包含在内。

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

是要在其中部署测试文件的文件夹的名称,应该是命名以便当多个测试项目部署到同一目录时它是唯一的,例如_TestInputs

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

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

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

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

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

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

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

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

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.

<Project_Folder_Name> is the name of the folder created in step 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.

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".

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).

On a test method, source is the path to the file or directory used in the test method relative to the target directory as created by the xcopy and destination is 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 DeploymentItem should be included on every method that uses that file or directory.

On a class, source and destination are 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")]

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").

一身骄傲 2024-07-14 00:49:04

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

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.

烏雲後面有陽光 2024-07-14 00:49:04

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

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

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:

[DeploymentItem("bin\\release\\iRock.dll")]
[DeploymentItem("bin\\debug\\iRock.dll")]
音盲 2024-07-14 00:49:04

接受的答案是正确的,大多数其他答案也是正确的。 然而,多年来我发现,如果您有大量数据文件,使用 DeploymentAttribtue 和复制到输出的 Visual Studio 单元测试的部署系统会很麻烦。 我发现将文件保留在原始位置效果更好。

详细信息请参见我的其他答案。
https://stackoverflow.com/a/53004985/2989655

希望这有帮助。

The accepted answer is correct and so are most of the other answers. However, over the years I have found that the Deploment system of Visual Studio Unit Tests using DeploymentAttribtue and Copy to Output to be cumbersome if you have a large number of data files. I have found that keeping the files in their original location worked better.

Full details in my other answer here.
https://stackoverflow.com/a/53004985/2989655

Hope this helps.

愿与i 2024-07-14 00:49:04

在 Visual Studio 2012 中,对于简单情况,您不需要 DeploymentItem 属性。 请参阅我的回答此处

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

夜雨飘雪 2024-07-14 00:49:04

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

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

疯了 2024-07-14 00:49:04
[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";
}
[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";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文