Solved

Get project folder path from a Test Project

Posted on 2011-09-12
12
527 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
 

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 19

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 19

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now