visual-studio MSTest 复制文件到测试运行文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/649106/
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-10-22 10:16:53  来源:igfitidea点击:

MSTest copy file to test run folder

visual-studiomstest

提问by Aaron Powell

I've got a test which requires an XML file to be read in and then parsed. How can I have this file copied into the test run folder each time?

我有一个测试需要读入一个 XML 文件然后解析。如何每次都将此文件复制到测试运行文件夹中?

The XML file is set to "Copy if newer" and a compile mode of "none" (since it's not really a compile-able thing)

XML 文件设置为“如果较新则复制”和“无”编译模式(因为它不是真正可编译的东西)

回答by Preet Sangha

use a DeploymentItemattribute

使用DeploymentItem属性

using System;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using CarMaker;

namespace DeploymentTest
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod()]
        [DeploymentItem("testFile1.xml")]
        public void ConstructorTest()
        {
            string file = "testFile1.xml";
            Assert.IsTrue(File.Exists(file), "deployment failed: " + file +
                " did not get deployed");
        }
    }
}

回答by TamW

It seems that if you provide a TestSettings file for the Solution then you can uncheck the "Enable deployment" option and stop mstest from trying to run from the ...TestResults\...\outfolder where it doesn't copy your extra files (unless you make them a deployment option).

似乎如果您为解决方案提供了一个 TestSettings 文件,那么您可以取消选中“启用部署”选项并阻止 mstest 尝试从...TestResults\...\out它不复制额外文件的文件夹中运行(除非您将它们设为部署选项) .

This is also useful if you depend on the extra files being in a preserved folder structure because Deployment items all seem to be copied directly (flat) into the temporary run folder (out) if you use the Deployment, Add Folder option in the TestSettings (answers above suggest you can keep the structure if you add each item as its own DeploymentItem).

如果您依赖保留文件夹结构中的额外文件,这也很有用,因为如果您使用 TestSettings 中的部署、添加文件夹选项,部署项目似乎都被直接(平面)复制到临时运行文件夹(输出)中(上面的答案建议您可以保留结构,如果您将每个项目添加为自己的 DeploymentItem)。

For me it worked fine running tests directly in Visual Studio (i.e. my extra files in their structure were found and used by tests) because I had created a TestSettings file for another reason long ago (which has Enable deployment unchecked), but not when TeamCity ran mstest to run tests because I hadn't specified that the TestSettings file should be used.

对我来说,它直接在 Visual Studio 中运行测试运行良好(即我在它们的结构中找到并被测试使用了我的额外文件),因为我很久以前因为另一个原因创建了一个 TestSettings 文件(它未选中启用部署),但不是当 TeamCity运行 mstest 来运行测试,因为我没有指定应该使用 TestSettings 文件。

To create a TestSettings file in Visual Studio, right click on the Solutionand choose New Item, and select the TestSettings template. To use the TestSettings file at the command prompt of mstest.exe add the option, /testsettings:C:\Src\mySolution\myProject\local.testsettings(or add as an extra command line option in TeamCity with appropriate path)

要在 Visual Studio 中创建 TestSettings 文件,请右键单击解决方案并选择 New Item,然后选择 TestSettings 模板。要在 mstest.exe 的命令提示符下使用 TestSettings 文件,请添加选项,/testsettings:C:\Src\mySolution\myProject\local.testsettings(或在 TeamCity 中使用适当的路径添加为额外的命令行选项)

回答by Eric Bole-Feysot

The Preet answer is used to deploy items for a single test. If you want to do it at solution level, use the .testrunconfig settings.

Preet 答案用于为单个测试部署项目。如果您想在解决方案级别执行此操作,请使用.testrunco​​nfig 设置

回答by FrankyHollywood

Best solution to me is using testsettings, especially if multiple tests need the same datafiles.

对我来说最好的解决方案是使用测试设置,特别是如果多个测试需要相同的数据文件。

First create a testsettings file, and add the deployment items you need (file or folder name):

首先创建一个testsettings文件,并添加你需要的部署项(文件名或文件夹名):

<TestSettings name="Local" id="00ebe0c6-7b64-49c0-80a5-09796270f111" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
  <Description>These are default test settings for a local test run.</Description>
  <Deployment>
    <DeploymentItem filename="Folder1\TestScripts\test.xml" outputDirectory="TestScripts"/>
    <DeploymentItem filename="Folder2\TestData\" outputDirectory="TestData"/>
  </Deployment>
<...../>
  • Running in visual studio, use "select Test Settings File" from "Test\Test Settings" menu to select new testsettings

  • Running mstest, use the /testsettings parameter to have mstest use your testsettings.

  • 在visual studio中运行,使用“Test\Test Settings”菜单中的“select Test Settings File”来选择新的testsettings

  • 运行 mstest,使用 /testsettings 参数让 mstest 使用您的测试设置。

回答by Sielu

You can define DeploymentItem in a class that holds a method with AssemblyInitialize attribute. Then you're sure that the files are copied regardless of which test you run.

您可以在包含具有 AssemblyInitialize 属性的方法的类中定义 DeploymentItem。然后您确定无论您运行哪个测试,文件都会被复制。

Unfortunately DeploymentItem attribute is executed only on classes which contain tests you're running. So if you have 10 test classes which use the same set of files, you have to add the attribute to all of them.

不幸的是,DeploymentItem 属性仅在包含您正在运行的测试的类上执行。因此,如果您有 10 个使用相同文件集的测试类,则必须向所有这些类添加该属性。

Also found out that changes in *.testsettings files are not automatically refreshed in Visual Studio. Therefore after adding files / folders into deployment in testsettings, you have to reopen solution file and then run the tests.

还发现 *.testsettings 文件中的更改不会在 Visual Studio 中自动刷新。因此,在测试设置中将文件/文件夹添加到部署中后,您必须重新打开解决方案文件,然后运行测试。

回答by acarlon

In Visual Studio 2012, vstest.console.exe (the built-in test runner) runs with the output dir as the current path. This means that you only need to include the items in your solution with the 'Copy always' or 'Copy if newer' property for them to be used by your test. You don't need the DeploymentItem attribute for the general case. The same applies when running vstest.console.exe from the command line inside your output/test directory.

在 Visual Studio 2012 中,vstest.console.exe(内置测试运行程序)以输出目录作为当前路径运行。这意味着您只需要在您的解决方案中包含具有“始终复制”或“如果更新则复制”属性的项目,以便您的测试使用它们。对于一般情况,您不需要 DeploymentItem 属性。从 output/test 目录中的命令行运行 vstest.console.exe 时同样适用。

There are some cases where a separate folder is used, one of them being when you are using the DeploymentItem attribute. See herefor more information.

在某些情况下会使用单独的文件夹,其中之一是当您使用 DeploymentItem 属性时。请参阅此处了解更多信息。