2012-06-13 100 views
8

我是新的單元測試在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

看起來像這樣柱的重複所有文件:[在單元測試樣品數據] [1] [1]:HTTP: // @ tackoverflow.com/questions/5383987/how-can-i-include-sample-data-files-in-vs-unit-tests – Clueless

+0

@無色:確實。我沒有找到這個帖子。謝謝。 – Hyralex

+0

也相關 - https://stackoverflow.com/questions/5581628/c-sharp-test-resources – sashoalm

回答

6

您需要在測試類上放置一個DeploymentItemAttribute

例如包括在數據文件夾

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

非常簡單。有用。謝謝。 – Hyralex

+0

DeploymentItem屬性僅適用於將項目上的生成操作設置爲「內容」並且「複製到輸出目錄」設置爲「複製,如果更新或始終複製」。 http://stackoverflow.com/a/9107244/74585 –

+0

我還必須在解決方案項目 - > Local.testsettings中啓用部署,並添加我想要部署的目錄。唷! –

相關問題