Ano*_*use 68 c# directory unit-testing
嗨,当我运行我的单元测试时,我想要获取我的项目正在运行的目录来检索文件.
假设我有一个名为MyProject的测试项目.测试我运行:
AppDomain.CurrentDomain.SetupInformation.ApplicationBase
Run Code Online (Sandbox Code Playgroud)
我接受"C:\\Source\\MyProject.Test\\bin\\Debug".
这接近我所追求的.我不想要这个bin\\Debug部分.
任何人都知道我怎么能得到"C:\\Source\\MyProject.Test\\"?
Ili*_*ian 65
我会做的不同.
我建议将该文件作为解决方案/项目的一部分.然后右键单击 - >属性 - >复制到输出=始终复制.
然后该文件将被复制到您的输出目录(例如C:\ Source\MyProject.Test\bin\Debug).
Dar*_*nas 36
通常,您检索解决方案目录(或项目目录,具体取决于您的解决方案结构),如下所示:
string solution_dir = Path.GetDirectoryName( Path.GetDirectoryName(
TestContext.CurrentContext.TestDirectory ) );
Run Code Online (Sandbox Code Playgroud)
这将为您提供测试项目创建的"TestResults"文件夹的父目录.
man*_*hah 25
Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName;
Run Code Online (Sandbox Code Playgroud)
这将为您提供所需的目录....
如
AppDomain.CurrentDomain.SetupInformation.ApplicationBase
Run Code Online (Sandbox Code Playgroud)
什么都没有
Directory.GetCurrentDirectory().
Run Code Online (Sandbox Code Playgroud)
已经在这个链接上了
http://msdn.microsoft.com/en-us/library/system.appdomain.currentdomain.aspx
继续@ abhilash的评论.
这适用于我的EXE,DLL 以及在调试或发布模式下从不同的UnitTest项目进行测试时:
var dirName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location.Replace("bin\\Debug", string.Empty));
Run Code Online (Sandbox Code Playgroud)
/// <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));
}
Run Code Online (Sandbox Code Playgroud)
我通常这样做,然后我只是添加"..\..\"到路径,以达到我想要的目录.
所以你能做的就是:
var path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"..\..\";
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
70201 次 |
| 最近记录: |