C# 如何在运行单元测试时获取目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10204091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get Directory while running unit test
提问by AnonyMouse
Hi when running my unit test I'm wanting to get the directory my project is running in to retrieve a file.
嗨,在运行我的单元测试时,我想获取我的项目正在运行的目录以检索文件。
Say I have a Test project named MyProject. Test I run:
假设我有一个名为 MyProject 的测试项目。我运行的测试:
AppDomain.CurrentDomain.SetupInformation.ApplicationBase
and I receive "C:\\Source\\MyProject.Test\\bin\\Debug".
我收到了"C:\\Source\\MyProject.Test\\bin\\Debug"。
This is close to what I'm after. I don't want the bin\\Debugpart.
这接近我所追求的。我不要那bin\\Debug部分。
Anyone know how instead I could get "C:\\Source\\MyProject.Test\\"?
有谁知道我怎么能得到"C:\\Source\\MyProject.Test\\"?
采纳答案by Ilian Pinzon
I would do it differently.
我会以不同的方式去做。
I suggest making that file part of the solution/project. Then right-click -> Properties -> Copy To Output = Copy Always.
我建议将该文件作为解决方案/项目的一部分。然后右键单击 -> 属性 -> 复制到输出 = 始终复制。
That file will then be copied to whatever your output directory is (e.g. C:\Source\MyProject.Test\bin\Debug).
然后该文件将被复制到您的任何输出目录(例如 C:\Source\MyProject.Test\bin\Debug)。
Edit: Copy To Output = Copy if Newer is the better option
编辑:复制到输出 = 如果较新是更好的选择则复制
回答by David Z.
I'm not sure if this helps, but this looks to be briefly touched on in the following question.
我不确定这是否有帮助,但在以下问题中似乎会简要介绍这一点。
回答by AHM
I normally do it like that, and then I just add "..\..\"to the path to get up to the directory I want.
我通常是这样做的,然后我只需添加"..\..\"到路径即可到达我想要的目录。
So what you could do is this:
所以你可以做的是:
var path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"..\..\";
回答by manu_dilip_shah
Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName;
This will give you the directory you need....
这将为您提供所需的目录....
as
作为
AppDomain.CurrentDomain.SetupInformation.ApplicationBase
gives nothing but
什么都不给
Directory.GetCurrentDirectory().
Have alook at this link
看看这个链接
http://msdn.microsoft.com/en-us/library/system.appdomain.currentdomain.aspx
http://msdn.microsoft.com/en-us/library/system.appdomain.currentdomain.aspx
回答by Andre L.A.C Bittencourt
The best solution I found was to put the file as an embedded resource on the test project and get it from my unit test. With this solution I don′t need to care about file paths.
我找到的最佳解决方案是将文件作为嵌入资源放在测试项目中并从我的单元测试中获取。有了这个解决方案,我不需要关心文件路径。
回答by Darien Pardinas
Usually you retrieve your solution directory (or project directory, depending on your solution structure) like this:
通常你会像这样检索你的解决方案目录(或项目目录,取决于你的解决方案结构):
string solution_dir = Path.GetDirectoryName( Path.GetDirectoryName(
TestContext.CurrentContext.TestDirectory ) );
This will give you the parent directory of the "TestResults" folder created by testing projects.
这将为您提供由测试项目创建的“TestResults”文件夹的父目录。
回答by user1207577
/// <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));
}
回答by noelicus
For NUnit this is what I do:
对于 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;
If required you can from there navigate back down to other directories if required:
如果需要,您可以从那里导航回其他目录(如果需要):
dir = Path.Combine(dir, "MySubDir");
回答by houss
In general you may use this, regardless if running a test or console app or web app:
通常,您可以使用它,无论是运行测试或控制台应用程序还是 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;
If you are running NUnit, then:
如果您正在运行 NUnit,则:
// return the absolute path of directory, i.e. C:\...\
var testDirectory = TestContext.CurrentContext.TestDirectory;
回答by Ogglas
You can do it like this:
你可以这样做:
using System.IO;
Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, @"..\..\"));

