Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Test Resources

I'm using Microsoft Visual Studio Test Framework and some of my tests require an xml file as input. Currently I've placed them under a Resource directory in my TestProject and I'm accessing them via a "..\..\whatever" path Name. I would like an advice for a good practice when using test resource files as I would like to reuse my test in all this situations - Directly from VS 2010 - During my build process on the server - In a Continuous Integration environment in a transparent (and simple) way.

like image 832
Terenzio Berni Avatar asked Apr 07 '11 13:04

Terenzio Berni


2 Answers

First of all I typically try to reduce the need to rely on the file system at all during testing (by introducing some sort of abstraction for the file system, so that the file system access can be mocked).

If that is not possible, I include such files in the test project, in a sub folder called TestData or something similar. Then I set the the "Copy to Output Directory" to "Always Copy", so that the file is included in the output, and at a location that is known in relation to the test assembly (regardless of whether the build is done inside Visual Studio or on a build server). This works very well as long as no code under test is modifying files, but only needs them for reading data.

like image 194
Fredrik Mörk Avatar answered Sep 19 '22 19:09

Fredrik Mörk


I would suggest the approach from the following answer. It's a similar question. This approach makes use of the DeploymentItem attribute, also used when doing data driven unit testing.

like image 30
Christophe Lambrechts Avatar answered Sep 22 '22 19:09

Christophe Lambrechts