[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 549
  • Last Modified:

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
0
ulf-jzl
Asked:
ulf-jzl
  • 7
  • 3
  • 2
2 Solutions
 
Snarf0001Commented:
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
 
ulf-jzlAuthor Commented:
"Test Project"  as it says ;)
0
 
Snarf0001Commented:
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
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!

 
ulf-jzlAuthor Commented:
It does not work,, still the TestResults folder is included.
0
 
Snarf0001Commented:
?? 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
 
ulf-jzlAuthor Commented:
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
 
Daniel Van Der WerkenIndependent ConsultantCommented:
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
 
ulf-jzlAuthor Commented:
Thx, works perfect!

But what is the Deployment thing for?
0
 
ulf-jzlAuthor Commented:
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
 
ulf-jzlAuthor Commented:
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
 
Daniel Van Der WerkenIndependent ConsultantCommented:
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
 
ulf-jzlAuthor Commented:
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!

  • 7
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now