DeploymentItem不部署文件

时间:2013-05-28 08:10:22

标签: c# unit-testing visual-studio-2012 vs-unit-testing-framework deploymentitem

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

在我的单元测试项目中,我在这个文件夹中创建了一个文件夹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
    }
}

我做错了什么?

3 个答案:

答案 0 :(得分:18)

我在this thread找到了两种可能的解决方案:

  1. 将部署文件的“复制到输出文件夹”属性设置为“始终复制”(请参阅​​Problems with DeploymentItem attribute
  2. 检查Local.testsettings文件中的“启用部署”设置(请参阅Problems with DeploymentItem attribute
  3. 希望这有帮助。

答案 1 :(得分:7)

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

答案 2 :(得分:0)

我有一组测试,其中该副本适用于某些测试,但不适用于最新的测试,即使DeploymentItem属性的设置方式完全相同。在用尽所有其他内容之后,我对该解决方案进行了“清理解决方案”并重新运行,它开始正确复制。 YMMV

相关问题