Link to home
Start Free TrialLog in
Avatar of ulf-jzl
ulf-jzlFlag for Sweden

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\bin\Debug\mytests.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\bin\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\TestResults\xxxxxx_2011-09-12 13_43_43\Out

Thx
Avatar of Snarf0001
Snarf0001
Flag of Canada image

What project type?  For winforms I use

System.Windows.Forms.Application.StartupPath

in just about every project I have and it returns the /debug folder.
Avatar of ulf-jzl

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.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
Avatar of ulf-jzl

ASKER

It does not work,, still the TestResults folder is included.
SOLUTION
Avatar of Snarf0001
Snarf0001
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ulf-jzl

ASKER

Assembly.GetExecutingAssembly().CodeBase

"file:///C:/WS/MyProject/Release/Rel-v1.0/Source/MyProject/TestResults/xxxxxx_2011-09-12 15_52_07/Out/MyProject.DLL"      
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ulf-jzl

ASKER

Thx, works perfect!

But what is the Deployment thing for?
Avatar of ulf-jzl

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?
Avatar of ulf-jzl

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?

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);
    }
}

Open in new window

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.GetExecutingAssembly().CodeBase;
        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.  
Avatar of ulf-jzl

ASKER

Thx guys! :)