2012-04-18 169 views
42

嗨,同時運行我的單元測試時,我希望得到我的目錄項目正在運行中檢索文件獲取目錄。如何運行單元測試

說我有一個名爲MyProject的一個測試項目。測試我運行:

AppDomain.CurrentDomain.SetupInformation.ApplicationBase 

和我收到"C:\\Source\\MyProject.Test\\bin\\Debug"

這是接近我後。我不想要bin\\Debug部分。

有誰知道我不是能拿"C:\\Source\\MyProject.Test\\"

+0

所以,如果我們正確地理解你,你在你的項目中的文件和要恢復的文件,而你正在運行的應用程序/單元測試? – abhilash 2012-04-18 06:47:47

+5

另外 - 你最好以這種方式得到位置'Path.GetDirectoryName(Assembly.GetExecutingAssembly()。Location)' – abhilash 2012-04-18 06:57:53

+0

是的我想在運行單元測試時檢索文件 – AnonyMouse 2012-04-22 09:06:54

回答

50

我會採取不同的方式。

我建議把解決方案/項目的該文件的一部分。然後右鍵單擊 - >屬性 - >複製到輸出=始終複製。

該文件將被複制到任何你的輸出目錄(如C:\來源\ MyProject.Test \ BIN \調試)。

+3

不要忘記,您還需要將「構建操作」設置爲「無」。 – 2016-01-26 11:22:56

+1

在構建時如何將文件複製到輸出文件夾的問題與要求的不同。 – 2016-12-28 20:16:13

+3

@ RickO'Shea最初的問題是[XY問題](http://meta.stackexchange.com/q/66377)。提問者在他的評論中澄清了他想做的事情 - 「是的,我想在運行單元測試時檢索文件」。我只是爲他的實際問題提供瞭解決方案(而不是他嘗試的解決方案)。 – 2017-01-02 23:43:19

4

我通常做的那樣,然後我再補充​​的路徑起牀我想要的目錄。

那麼你可以做的是這樣的:

var path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"..\..\"; 
+0

不知道你如何添加「.. \ .. \「到AppDomain.CurrentDomain.SetupInformation.ApplicationBase上去目錄 – AnonyMouse 2012-04-18 06:47:34

+0

是的,對不起。我修正了格式並添加了一個例子。 – AHM 2012-04-18 06:51:44

+0

或者你可以這樣做:Path.GetFullPath(AppDomain.CurrentDomain.SetupInformation.ApplicationBase +「.. \\ .. \\ .. \\」) – 2013-11-07 16:46:29

1

我發現最好的解決辦法是把文件作爲測試項目嵌入的資源,並從我的單元測試得到它。有了這個解決方案,我不需要關心文件路徑。

24

通常你找回您的解決方案目錄(或項目目錄,這取決於您的解決方案結構)是這樣的:

string solution_dir = Path.GetDirectoryName(Path.GetDirectoryName(TestContext.TestDir)); 

這會給你通過測試的項目創造了「TestResults」文件夾中的父目錄。

+1

可愛,對我來說夠好! – demoncodemonkey 2013-11-27 15:30:01

+6

2016回答:Path.GetDirectoryName(Path.GetDirectoryName(TestContext.CurrentContext。TestDirectory)) – DavidActualX 2016-04-27 15:23:24

+5

TestDir被標記爲不贊成(2016),請考慮使用TestContext.TestRunDirectory。 – uli78 2016-09-22 07:35:21

6
/// <summary> 
/// Testing various directory sources in a Unit Test project 
/// </summary> 
/// <remarks> 
/// I want to mimic the web app's App_Data folder in a Unit Test project: 
/// A) Using Copy to Output Directory on each data file 
/// D) Without having to set Copy to Output Directory on each data file 
/// </remarks> 
[TestMethod] 
public void UT_PathsExist() 
{ 
    // Gets bin\Release or bin\Debug depending on mode 
    string baseA = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; 
    Console.WriteLine(string.Format("Dir A:{0}", baseA)); 
    Assert.IsTrue(System.IO.Directory.Exists(baseA)); 

    // Gets bin\Release or bin\Debug depending on mode 
    string baseB = AppDomain.CurrentDomain.BaseDirectory; 
    Console.WriteLine(string.Format("Dir B:{0}", baseB)); 
    Assert.IsTrue(System.IO.Directory.Exists(baseB)); 

    // Returns empty string (or exception if you use .ToString() 
    string baseC = (string)AppDomain.CurrentDomain.GetData("DataDirectory"); 
    Console.WriteLine(string.Format("Dir C:{0}", baseC)); 
    Assert.IsFalse(System.IO.Directory.Exists(baseC)); 


    // Move up two levels 
    string baseD = System.IO.Directory.GetParent(baseA).Parent.FullName; 
    Console.WriteLine(string.Format("Dir D:{0}", baseD)); 
    Assert.IsTrue(System.IO.Directory.Exists(baseD)); 


    // You need to set the Copy to Output Directory on each data file 
    var appPathA = System.IO.Path.Combine(baseA, "App_Data"); 
    Console.WriteLine(string.Format("Dir A/App_Data:{0}", appPathA)); 
    // C:/solution/UnitTestProject/bin/Debug/App_Data 
    Assert.IsTrue(System.IO.Directory.Exists(appPathA)); 

    // You can work with data files in the project directory's App_Data folder (or any other test data folder) 
    var appPathD = System.IO.Path.Combine(baseD, "App_Data"); 
    Console.WriteLine(string.Format("Dir D/App_Data:{0}", appPathD)); 
    // C:/solution/UnitTestProject/App_Data 
    Assert.IsTrue(System.IO.Directory.Exists(appPathD)); 
} 
+0

對於'Assert.IsTrue(System.IO.Directory.Exists(directory));'的想法。我調整了它,並使用'Assert.That(System.IO.Directory.Exists(directory),Is.True);'。同樣的事情,但更可讀 – RSM 2016-01-14 17:09:15

0

對於NUnit的,這是我做的:

// Get the executing directory of the tests 
string dir = NUnit.Framework.TestContext.CurrentContext.TestDirectory; 

// Infer the project directory from there...2 levels up (depending on project type - for asp.net omit the latter Parent for a single level up) 
dir = System.IO.Directory.GetParent(dir).Parent.FullName; 

如果需要,您可以從那裏導航回落到其他目錄如果需要的話:

dir = Path.Combine(dir, "MySubDir"); 
0

一般來說,你可以使用這個,不管運行測試或控制檯應用程序還是Web應用程序:

// returns the absolute path of assembly, file://C:/.../MyAssembly.dll 
var codeBase = Assembly.GetExecutingAssembly().CodeBase;  
// returns the absolute path of assembly, i.e: C:\...\MyAssembly.dll 
var location = Assembly.GetExecutingAssembly().Location; 

如果您正在運行NUnit的,則:

// return the absolute path of directory, i.e. C:\...\ 
var testDirectory = TestContext.CurrentContext.TestDirectory; 
相關問題