【问题标题】:DeploymentItem fails copying directories when there are multiple test projects per solution当每个解决方案有多个测试项目时,DeploymentItem 无法复制目录
【发布时间】:2010-09-15 12:59:19
【问题描述】:

我有许多复制特定目录的测试类和方法,如下所示:

[TestClass, DeploymentItem("LanguageData", "LanguageData")]
public class OcrTests
{
    [TestMethod]
    public void Can_Capture_Field()
    {
        // some code that expects the LanguageData directory to be in the test results Out directory
    }

    // etc
}

[TestClass]
public class OcrBuilderTests
{
    [TestMethod, DeploymentItem("LanguageData", "LanguageData")]
    public void Can_Build_Specific_Ocr_Engine_Implementation()
    {
        // some more code that expects the LanguageData directory to be in the test results Out directory
    }

    // etc
}

这些测试位于单个程序集中,并且 LangaugeData 目录中的所有文件都将其 Copy to Output Directory 设置为 Copy Always。

一切正常,只要我只有一个测试程序集加载到解决方案中,或者这是我运行测试的唯一程序集(即仅在当前运行测试上下文/类)。

只要我添加第二个程序集并运行解决方案中的所有测试,则该目录将不再被复制,但任何其他只是单个文件的 DeploymentItems 似乎都可以正常复制。

测试本身仍在运行,但依赖于该目录的测试会崩溃。大概是因为 MSTest 找不到该目录 - 也许它期望它位于其他测试程序集之一的构建目录中?

对于阻止复制的多个测试项目有什么想法,以及除了将该目录中的每个文件都添加为单独的 DeploymentItem 之外,我可以做些什么来解决它?

【问题讨论】:

    标签: visual-studio-2010 .net-4.0 mstest deploymentitem


    【解决方案1】:

    这个问题已经很老了,但仍然可以使其他人受益。尤其是因为 我 来到了这里 :)

    DeploymentItemAttribute 似乎不支持在多个测试类中使用相同的源路径名。 注意:我说的是相同的路径名,而不是物理文件夹(认为不同的测试项目具有相同的文件夹名来部署)。

    但是目标文件夹名称可以不同,不会产生不良影响。

    我的建议是:

    1. 创建一个夹具基类(如果您愿意,可以在单独的项目中)
    2. 添加属性:[TestClass, DeploymentItem("LanguageData", "LanguageData")]
    3. 更改您的 OcrTests 和 OcrBuilderTests 类以继承新类。
    4. 记得从 OcrTests 和 OcrBuilderTests 中删除“LanguageData”的部署项属性

    我已经尝试过了,非常成功。 就我而言,我有一个通用的测试夹具项目和多个测试项目,每个项目都使用基类。

    不幸的是,DeploymentItemAttribute 充满了陷阱,请参阅here 了解更多信息。

    【讨论】:

      【解决方案2】:

      尝试了你的方法,但它仍然没有正确复制文件夹,所以我所做的是复制文件而不是目录(也许这对某人有帮助):

      [TestClass]
      [DeploymentItem("connectionStrings.config")]
      
      // should be able to do this, but it does not work always, only sometimes
      //[DeploymentItem("Configs", "Configs")]
      
      // this instead should work always
      [DeploymentItem("Configs\\file1.txt", "Configs")]
      [DeploymentItem("Configs\\file2.txt", "Configs")]
      [DeploymentItem("Configs\\file3.txt", "Configs")]
      .....
      [DeploymentItem("Configs\\filen.txt", "Configs")]
      public class BaseTests
      {
      }
      

      【讨论】:

      • 根据我的经验,这实际上并没有添加其他文件。它似乎只是添加了第一个。
      猜你喜欢
      • 1970-01-01
      • 2011-07-09
      • 1970-01-01
      • 1970-01-01
      • 2016-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多