ulf-jzl
asked on
Get project folder path from a Test Project
Hi guys!
I need some help to get the executing folder path from a test project.
c:\projects\myapplication\ MyTests\bi n\Debug\my tests.dll
So when retrieving the path in my Test class
[ClassInitialize]
public static void ClassInit(TestContext context)
{
var dir = ?????;
}
I need to get
c:\projects\myapplication\ MyTests\bi n\Debug\
Plz test it before posting, because I have already tested around 20 diffrent methods, but all returns the path with the TestResults folder incuded.
c:\projects\myapplication\ MyTests\Te stResults\ xxxxxx_201 1-09-12 13_43_43\Out
Thx
I need some help to get the executing folder path from a test project.
c:\projects\myapplication\
So when retrieving the path in my Test class
[ClassInitialize]
public static void ClassInit(TestContext context)
{
var dir = ?????;
}
I need to get
c:\projects\myapplication\
Plz test it before posting, because I have already tested around 20 diffrent methods, but all returns the path with the TestResults folder incuded.
c:\projects\myapplication\
Thx
ASKER
"Test Project" as it says ;)
I guess that shoudl have been obvious ;)
Bad habits I get into often making simple test projects out of other types.
In any case, using reflection and getting the executing assembly should work for you:
string startupPath = System.IO.Path.GetDirector yName(Asse mbly.GetEx ecutingAss embly().Lo cation);
Bad habits I get into often making simple test projects out of other types.
In any case, using reflection and getting the executing assembly should work for you:
string startupPath = System.IO.Path.GetDirector
ASKER
It does not work,, still the TestResults folder is included.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Assembly.GetExecutingAssem bly().Code Base
"file:///C:/WS/MyProject/R elease/Rel -v1.0/Sour ce/MyProje ct/TestRes ults/xxxxx x_2011-09- 12 15_52_07/Out/MyProject.DLL "
"file:///C:/WS/MyProject/R
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thx, works perfect!
But what is the Deployment thing for?
But what is the Deployment thing for?
ASKER
why I need to get that path is I'm storing some xml files that I use for testing in my test project folder, and I do not want to hard code in the path.
But maybe there is another good way to do this?
But maybe there is another good way to do this?
ASKER
This works really well. If I do what Dan7el says.
Found this.
http://msdn.microsoft.com/en-us/library/ms182475.aspx
"If you deploy your tests to a separate folder, you can continue to build your solution while your tests are running."
Don't know if this is anything to care about, when you have projects that takes 1-5 min to build?
But maybe there is a better way to move my xml files to a specifc folder for testing. So I don't need to turn off the Deployment.
Now I like to move it from
c:\projects\myapplication\ MyTests\-= Test Data =-
to
c:\Unittesting\XmlData\
Any better way to do this?
Found this.
http://msdn.microsoft.com/en-us/library/ms182475.aspx
"If you deploy your tests to a separate folder, you can continue to build your solution while your tests are running."
Don't know if this is anything to care about, when you have projects that takes 1-5 min to build?
But maybe there is a better way to move my xml files to a specifc folder for testing. So I don't need to turn off the Deployment.
Now I like to move it from
c:\projects\myapplication\
to
c:\Unittesting\XmlData\
Any better way to do this?
public static string AssemblyDirectory
{
get
{
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
}
}
Well, I don't know if you want to run though all that code each time you access the property.
I would do:
private static string assemblyDirectory = string.Empty;
public static string AssemblyDirectory
{
get
{
if ( string.IsNullOrEmpty( assemblyDirectory ) )
{
string codeBase = Assembly.GetExecutingAssem bly().Code Base;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri .Path);
assemblyDirectory = Path.GetDirectoryName(path );
}
return assemblyDirectory;
}
}
Otherwise, really you can do this whatever you feel is best.
I would do:
private static string assemblyDirectory = string.Empty;
public static string AssemblyDirectory
{
get
{
if ( string.IsNullOrEmpty( assemblyDirectory ) )
{
string codeBase = Assembly.GetExecutingAssem
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri
assemblyDirectory = Path.GetDirectoryName(path
}
return assemblyDirectory;
}
}
Otherwise, really you can do this whatever you feel is best.
ASKER
Thx guys! :)
System.Windows.Forms.Appli
in just about every project I have and it returns the /debug folder.