views:

338

answers:

3

Hi,

I want to do some interaction testing in my test project, and would like to test some xml configuration based components... How can I find the base directory of the test project in code so that I can create paths relative to it? I want to have a sub folder that contains the test xml files.

Thanks

EDIT:

Iv used Assembly.GetExecutingAssembly().Location - which gives the out put folder of the time I ran the tests.. I need to configure the xml files to go with the assemblies into that directory.. how can I do that?

A: 
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

Will get you the applications file path, which you can use to load XML files in it's directory.

If you're talking strictly about the project folder, assuming it's in bin\debug, just append a "....\" to the end of it.

Aequitarum Custos
Iv used Assembly.GetExecutingAssembly().Location - which gives the out put folder of the time I ran the tests.. I need to configure the xml files to go with the assemblies into that directory.. how can I do that?
theringostarrs
This will work only for assemblies directly loaded. Assemblies streamed from remote locations will return a URI not a file path. Just FYI.
GrayWizardx
+1  A: 

I'm assuming Application.StartupPath won't work for you either?

Maybe there is a requirment I'm missing but is there some reason you can't just tell the tests where to put the output?

Cory Charlton
A: 

You should juts be able to use relative paths to your project root; for example: doc.Load(@"somedir\my.xml");. The question is: which test framework are you using? With NUnit, most test-runners (in the IDE, such as TestDriven.NET, R#, etc) just use the project-item's properties. So make sure that "Copy to Output Directory" is "Copy Always" or "Copy if Newer".

For the inbuilt MSTest runner that ships with some VS SKUs, things are different; you can use either the testrunconfig or attributes on your test-fixture to tell it what to deploy. Which is silly, IMO. For testrunconfig, double click the testrunconfig and select "Deployment", then "Add File..."; for attributes, you want [DeploymentItem] - as in:

[TestClass, DeploymentItem(@"foo\bar.xml")]
public class SomeTestClass {
    ...
}
Marc Gravell