![](/img/trans.png)
[英]Why does MSTest DeploymentItem fail when running all tests in solution?
[英]DeploymentItem fails copying directories when there are multiple test projects per solution
我有许多测试类和方法可以复制特定目录,如下所示:
[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
。
一切正常,只要将一个测试程序集加载到解决方案中,或者该目录是我运行测试的唯一程序集(即仅在当前上下文/类中运行测试),就可以将目录复制到测试结果Out
目录。
一旦添加第二个程序集并运行该解决方案中的所有测试,该目录就不再被复制, 而其他仅仅是单个文件的DeploymentItems似乎可以被复制。
测试本身仍然可以运行,但是依赖于该目录的测试会崩溃。 大概是因为MSTest找不到目录-也许期望它位于其他测试程序集之一的生成目录中?
有什么想法可以防止多个测试项目阻止复制,以及我可以做些什么来解决这个问题,而无需将该目录中的每个文件都添加为单独的DeploymentItem?
这个问题已经很老了,但仍然可以使其他人受益。 特别是因为我到这里结束了:)
看来DeploymentItemAttribute
不支持在多个测试类中使用相同的源路径名。 注意:我说的是相同的路径名,而不是物理文件夹(请考虑使用相同的文件夹名部署不同的测试项目)。
但是,目标文件夹名称可以不同,不会产生不良影响。
我的建议是:
[TestClass, DeploymentItem("LanguageData", "LanguageData")]
OcrTests
和OcrBuilderTests
类以继承新类。 我已经尝试过了,并取得了巨大的成功。 就我而言,我有一个通用的测试夹具项目和多个测试项目,每个项目都使用基类。
不幸的是,DeploymentItemAttribute充满了Gotchas,更多信息请参见此处 。
尝试了您的方法,但仍然无法正确复制文件夹,因此我所做的是复制文件而不是目录(可能对某人有所帮助):
[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.