2009-11-30 160 views

回答

17

MSDN網頁...

相對路徑是相對於在 .testrunco​​nfig文件中找到的 RelativePathRoot設置。

該設置默認爲解決方案目錄。所以,如果你有這樣的項目結構

SecretProject\ 
    ComponentA\ 
    ComponentA.Test\ 
     Resources\ 
      required.xml 
     ComponentA.Test.csproj 
     Tests.cs 
    SecretProject.sln 

而且要部署required.xml,你要創建一個DeploymentItemAttribute這樣

[TestClass] 
public class Tests 
{ 
    [TestMethod] 
    [DeploymentItem("ComponentA.Test\Resources\required.xml")] 
    public void Test() 
    { 

    } 
} 

看來屬性需要的文件被設置爲'內容'和'複製總是'或'如果更新複製'。在this MSDN page上有高級示例。

+3

「看來文件屬性需要被設置爲‘內容’和‘始終複製’或‘複製,如果新的’。」你會感到驚訝有多難找到這個... – SouthShoreAK

+0

@SouthShoreAK - 它被發現在哪裏? –

+0

@ScottLangham它位於部署項目的File屬性中。在'解決方案資源管理器'中,您需要突出顯示該文件(在本例中爲'required.xml'),右鍵單擊它並選擇屬性。你應該在那裏看到它。 – dpimente

1

假設RelativePathRoot默認值是您的解決方案所在的目錄不正確,我的.testrunco​​nfig文件中也沒有定義RelativePathRoot。我發現RelativePathRoot默認爲解決方案的/ bin/debug目錄。

從這一點開始走回頭路,然後走到我正在嘗試部署用於單元測試的文件時工作得很好。

0

所以我要添加我的經驗。

所以,如果你使用.testrunco​​nfig文件,這將勝過(覆蓋)我在下面說的。

我覺得有幾個選擇。

相對於目前的.csproj

相對於的.sln

相對於當前.cs文件

我終於得到了我的構建系統通過使用「工作相對於當前的.cs文件「的方法。

在我的示例中,我需要爲我的UnitTest複製另一個項目的Xsd。但是文件的類型並不重要。

實施例:

C:\ MyFolder文件\ MyXsdProject \的XSD \ MyCoolXsd.xsd

C:\ MyFolder文件\ MyCsharpUnitTestProject \ MySubFolder1 \ MySubFolder2 \ MyUnitTestClass。CS

namespace MyCsharpUnitTestProject.MySubFolder1.MySubFolder2 
{ 

    [TestClass] 
    [DeploymentItem(@"..\..\..\MyXsdProject\XSDs\MyCoolXsd.xsd")] 

    public class MyUnitTestClass 
    { 
    } 
}