visual-studio C# DeploymentItem 无法为 MSTest 单元测试复制文件

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

C# DeploymentItem fails to copy file for MSTest unit test

c#visual-studiomstestdeploymentitem

提问by Sarah Vessels

I'm having trouble getting an XSL file to be copied to the same directory as the test assembly when I use the DeploymentItemattribute on an MSTest unit test. I followed the chosen answer for this questionand the file I need copied has its "Copy to Output Directory" set to "Copy Always". When I check my ProjectDir\bin directory (the Target directory), the file I want copied is indeed there, alongside the DLLs and PDBs.

当我DeploymentItem在 MSTest 单元测试中使用该属性时,无法将 XSL 文件复制到与测试程序集相同的目录中。我遵循了这个问题的选择答案,我需要复制的文件将其“复制到输出目录”设置为“始终复制”。当我检查我的 ProjectDir\bin 目录(目标目录)时,我想要复制的文件确实在那里,与 DLL 和 PDB 一起。

I have a couple unit tests with the following setup:

我有几个具有以下设置的单元测试:

private const string DLL = "Service.dll";
private const string XSL_PATH = "transform.xsl";

[TestInitialize]
public void InitializeTest()
{
    Assert.IsTrue(File.Exists(DLL)); // passes
}

[TestMethod]
[DeploymentItem(DLL)]
[DeploymentItem(XSL_PATH)]
public void XmlToResultsTest()
{
    Assert.IsTrue(File.Exists(XSL_PATH)); // fails
}

The XSL test fails because when I check MSTest's TestResults\particularTestRun\Out directory, I see the DLLs and the PDBs, but my XSL file is not there. What I want to know is whythe XSL file does not get copied alongside the DLLs and PDBs even when I explicitly tell Visual Studio to copy it there via DeploymentItem?

XSL 测试失败,因为当我检查 MSTest 的 TestResults\particularTestRun\Out 目录时,我看到了 DLL 和 PDB,但我的 XSL 文件不在那里。我想知道的是,为什么在XSL文件不被复制的DLL和PDBS旁边,甚至当我明确地告诉Visual Studio中通过复制它那里DeploymentItem

采纳答案by Sarah Vessels

Thanks to Marc Gravell's answerto a related question of mine, I tried updating my MSTest .testrunconfig file so that my XSL file is included in the 'Deployment' section. This lets my unit tests pass, but I'm still perturbed that I had to do this--shouldn't the combination of DeploymentItemand marking the file's properties in my project to copy to the output directory be sufficient?

感谢Marc Gravell对我的一个相关问题的回答,我尝试更新我的 MSTest .testrunco​​nfig 文件,以便我的 XSL 文件包含在“部署”部分中。这让我的单元测试通过了,但我仍然感到不安,我必须这样做——DeploymentItem在我的项目中组合和标记文件的属性来复制到输出目录是否就足够了?

回答by andrewb

Allegedly VS2008 deployment items silently fail unless the output directory is a literal string. Hmmm ^_^

据称,除非输出目录是文字字符串,否则 VS2008 部署项目会静默失败。嗯嗯^_^

回答by mgutzwiller

I had the same problem even though I wasusing a literal string in the deployment item. I even tried adding the file to the 'Deployment' section of the test settings which didn't work either. It turned out that the problem was related to the testing platform.

我有同样的问题,即使我使用部署项目文本字符串。我什至尝试将文件添加到测试设置的“部署”部分,但这也不起作用。原来问题与测试平台有关。

I have a 64 bit machine and both the project I was testing and the unit test project's platform target were 'Any CPU'.

我有一台 64 位机器,我正在测试的项目和单元测试项目的平台目标都是“任何 CPU”。

I found that the deployment item was only copied if I selected 'Run tests in a 64 bit process on 64 bit machine".

我发现只有当我选择“在 64 位机器上的 64 位进程中运行测试”时才会复制部署项。

回答by Louis

We had a similar situation at work where the DeploymentItem attribute was not working as expected. We were also using 64 bit machines, with project platform set at "Any CPU". As a workaround, we included the file required by the unit test as a link from the unit test project and set its "Copy to Output Directory" to "Copy Always".

我们在工作中遇到过类似的情况,即 DeploymentItem 属性没有按预期工作。我们还使用了 64 位机器,项目平台设置为“任何 CPU”。作为一种解决方法,我们将单元测试所需的文件包含为单元测试项目的链接,并将其“复制到输出目录”设置为“始终复制”。