在单元测试输出目录中添加内容文件

Hyr*_*lex 8 c# unit-testing visual-studio

我是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");
}
Run Code Online (Sandbox Code Playgroud)

例外是:

System.IO.DirectoryNotFoundException: 'd:\***\solutiondirectory\testresults\*** 2012-06-13 17_59_53\out\content.xml'.
Run Code Online (Sandbox Code Playgroud)

如何在execute文件夹中添加此文件?

提前致谢.(对不起我的英文...)

Eri*_* J. 6

您需要在测试类上放置DeploymentItemAttribute.

例如,要包含Data文件夹中的所有文件

[TestClass()]
[DeploymentItem("Data")]
public class MyTestClass
{
    ...
}
Run Code Online (Sandbox Code Playgroud)