12

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

在我的单元测试项目中,我在这个文件夹中创建了一个文件夹TestFile有多个文件,可以说a.txt,b.txt。

现在在我的单元测试类中,我添加了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 
    } 
} 

我在做什么错了?

回答

14

我在this thread发现了两个可能的解决方案:

  1. 设置您的部署文件的“复制到输出文件夹”属性设置为“一直拷贝”(见Problems with DeploymentItem attribute
  2. 选中“启用部署”设置在Local.testsettings文件中(参见Problems with DeploymentItem attribute

希望这会有所帮助。

+0

感谢您的帮助。我设置了“始终复制”。我已经完成了使用测试设置的部署,这次我正在考虑使用DeplyomentAttribute。 –

4

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