I am new to c# and unit testing. I have a config file stored in Source folder(on Server). I need to read that file in my Unit test class. This will be running on build server so need to find out a way of working out. Something like GetExecutingAssembly
2
Answers
Add the file to the same project, preferably in a data folder or something like that.
Right click the file in visual studio and select properties, build action set to embedded resource
depending on how defensive you want you load, do something like
Or if not wanting to bring your config to an unknown test server … but looking blindly offset where you are for some reason, you could use the where you are as a navigation point.
Are you using NUnit, MSTest, or something else as your test framework? You can get the directory of your unit test dlls using NUnit’s TestContext.TestDirectory or MSTest’s TestContext.TestRunDirectory. Then I’d add the config file to the test project, and set it to copy to output directory. If the config is somewhere else in your repo tree, maybe add to your test project as a linked file.