Determining a solution path during test execution

How to determine the solution path from my test code?

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

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

My test code searches for plugins at this location and uploads plugin types to the collection for future use.

At the moment, I need to hard-code the path to the plugins folder in my test, which is unpleasant.

Oh, and I use Visual Studio's built-in test projects (not NUnit), if that matters.

+4
source share
4 answers

The relative path works, but we need to track from the executing binary that is hidden in the test project.

 string solutionPath = Directory .GetParent(Assembly.GetExecutingAssembly().Location) .Parent.Parent.Parent.FullName; string pluginPath = Path.Combine(solutionPath, "TestPlugins"); 

The following is required

 using System.IO; using System.Reflection; 

Not the most elegant solution, but it works.

+2
source

One possible solution would be to create a post-build event for a test project that writes configuration. That is, along the lines:

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

And read the configuration file from your tests.

+1
source

if you know the location of the plugins in relation to the actual solution, you can make a relative path. It will still be hard-coded, but a small tape is better than a 100% hard-coded path. Another option is to have a settings file that shows the path to the plugins regardless of the system, so you can simply read this setting from the file and work with it that way. Let me know if none of them work for you, and I can tell you what I can come up with.

0
source

To add “Stewart Ritchie” (which works beautifully, by the way!) To the answer, the number of “.Parent” arguments depends on how far back you want to return the tree to the running assembly. Therefore, if you continue to receive IOExceptions saying that the directory could not be found, you need to check the value of the returned solution to determine if you need to add or remove some recursion levels to reach the path you are looking for.

- Happy coding!

0
source

All Articles