Solved

Get project folder path from a Test Project

Posted on 2011-09-12
12
539 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
  • 2
12 Comments
 
LVL 23

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 23

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

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

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

717 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