DeploymentItem 属性存在问题
我目前正在维护一个用 C# .NET 编写的“旧”系统,删除了一些过时的功能并进行了一些重构。 前一个人写了一些单元测试(MSTests)。 我对 JUnit 测试非常满意,但对 MSTests 还没有做太多事情。
测试方法有一个 DeploymentItem 属性,指定由具有正在测试的业务逻辑的方法解析的文本文件,以及仅指定路径的第二个 DeploymentItem包含一堆也必须部署的 TIF 文件。
[TestMethod()]
[DeploymentItem(@"files\valid\valid_entries.txt")]
[DeploymentItem(@"files\tif\")]
public void ExistsTifTest()
{
...
}
测试之前有效,但现在我必须更改 \files\tif 目录中包含的 TIF 文件的名称。 根据规则,TIF 文件名必须匹配特定模式,该模式也由 ExistsTifTest()
方法检查。 现在我必须更改文件名以使它们适应新的要求,突然间 TIF 文件不再像以前那样部署。
有人可以给我提示为什么会发生这种情况或者可能是什么原因吗? 如果我在 \files\valid\ 目录中的“valid_entries.txt”旁边添加一个新的文本文件“my2ndTest.txt”,并在测试方法上使用相应的 DeploymentItem 属性,也会发生同样的情况。 文件未部署?
我现在通过直接在 testrunconfig 中定义部署路径来部署图像,但我想了解为什么会发生这些事情,或者为什么我的新文件“my2ndTest.txt”没有部署,而其他文件却部署了。
I'm currently maintaining an "old" system written in C# .NET, removing some obsolete features and doing some refactoring. The previous guy wrote some unit tests (MSTests). I quite comfortable with JUnit tests but didn't do much yet with MSTests.
The test methods have a DeploymentItem
attribute, specifying a text file which is parsed by the method with business logic that is being tested and a 2nd DeploymentItem
where just a path has been specified containing a bunch of TIF files that have to be deployed too.
[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.
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?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
DeploymentItem
有点混乱。解决方案中的每个文件在 VS.NET 中都会有一个“复制到输出文件夹”设置。 您需要将其设置为“始终复制”(或类似的)才能将文件放入输出文件夹中。
检查您是否已为新文件设置了此设置。 如果您没有进行此设置,则文件将不会复制到输出文件夹,并且无法将它们从输出文件夹部署到 MSTest 执行操作的文件夹。
就我个人而言,如果我有单元测试所需的文件,我发现将这些文件作为资源嵌入到程序集中,并在测试期间让该程序集“解包”本身是一种更可预测的做事方式。 YMMV。
注意:这些评论基于我使用 VS2010 的经验。 对我的回答的评论表明这不是 VS2012 的问题。 我仍然坚持这样的观点,即使用嵌入式资源涉及较少的“魔力”,并且对我来说,使我的单元测试的“安排”阶段更加明确。
DeploymentItem
is a bit of a mess.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.
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.
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 中,我的 Local.testsettings 未选中“启用部署”,并且 DeploymentItem 属性不起作用。 我检查了一下,一切正常。
我希望这有帮助!
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!
我也遇到过类似的问题,但我找到了简单的三步解决方案:
假设您的文件夹结构如下所示:
<代码>
解决方案文件夹\
测试项目文件夹\
子文件夹\
[DeploymentItem(@"TestProjectFolder\SubFolder")]
将
的所有内容部署到测试运行目录[DeploymentItem(@"TestProjectFolder\SubFolder", "TargetFolder")]
将
的所有内容部署到最后一个关于 MSTest 的注意事项(至少对于 VS2010):
如果您希望
与
具有相同的名称,请使用[DeploymentItem当 MSTest 运行程序遇到愚蠢的边缘情况时, (@"SubFolder", @"SubFolder")]
将默默失败。 这就是为什么您应该在
前添加
前缀,如下所示:[DeploymentItem(@"TestProjectFolder\SubFolder", @"SubFolder “)]
I have also faced similar problems but i found easy 3 step solution for this:
Assuming your folder structure looks like this:
SolutionFolder\
TestProjectFolder\
SubFolder\
[DeploymentItem(@"TestProjectFolder\SubFolder")]
to deploy all contents of<SubFolder>
to the Test Run directory[DeploymentItem(@"TestProjectFolder\SubFolder", "TargetFolder")]
to deploy all contents of<SubFolder>
to<TargetFolder>
in the Test Run directoryOne final note about MSTest (at least for VS2010):
If you want the
<TargetFolder>
to have the same name as the<SubFolder>
, using[DeploymentItem(@"SubFolder", @"SubFolder")]
will fail silently as the MSTest runner hits a silly edge case. This is why you should prefix the<SubFolder>
with the<TestProjectFolder>
as so:[DeploymentItem(@"TestProjectFolder\SubFolder", @"SubFolder")]
希望可以帮助其他人:我尝试了这里的所有建议,但仍然我的部署项目没有被复制。
我必须做什么(按照此处的建议)是向 DeploymentItem 属性添加第二个参数:
For hopefully helping someone else out: I tried all the suggestions here and still my deployment item was not being copied.
What I had to do (as suggested here) was to add a second parameter to the DeploymentItem attribute:
如果您进入 .testrunconfig 文件并在部署下取消选中“启用部署”,测试将在正常位置运行,并且一切都会像在单元测试之外运行应用程序时一样工作。
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.
这可能与您的确切问题无关,但这里有一些我在 [DeploymentItem] 属性中发现的提示。
当与 [TestInitialize] 属性一起使用时,它不能工作
它应该在您的 [TestMethod] 上,例如
This probably doesn't relate to your exact problem, but here's a couple of tips I found with the [DeploymentItem] attribute.
It does NOT work when used with the [TestInitialize] attribute
It should be on your [TestMethod], e.g.
不要使用
DeploymentItem
。正确设置非常困难,并且它无法与我的 ReSharper 测试运行程序或 Visual Studio 2017 中 MSTEST 的本机运行程序一起使用。
相反,右键单击您的数据文件,然后选择属性。 选择复制到输出目录:始终。
现在在您的测试中,执行此操作。 该目录只是相对于测试项目的文件目录。 简单的。
这似乎确实适用于自动化构建和测试系统。
下面是文件在解决方案中的位置的图片:
Don't use
DeploymentItem
.It is very hard to set up correctly and it was not working with my ReSharper test runner nor the native one for MSTEST in Visual Studio 2017.
Instead, right click your data file, and select properties. Select Copy to output directory: Always.
Now in your test, do this. The directory is simply the directory of the file relative to the test project. Easy.
This does seem to work well with automated build and test systems.
Here is a picture of where the files go in the solution:
在尝试了此处列出的所有其他建议后,我仍然无法弄清楚发生了什么。 最后我发现在测试/测试设置菜单下没有选择设置文件,这意味着部署没有被启用。 我单击“测试/测试设置/选择测试设置文件”菜单项,选择 Local.TestSettings 文件,然后一切正常。
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.
不确定这是否完全回答了问题,但可能对一些人有帮助。
首先,我发现必须选中“启用部署”框才能使部署正常工作。
其次,文档说源路径是“相对于项目路径”,起初我认为它是指项目文件夹。 事实上,它似乎指的是构建输出文件夹。
因此,如果我有一个名为“TestFiles”的项目文件夹,其中有一个名为
Testdata.xml
的文件,则以这种方式使用该属性不起作用:我可以标记
Testdata.xml
code> 文件始终复制
,以便构建将副本放在输出文件夹下(例如,Debug\TestFiles\TestData.xml
)。 然后,部署机制将找到位于相对于构建输出的该路径 (TestFiles\Testdata.xml
) 的文件副本。或者,我可以这样设置属性:
部署机制将找到原始文件。
因此,两者都有效,但我注意到,使用
Copy Always
我偶尔会遇到在项目中编辑 app.config 文件时遇到的相同问题 - 如果我不更改代码或强制执行重建,没有任何东西会触发复制标记为在构建时复制的文件。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:I can mark the
Testdata.xml
fileCopy 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:
and the deployment mechanism will find the original file.
So either works, but I have noticed that using the
Copy Always
I 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.我首先禁用了部署标志。 但即使在我启用它之后,由于某种未知的原因,甚至目标 DLL 仍然不会被复制。 我不小心打开了“测试运行”窗口并终止了之前的所有运行,并且神奇地我在下一次运行的测试文件夹中找到了我需要的所有 DLL 和文件...非常令人困惑。
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.
我在尝试部署文件时遇到了巨大的问题 - 尝试了上面的所有建议。
然后我关闭了VS2010; 重新启动它,加载解决方案,一切正常。 (!)
我做了一些检查; 在 local.TestSetting 上设置“启用部署”标志后,您不应简单地从“测试结果”窗口重新运行测试。 您必须从 UI 中删除先前的测试运行,例如通过运行不同的测试或重新打开解决方案。
I was having huge problems trying to get files to deploy - trying all the suggestions above.
Then I closed VS2010; restarted it, loaded the solution and everything worked. (!)
I did some checking; After setting the 'Enable deployment' flag on local.TestSetting you should not simply re-run the test from the Test Results window. You have to get the previous test run removed from the UI e.g. by running a different test, or by re-opening your solution.
对于那些希望避免 DeploymentItem 混乱并采用@Martin Peck(已接受答案)建议的方法的人,您可以使用以下代码来访问嵌入资源的内容:
有关详细信息,请参阅 这个SO线程
For those who prefer to avoid the mess of DeploymentItem and take the approach suggested by @Martin Peck (accepted answer), you can use the following code to access the contents of the embedded resource:
For details, see this SO Thread
由于我总是发现 DeploymentItem 属性一团糟,因此我使用构建后脚本来部署此类文件。
- 确保您要复制的文件设置了“始终复制”属性。
- 修改您的测试项目构建后脚本,将文件从构建目标文件夹 (Bin\Debug) 复制到测试所需的位置。
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.
在 VS2010 上试试这个。 因此您不需要为每个 tif 添加 DeployItems
删除
添加测试配置。
- 右键单击解决方案资源管理器中的解决方案节点
- 添加-> 新项目...
- 选择左侧的测试设置节点,选择右侧的项目
- 单击“添加”,
调用它,例如
TDD
选择
TestMenu
下的TDD
>编辑测试设置
。单击“部署”。 启用它,然后添加所需的文件和目录。将会有一个相对于解决方案的路径。 文件将被放上。
例如,原始文件如下:
当我运行单元测试时,它会被复制到
我调用它的测试代码中:
无需选择“始终复制”; 将文件放入测试项目中; 在测试代码中添加硬编码路径。 对我来说这个解决方案效果最好。 我尝试使用 DeploymentItem,总是复制,但它不符合我的喜好。
Try this for VS2010. So you do not need to add DeployItems for every tif
Remove the
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
Choose
TDD
underTestMenu
>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:
When I run my unit test it gets copied to
in testcode I call it from:
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.
对我来说,根本原因完全是另一回事:我的测试所执行的生产代码是重命名和/或删除正在部署的 .xml 测试文件。
因此,当我单独运行测试时,它们会通过,但是当一起运行它们时,第二个和后续测试将失败,并出现“文件未找到”错误(我最初将其误诊为 DeploymentItem< /code> 属性不起作用)。
我的解决方案是让每个单独的测试方法复制已部署的文件(使用此技术),然后让正在测试的生产代码使用复制的文件而不是原始文件。
For me, the root cause was something else entirely: The production code being exercised by my tests was renaming and/or deleting the .xml test file being deployed.
Therefore, when I would run my tests individually, they'd pass, but when running them all together, the 2nd and subsequent test would fail with "file not found" errors (which I'd originally misdiagnosed as the
DeploymentItem
attribute not working).My solution was to have each individual test method make a copy of the deployed file (using this technique), and then have the production code being tested use the copied file instead of the original.
我们花了很多时间来解决部署项目问题,在本地单元测试运行和 teamcity 单元测试重新运行中也解决了这个问题。 这不简单。
调试此问题的非常好的工具是 ProcessExplorer。 使用流程资源管理器,您可以检查Visual Studio在哪里搜索部署项并对项目进行更正。 只需过滤路径包含您的部署项文件名的所有文件操作,您就会看到它。
We have spent a lot of time by Deployment items problem to solve it in local unittest run and teamcity unittest reun as well. It is not easy.
Very good tool to debug this issue is ProcessExplorer. Using process explorer, you can check where is Visual Studio searching for the deployment items and make the correction to the project. Just filter all file operation where path contains your deploymentitem filename and you will see it.
除了需要检查 Deployment 属性之外,我还发现了有关 DeploymentItem 属性的其他内容。
您的deploymentFile.txt需要与解决方案文件相关,而不是与testfile.cs相关。
Besides the Deployment attribute needing to be checked, I discovered something else about the DeploymentItem attribute.
Your deploymentFile.txt needs to be relative to the solution file and not the testfile.cs.
我一直在VS2013中从事这个工作。 我的发现是为了使其正常工作:“
根本没有 .TestSettings 文件。
我还通过惨痛的经历学到了一个技巧:不要忘记将此属性添加到每个单独的测试中。 该文件在测试运行中的第一个属性测试上进行复制,但当测试顺序更改并且非属性测试首先尝试查找该文件时,该文件仍然丢失。
I've been working on this in VS2013. My findings to get this working:
without a .TestSettings file at all.
A tip I also learned the hard way: do not forget to add this attribute to each individual test. The file copies on the first attributed test in the testrun, but remained missing when the order of the tests changed and non-attributed tests tried to find the file first.
我最大的“问题”是 DeploymentItem 处理目录的方式。 我使用的是双参数版本,两者都作为包含我想要部署的子目录的目录路径。 我最初没有意识到它只复制目录根目录中的内容,而不是整个递归文件夹结构!
我基本上有 [DeploymentItem(@"Foo\", @"Foo\")] 并期望它部署我的 Foo\Bar。 我特别必须将其更改为 [DeploymentItem(@"Foo\Bar\", @"Foo\Bar\")],现在它就像一个魅力。
My big "gotcha" was the way DeploymentItem handles directories. I was using the two-parameter version with both as directory path containing subdirectories I wanted deployed. I didn't realize initially that it only copies the stuff in the ROOT of the directory and not the entire recursive folder structure!
I basically had [DeploymentItem(@"Foo\", @"Foo\")] and was expecting it to deploy my Foo\Bar. I specifically had to change it to [DeploymentItem(@"Foo\Bar\", @"Foo\Bar\")] and now it works like a charm.
我也遇到过类似的问题。 我已经完成了上述所有步骤,但仍然没有运气。 我用的是VS2010。 然后我发现 $Menu > 测试> 选择主动测试设置 > 选择跟踪和测试影响。 在我将跟踪和测试影响更改为本地后,它开始工作。 此页面包含有关将文件复制到测试结果文件夹的非常丰富的信息,我觉得也可以添加此经验。
I have also faced similar problems. I have all the steps mentioned above but still no luck. I am using VS2010. Then i have found that $Menu > Test > Select Active Test Setting > Trace and Test impact was selected. It started working after i change Trace and test impact to Local. This page contains very resourceful information about copying files to test results folder, I feel to add this experience as well.