Saving resource files to disk for testing in .NET

How many times have you wanted to write a test which uses a real file (xml, xslx, xls, txt etc.) and got stumped as to how to include it in your project, and in source control AND then pop it in your c:\ temp to reference a filepath. You generally want to do this as this is what a user would be doing in a real life situation and is precidely what you want for integration and regression level tests.

Here’s what to do:

  1. Add your file to your solution/project (I like to create a solution folder, add the file there, and then link to this file in my project, again under a testresources folder or such*)
  2. On the properties for this file keep the build action as “None” and Copy to Output Directory to “Copy Always”
  3. In the project properties in the post-build event add the following line:
    copy "$(TargetDir)\resources\*.xlsx" c:\temp\
  4. Build and hey presto! Your file is in your temp dir!

add as link

What did you think?

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s