Skip to content

Instantly share code, notes, and snippets.

@Kritner
Last active December 13, 2016 13:27
Show Gist options
  • Save Kritner/781ab4b708ea5f49271df918c4cca180 to your computer and use it in GitHub Desktop.
Save Kritner/781ab4b708ea5f49271df918c4cca180 to your computer and use it in GitHub Desktop.
Testing Assembly location
// This seems to work with the differences between NUnit runner, ReSharper runner, and dotCover runner - have not yet tested w/ team city dotCover.
/// <summary>
/// NUnit test runner and ReSharper test runner start from different directories. Get a consistent start directory for the runners.
/// </summary>
/// <example>
/// ReSharper:
/// C:\Users\myUser\Documents\project\test\NameSpace.Tests\bin\Release\
/// Nunit:
/// C:\Users\myUser\Documents\project\test\NameSpace.Tests\
/// ReSharper - dotCover
/// ?? Shadow copy directory is this ./bin/Release or ./ ???
/// TeamCity - dotCover
/// ?? - TODO
/// </example>
/// <param name="typeToGetAssemblyInfoFrom">The object to get the assembly information from.</param>
/// <param name="pathAdditions">Path changes to apply after arriving at a consistent start path</param>
public static string GetConsistentTestingStartPath(Type typeToGetAssemblyInfoFrom, string pathAdditions)
{
// drop that file:\\ stuff
var directory = new Uri(
// Get the directory name from the executing assembly
Path.GetDirectoryName(
typeToGetAssemblyInfoFrom.GetTypeInfo().Assembly.Location)
).LocalPath;
// drop the \bin\* from the directory
int binStartIndex = directory.LastIndexOf(@"\bin\", StringComparison.OrdinalIgnoreCase);
if (binStartIndex != -1)
{
directory = directory.Substring(0, binStartIndex + 1);
}
// combine the directory with the pathAdditions (mostly "../../" to get back to the test files.
directory = Path.GetFullPath(directory + pathAdditions);
return directory;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment