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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With