我是visual studio中的单元测试的新手,我想加载一个物理xml文件。 此文件作为内容在单元测试项目中,并将其复制到输出目录中。
因此,当我编译项目时,xml文件位于输出目录中。 但是当我执行测试时,会创建一个包含所有相关DLL的新目录,但不会复制xml文件。
执行测试需要Xml的内容。我执行此代码以检索执行文件夹中的Xml文件的路径:
private static string GetXmlFullName()
{
// GetApplicationPath use the current DLL to find the physical path
// of the current application
string executeDirectory = AssemblyHelper.GetApplicationPath();
return Path.Combine(executeDirectory, "content.xml");
}
例外是:
System.IO.DirectoryNotFoundException: 'd:\***\solutiondirectory\testresults\*** 2012-06-13 17_59_53\out\content.xml'.
如何在执行文件夹中添加此文件?
提前致谢。 (对不起我的英文......)
答案 0 :(得分:6)
您需要在测试类上放置DeploymentItemAttribute。
例如,要包含Data文件夹中的所有文件
[TestClass()]
[DeploymentItem("Data")]
public class MyTestClass
{
...
}