Solved

Get project folder path from a Test Project

Posted on 2011-09-12
12
533 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:ulf-jzl
  • 7
  • 3
  • 2
12 Comments
 
LVL 22

Expert Comment

by:Snarf0001
ID: 36522264
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.
0
 

Author Comment

by:ulf-jzl
ID: 36522400
"Test Project"  as it says ;)
0
 
LVL 22

Expert Comment

by:Snarf0001
ID: 36522482
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);
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:ulf-jzl
ID: 36522545
It does not work,, still the TestResults folder is included.
0
 
LVL 22

Assisted Solution

by:Snarf0001
Snarf0001 earned 200 total points
ID: 36522574
?? From ClassInit?  Are you sure you're not resetting it somewhere else?

What does

Assembly.GetExecutingAssembly().CodeBase

return?  It won't be a properly formatted file path, but should be a file:/// url right to the .dll.
0
 

Author Comment

by:ulf-jzl
ID: 36522798
Assembly.GetExecutingAssembly().CodeBase

"file:///C:/WS/MyProject/Release/Rel-v1.0/Source/MyProject/TestResults/xxxxxx_2011-09-12 15_52_07/Out/MyProject.DLL"      
0
 
LVL 20

Accepted Solution

by:
Daniel Van Der Werken earned 300 total points
ID: 36522836
The problem is that you're deploying the test.

In the IDE, from the menu bar, select Test | Edit Test Run Configurations | Local Test Run

In the dialog, select Deployment.  Uncheck the "Enable deployment" checkbox.

Now, the things you've been trying should work, including the .CodeBase solution provided above.
0
 

Author Comment

by:ulf-jzl
ID: 36528422
Thx, works perfect!

But what is the Deployment thing for?
0
 

Author Comment

by:ulf-jzl
ID: 36528453
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?
0
 

Author Comment

by:ulf-jzl
ID: 36528569
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

0
 
LVL 20

Expert Comment

by:Daniel Van Der Werken
ID: 36529434
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.  
0
 

Author Closing Comment

by:ulf-jzl
ID: 36534509
Thx guys! :)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How can i use linq for parsing string datetime to real datetime? 16 51
SqlDataBase 7 54
MailAddress in vb 4 30
I need help adding validation to my MVC.Net view 6 11
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question