views:

30

answers:

3

How would I determine the solution path from within my test code?

I'm trying to write tests for a plugin architecture. I have some fake classes that implement my plugin interface in a separate project within my solution. After these build, the dll is copied into a 'plugins' folder using a post-build event:

copy "$(TargetPath)" "$(SolutionDir)TestPlugins"

My test code looks for plugins in that location and loads plugin types into a collection for later use.

At the moment, I'm having to hard-code the 'plugins' folder path in my test, which is nasty.

Oh, and I'm using Visual Studio's built-in test projects (rather than NUnit), in case that makes a difference.

A: 

One possible solution would be to create a post-build event to the test-project write a config. I.e. along the lines of:

echo $(SolutionDir)TestPlugins > $(TargetDir)PluginConfig.cfg

And read the config file from your tests.

Andreas
beat me to it. I really need to type faster or shorten my answers :P
Adkins
This wouldn't help... In order to read TestPlugins.cfg, I'd still need to hard-code the path to my 'plugins' folder, which is where we just created the new file.
Stewart Ritchie
The trick is to have it as a post build event of the test project (not the plugin project). Unless I'm missing something.
Andreas
A: 

if you know the location of the plugins in relation to the actual solution you could do a relative path. This would still be hard coded, but a small sliver better than a 100% hard coded path. Another option would be to have settings file that states the path to the plugins regardless of system, so you can simply read that setting out of the file and work with it that way. Let me know if neither one of these work for you and I can let you know what elese I can come up with.

Adkins
A: 

The relative path works, but we need to track up from the excuting binary, which is buried away in a test project.

string solutionPath = Directory
    .GetParent(Assembly.GetExecutingAssembly().Location)
    .Parent.Parent.Parent.FullName;

string pluginPath = Path.Combine(solutionPath, "TestPlugins");

Needs the following

using System.IO;
using System.Reflection;

Not the most elegant solution, but it works.

Stewart Ritchie