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

发布于 2024-09-19 03:46:37 字数 1078 浏览 2 评论 0原文

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

[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 设置为 始终复制

一切正常,只要我只将一个测试程序集加载到解决方案中,或者这是我运行测试的唯一程序集(即仅运行测试),该目录就会复制到测试结果 Out 目录在当前上下文/类别中)。

一旦我添加第二个程序集并运行解决方案中的所有测试,那么该目录就不再被复制,但任何其他只是单个文件的 DeploymentItem 似乎都可以很好地复制。

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

您知道阻止复制的多个测试项目是什么吗?除了将该目录中的每个文件添加为单独的 DeploymentItem 之外,我可以采取哪些措施来解决它?

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.

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 (i.e. run tests only in current context/class).

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.

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?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深居我梦 2024-09-26 03:46:55

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

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

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
{
}
跨年 2024-09-26 03:46:51

这个问题很老了,但仍然可以使其他人受益。特别是自从结束在这里:)

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

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

我的建议是:

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

我已经尝试过此操作,并取得了巨大成功。
就我而言,我有一个公共测试装置项目和多个测试项目,每个项目都使用基类。

不幸的是,DeploymentItemAttribute 充满了陷阱,请参阅此处< /a> 了解更多。

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.
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")]
  3. Change your OcrTests and OcrBuilderTests classes to inherit the new class.
  4. Remember to remove the deploymentitem attributes for 'LanguageData' from OcrTests and OcrBuilderTests

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文