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
-
Does this answer your question? [Unit testing: how to access a text file?](https://stackoverflow.com/questions/1805012/unit-testing-how-to-access-a-text-file) – gunr2171 Oct 21 '21 at 17:17
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
using var stream = Assembly .GetAssembly(this.GetType()) .GetManifestResourceStream("<mynamspecase>.<myfilename>.<ext>"); Assert.NotNull(stream); var reader = new StreamReader(stream); string serialized = reader.ReadToEnd(); var dto = System.Text.Json.JsonSerializer.Deserialize<myfiletype>(serialized); Assert.NotNull(dto);
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.
string[] pathParts =
Assembly.GetAssembly(this.GetType()).Location.Split(@"\");
//Go to party
// now your text assembly is in last position, you will not be looking for that
pathParts[pathParts.GetUpperBound(0)] = "";
string basePathWhereWeExecute = string.Join(@"\", pathParts);
var di = new DirectoryInfo(basePathWhereWeExecute);
//You can combine, just jump up and down the path and use the following
FileInfo[] whatFilesAreThere = di.GetFiles();
DirectoryInfo[] whatDirectoriesAreThere = di.GetDirectories();
// I'd suspect you can get more info that just anywhere with a name if it's legit with somebody wanting you to succeed :D

- 835
- 5
- 18
-
presuming you have serialized object in the file else obvisouly you may not want to yield the JsonSerializer :) – T. Nielsen Oct 21 '21 at 17:24
-
The file is present on server and has different location for other developers. We know the folder name containing the config but the path can change. Is there any way I can find the file path on server. – Diya Bhat Oct 21 '21 at 17:32
-
well i suppose, but a unit is that not supposed to be without ties to anything, such a a physical location, but ok i can add how to get the path relative i mean 'the path can change' and we just know the file name .. you are almost certainly not running your code in a context with priviledges to do these things but anyway will update – T. Nielsen Oct 21 '21 at 19:04
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.

- 4,931
- 3
- 38
- 36