簡體   English   中英

在單元測試輸出目錄中添加內容文件

[英]Add content file in unit testing output directory

我是 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'.

如何在執行文件夾中添加此文件?

提前致謝。 (對不起我的英語......)

您需要在測試類上放置DeploymentItemAttribute

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

[TestClass()]
[DeploymentItem("Data")]
public class MyTestClass
{
    ...
}

正如 Matthew Lock 所提到的,我唯一需要做的就是在解決方案項中的 Local.testsettings 中的 Deployment 下添加文件。

測試設置

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM