Skip to content

Instantly share code, notes, and snippets.

Created January 16, 2013 16:54
Show Gist options
  • Save anonymous/4548768 to your computer and use it in GitHub Desktop.
Save anonymous/4548768 to your computer and use it in GitHub Desktop.
For unit tests that need to load a file. Locating the file is always a problem, instead make it a resource and then call SerializedResource() to write the files to disk. To locate the file just call GetFullname("file1.txt") to get the true name. #nunit #mstest
private string BasePath
{
get
{
return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
}
}
private void SerializeResources()
{
foreach (var s in Assembly.GetExecutingAssembly().GetManifestResourceNames())
{
string filename = BasePath + "\\" + s;
using (StreamWriter writer = new StreamWriter(filename))
using (var reader = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(s)))
{
writer.Write(reader.ReadToEnd());
}
}
}
private string GetFullname(string filename)
{
return BasePath + "\\" + GetType().Namespace + "." + filename;
}
@joshgo
Copy link

joshgo commented Jan 16, 2013

Add a default constructor to the unit test class. Inside the constructor, call SerializedResources() to write the resources to disk. This will work in NUnit and MsTest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment