DeploymentItem不部署文件

我正在使用MS unit testing framework来测试我的C#库。 我必须使用DeploymentItem attribute打开我正在部署的文件。 但它没有将文件部署到Test部署目录。

在我的Unit-test项目中,我在这个文件夹中创建了一个文件夹TestFile ,有多个文件,比方说a.txt,b.txt。

现在在我的Unit-test类中,我添加了DeploymentItem属性。 但文件没有被复制。

这是我的代码。

 [TestClass] [DeploymentItem("TestFile")] public class CustomLibraryTest { public TestContext TestContext { get; set; } [TestMethod] [DeploymentItem(@"TestFiles\a.txt")] // THis should deploy a.txt to test deployment directory { var path = TestContext.TestDeploymentDir + "a.txt"; // Now when I debug this unit-test, and open path in explorer, // a.txt is not present in "TestResults\Deploy_fhafeez 2013-05-28 13_02_37\Out" folder } } 

我做错了什么?

我在这个post中找到了两个可能的解决方案:

  1. 将部署文件的“复制到输出文件夹”属性设置为“始终复制”(请参阅DeploymentItem属性的问题 )
  2. 检查Local.testsettings文件中的“启用部署”设置(请参阅DeploymentItem属性的问题 )

希望这可以帮助。

对于将来的引用,从我注意到的使用VS 2015 – 您在部署项属性中指定的路径必须相对于构建输出(调试文件夹)。 如果您的文件夹结构是“UnitTesting \ TestData \ Test.xml”,则DeploymentItem必须是DeploymentItem(“.. \ .. \ TestData \ Test.xml”)在这种情况下,TestData文件夹不需要包含在UnitTesting项目中。