C# DeploymentItem 属性的问题

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

Problems with DeploymentItem attribute

c#visual-studiounit-testingmstestdeploymentitem

提问by Juri

I'm currently maintaining an "old" system written in C#.net, removing some obsolete features and doing some refactoring. Thanks god, the previous guy wrote some unit tests (MSTests). I quite comfortable with JUnit tests, but didn't do yet much with MSTests.

我目前正在维护一个用 C#.net 编写的“旧”系统,删除一些过时的功能并进行一些重构。感谢上帝,前一个人写了一些单元测试(MSTests)。我对 JUnit 测试很满意,但对 MSTests 还没有做太多。

The test methods have a DeploymentItemattribute, specifying a text file which is parsed by the business logic method that is being tested and a 2nd DeploymentItemwhere just a path has been specified containing a bunch of TIF files that have to be deployed too.

测试方法有一个DeploymentItem属性,指定一个由正在测试的业务逻辑方法解析的文本文件,以及第二个DeploymentItem,其中只指定了一个包含一堆必须部署的 TIF 文件的路径。

[TestMethod()]
[DeploymentItem(@"files\valid\valid_entries.txt")]
[DeploymentItem(@"files\tif\")]
public void ExistsTifTest()
{
   ...
}

The tests worked before, but now I had to change the names of the TIF files contained in the \files\tif directory. According to a rule, the TIF filenames have to match a certain pattern which is also checked by the ExistsTifTest()method. Now I had to change the filenames in order to adapt them to the new requirements and suddently the TIF files are no more being deployed as before.

测试以前有效,但现在我必须更改 \files\tif 目录中包含的 TIF 文件的名称。根据规则,TIF 文件名必须匹配某个模式,该模式也由该ExistsTifTest()方法检查。现在我不得不更改文件名以使它们适应新的要求,突然间 TIF 文件不再像以前一样部署。

Can someone give me a hint why this happens or what may be the cause? The same thing happens also if I add a new text-file say "my2ndTest.txt" beside the "valid_entries.txt" in the \files\valid\ directory with the according DeploymentItem attribute on the test method. The file doesn't get deployed?

有人可以给我一个提示为什么会发生这种情况或可能是什么原因?如果我在 \files\valid\ 目录中的“valid_entries.txt”旁边添加一个新的文本文件“my2ndTest.txt”,并在测试方法上添加相应的 DeploymentItem 属性,也会发生同样的事情。文件没有部署?

I got the images now deployed by defining the deployment path directly in the testrunconfig, but I'd like to understand why these things happen or why for instance my new file "my2ndTest.txt" doesn't get deployed while the others do.

我现在通过直接在 testrunco​​nfig 中定义部署路径来部署图像,但我想了解为什么会发生这些事情,或者为什么我的新文件“my2ndTest.txt”没有被部署,而其他人则这样做。

采纳答案by Martin Peck

DeploymentItemis a bit of a mess.

DeploymentItem有点乱。

Each file in your solution will have a "Copy To Output Folder" setting in VS.NET. You need this to be "Copy Always" (or similar) in order to get the files into the output folder.

解决方案中的每个文件在 VS.NET 中都有一个“复制到输出文件夹”设置。您需要将其设为“始终复制”(或类似),以便将文件放入输出文件夹。

Check that you've got this set for the new files. If you don't have this set then the files won't get copied to the output folder, and then they can't be deployed from the output folder to the folder where MSTest does it stuff.

检查您是否为新文件设置了此设置。如果您没有此设置,则文件将不会被复制到输出文件夹,然后它们将无法从输出文件夹部署到 MSTest 执行此操作的文件夹。

Personally, if I have files that I need for my unit tests I've found that embedding those files as resources into an assembly, and having that assembly "unpack" itself during the tests is a more predictable way of doing things. YMMV.

就我个人而言,如果我有单元测试所需的文件,我发现将这些文件作为资源嵌入到程序集中,并在测试期间让程序集“解包”是一种更可预测的做事方式。天啊。

note:These comments are based upon my experience with VS2010. Comments to my answer would suggest that this is not problem with VS2012. I still stand by comments that using embedded resources involves less "magic" and, for me, makes the "arrange" stage of my unit tests much more explicit.

注意:这些评论是基于我对 VS2010 的经验。对我的回答的评论表明这不是 VS2012 的问题。我仍然支持使用嵌入式资源涉及更少“魔法”的评论,对我来说,使我的单元测试的“安排”阶段更加明确。

回答by Josh Close

If you go into your .testrunconfig file and under deployment uncheck "Enable Deployment", the tests will run in their normal location, and everything will work like it does when running the app outside a unit test.

如果您进入 .testrunco​​nfig 文件并在部署下取消选中“启用部署”,则测试将在其正常位置运行,并且一切都将像在单元测试之外运行应用程序时一样工作。

回答by Ivan Muzzolini

In VS2010, my Local.testsettings had the "Enable Deployment" unchecked and the DeploymentItem attribute was not working. I checked it and everything worked fine. I hope this helps!

在 VS2010 中,我的 Local.testsettings 未选中“启用部署”并且 DeploymentItem 属性不起作用。我检查了一下,一切正常。我希望这有帮助!

回答by Ismail Hawayel

Since I always found the DeploymentItem attribute a mess, I do the deployment of such files by using the post-build script. - Make sure the files you wanna copy has the Copy Always property set. - Modify your test project post-build script to copy the files from build target folder(Bin\Debug) to the location where your test is expecting them.

由于我总是发现 DeploymentItem 属性一团糟,因此我使用构建后脚本来部署此类文件。- 确保您要复制的文件设置了始终复制属性。- 修改您的测试项目构建后脚本,将文件从构建目标文件夹 (Bin\Debug) 复制到您的测试期望它们的位置。

回答by Matt Frear

This probably doesn't relate to your exact problem, but here's a couple of tips I found with the [DeploymentItem] attribute.

这可能与您的确切问题无关,但这里有一些我在 [DeploymentItem] 属性中发现的提示。

  1. Copy to output directory should be set to Copy Always.
  1. 复制到输出目录应设置为始终复制。

It does NOTwork when used with the [TestInitialize] attribute

与[TestInitialize]属性使用时工作

[TestInitialize]
[DeploymentItem("test.xlsx")]
public void Setup()
{

It should be on your [TestMethod], e.g.

它应该在您的 [TestMethod] 上,例如

    [TestInitialize]
    public void Setup()
    {
        string spreadsheet = Path.GetFullPath("test.xlsx");
        Assert.IsTrue(File.Exists(spreadsheet));
        ...
    }

    [TestMethod]
    [DeploymentItem("test.xlsx")]
    public void ExcelQuestionParser_Reads_XmlElements()
    {
        ...
    }

回答by Schultz9999

I had Deployment flag disabled first. But even after I enabled it, for some unknown reason nothing even target DLLs would still be copied. Accidentally I opened Test Run window and killed all the previous runs and magically I found all the DLLs and files I needed in the test folder the very next run... Very confusing.

我首先禁用了部署标志。但即使在我启用它之后,由于某些未知原因,甚至目标 DLL 仍然不会被复制。我不小心打开了“测试运行”窗口并终止了所有以前的运行,并且神奇地在下一次运行时在测试文件夹中找到了我需要的所有 DLL 和文件......非常混乱。

回答by Patrik Lindstr?m

Try this for VS2010. So you do not need to add DeployItems for every tif
Remove the

在 VS2010 上试试这个。所以你不需要为每个 tif 添加 DeployItems
删除

[DeploymentItem(@"files\valid\valid_entries.txt")]  
[DeploymentItem(@"files\tif\")]  

Add a test configuration.
- right-click on solution node in solution explorer
- Add -> New Item...
- Select Test Settings node on the left, select the item on the right
- Click Add

添加测试配置。
- 右键单击​​解决方案资源管理器中的解决方案节点
- 添加 -> 新建项目...
- 选择左侧的测试设置节点,选择右侧的项目
- 单击添加

Call it eg TDD

称之为例如 TDD

Choose TDDunder TestMenu> Edit Testsettings.

TDDTestMenu>下选择Edit Testsettings

Click on the Deployment. Enable it and then Add the files and directories that you want.There will be a path relative to the solution. The files will be put on. The original file are for example here:

单击部署。启用它然后添加你想要的文件和目录。会有一个相对于解决方案的路径。文件将被放置。原始文件例如在这里:

D:\Users\Patrik\Documents\Visual Studio 2010\Projects\DCArrDate\WebMVCDCArrDate\Trunk\WebMVCDCArrDate\Authority.xml  

When I run my unit test it gets copied to

当我运行我的单元测试时,它被复制到

D:\Users\Patrik\Documents\Visual Studio 2010\Projects\DCArrDate\WebMVCDCArrDate\Trunk\WebMVCDCArrDate.Tests\bin\Debug\TestResults\Patrik_HERKULES 2011-12-17 18_03_27\Authority.xml  

in testcode I call it from:

在测试代​​码中,我从以下位置调用它:

[TestMethod()]
public void Read_AuthorityFiles_And_ParseXML_To_Make_Dictonary()  
{  
  string authorityFile = "Authority.xml";  
  var Xmldoc = XDocument.Load(authorityFile);  

There is no need to choose Copy Always; put the files in the testproject; add hardcoded paths in the testcode. For me this solution worked best. I tried with DeploymentItem, copy always but it was not to my liking.

无需选择始终复制;将文件放在 testproject 中;在测试代​​码中添加硬编码路径。对我来说,这个解决方案效果最好。我尝试使用 DeploymentItem,总是复制,但不是我喜欢的。

回答by Peter K.

For hopefully helping someone else out: I tried all the suggestions here and stillmy deployment item was not being copied.

希望能帮助其他人:我在这里尝试了所有建议,但仍然没有复制我的部署项目。

What I had to do (as suggested here) was to add a second parameter to the DeploymentItem attribute:

我必须做的(如此处建议的)是向 DeploymentItem 属性添加第二个参数:

[DeploymentItem(@"UnitTestData\TestData.xml", "UnitTestData")]

回答by user1546704

Not sure if this exactly answers the question, but it may help some. First, I've found the "Enable Deployment" box must be checked for deployment to work. Second, the doc says the source path is "relative to the project path" which at first I took to mean the project folder. In fact, it seems to refer to the build output folder. So if I have a project folder called 'TestFiles' and a file in it called Testdata.xml, using the attribute this way doesn't work:

不确定这是否完全回答了问题,但它可能对某些人有所帮助。首先,我发现必须选中“启用部署”框才能使部署工作。其次,文档说源路径是“相对于项目路径”,起初我指的是项目文件夹。实际上,它似乎是指构建输出文件夹。因此,如果我有一个名为 'TestFiles' 的项目文件夹和一个名为 的文件,则以Testdata.xml这种方式使用该属性是行不通的:

[DeploymentItem(@"TestFiles\Testdata.xml")] 

I can mark the Testdata.xmlfile Copy Always, so that the build puts a copy under the output folder (e.g., Debug\TestFiles\TestData.xml). The deployment mechanism will then find the copy of the file located at that path (TestFiles\Testdata.xml) relative to the build output. Or, I can set the attribute this way:

我可以标记Testdata.xml文件Copy Always,以便构建在输出文件夹下放置一个副本(例如,Debug\TestFiles\TestData.xml)。然后,部署机制将找到位于TestFiles\Testdata.xml相对于构建输出的该路径 ( )处的文件副本。或者,我可以这样设置属性:

[DeploymentItem(@"..\..\TestFiles\Testdata.xml")] 

and the deployment mechanism will find the original file. So either works, but I have noticed that using the Copy AlwaysI occasionally run into the same problem I have when editing the app.config file in a project - if I don't change code or force a rebuild, nothing triggers copying of files marked to be copied on build.

并且部署机制会找到原始文件。所以两者都有效,但我注意到使用Copy Always我偶尔会遇到在项目中编辑 app.config 文件时遇到的相同问题 - 如果我不更改代码或强制重建,则不会触发标记为的文件的复制在构建时被复制。

回答by Mike

After trying all of the other suggestions listed here I still couldn't figure out what was going on. Finally I discovered that there was no settings file selected under Test/Test Settings menu, which meant that Deployment wasn't being enabled. I clicked the Test/Test Settings/Select Test Settings File menu item, selected the Local.TestSettings file, then everything worked.

在尝试了此处列出的所有其他建议后,我仍然无法弄清楚发生了什么。最后我发现在测试/测试设置菜单下没有选择设置文件,这意味着没有启用部署。我单击了测试/测试设置/选择测试设置文件菜单项,选择了 Local.TestSettings 文件,然后一切正常。