简体   繁体   English

每个解决方案有多个测试项目时,DeploymentItem无法复制目录

[英]DeploymentItem fails copying directories when there are multiple test projects per solution

I have a number of test classes and methods that copy a particular directory like so: 我有许多测试类和方法可以复制特定目录,如下所示:

[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
}

Those tests are in a single assembly and all the files in the LangaugeData directory have their Copy to Output Directory set to Copy Always . 这些测试在单个程序集中,并且LangaugeData目录中的所有文件的“ Copy to Output Directory设置为Copy Always

It all works fine and the directory is copied to the test results Out directory as long as I only have that one test assembly loaded into the solution or that's the only assembly I run tests from (ie run tests only in current context/class). 一切正常,只要将一个测试程序集加载到解决方案中,或者该目录是我运行测试的唯一程序集(即仅在当前上下文/类中运行测试),就可以将目录复制到测试结果Out目录。

As soon as I add a second assembly and run all the tests in the solution then that directory no longer gets copied, but any other DeploymentItems that are just individual files seem to get copied fine. 一旦添加第二个程序集并运行该解决方案中的所有测试,该目录就不再被复制, 其他仅仅是单个文件的DeploymentItems似乎可以被复制。

The tests themselves all still run, but the ones that depend on that directory crash. 测试本身仍然可以运行,但是依赖于该目录的测试会崩溃。 Presumably that's because MSTest can't find the directory - perhaps it's expecting it to be in the build directory of one of the other test assemblies? 大概是因为MSTest找不到目录-也许期望它位于其他测试程序集之一的生成目录中?

Any ideas what it is about multiple test projects that's preventing the copy, and what I can do to get around it, short of adding every single file in that directory as an individual DeploymentItem? 有什么想法可以防止多个测试项目阻止复制,以及我可以做些什么来解决这个问题,而无需将该目录中的每个文件都添加为单独的DeploymentItem?

This question is quite old, but could still benefit others. 这个问题已经很老了,但仍然可以使其他人受益。 Especially since I ended up here :) 特别是因为到这里结束了:)

It seems that DeploymentItemAttribute doesn't support using the same source path name in multiple test classes. 看来DeploymentItemAttribute不支持在多个测试类中使用相同的源路径名。 Note: I said same path name, not physical folder (think different test projects with same folder name to deploy). 注意:我说的是相同的路径名,而不是物理文件夹(请考虑使用相同的文件夹名部署不同的测试项目)。

However the destination folder name can be different, with no ill effects. 但是,目标文件夹名称可以不同,不会产生不良影响。

My suggestion is: 我的建议是:

  1. Create a fixture base class (if you prefer, in separate project) 创建一个夹具基类(如果您愿意,在单独的项目中)
  2. Add the attribute: [TestClass, DeploymentItem("LanguageData", "LanguageData")] 添加属性: [TestClass, DeploymentItem("LanguageData", "LanguageData")]
  3. Change your OcrTests and OcrBuilderTests classes to inherit the new class. 更改您的OcrTestsOcrBuilderTests类以继承新类。
  4. Remember to remove the deploymentitem attributes for 'LanguageData' from OcrTests and OcrBuilderTests 请记住从OcrTests和OcrBuilderTests中删除“ LanguageData”的Deploymentitem属性

I've tried this, with Great Success. 我已经尝试过了,并取得了巨大的成功。 In my case, I had a common test fixture project and multiple test projects, each using the base class. 就我而言,我有一个通用的测试夹具项目和多个测试项目,每个项目都使用基类。

Unfortunately the DeploymentItemAttribute is filled with Gotchas, see here for more. 不幸的是,DeploymentItemAttribute充满了Gotchas,更多信息请参见此处

Tried your approach but still it did not copy folder properly, so what i did instead copied files not directories(maybe this helps someone): 尝试了您的方法,但仍然无法正确复制文件夹,因此我所做的是复制文件而不是目录(可能对某人有所帮助):

[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
{
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM