0

I have a Xunit test project that targets both .NET Core 3.1 and .NET Framework 4.6.1. My test loads an appsettings.json file as detailed by Rick Strahl. When the test runs, even though I have set the file properties to be of Content type and to always copy to output directory, the file isn't actually there when the test runs.

Now I was successfully able to use this technique in another unit test project, but the difference is that one targets .NET Core 3.1 only. In the working test project, Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) is C:\git\myproject\src\myproject.tests\bin\Debug\netcoreapp3.1 (i.e., the build output directory). However, in the failing test, the location is C:\Users\hcheng\AppData\Local\Temp\ffe98278-56d3-41f6-8864-e50c74cb2c08\ffe98278-56d3-41f6-8864-e50c74cb2c08\assembly\dl3\8ba8de87\6b2a96ca_54b8d601 and when I open Windows Explorer to that path, the only contents in that folder are the DLL and the PDB of the unit test project, and __AssemblyInfo__.ini (none of the other dependencies are there either).

I'm totally new to .NET Core so I can't really tell what I'm doing wrong, but I suspect that the multi-targeting is related. Since the other dependencies are missing from that temp location, I'm also worried that if I get past this hurdle of loading the config file, I'll still get TypeLoadExceptions afterwards.

Thanks in advance for your help.

howcheng
  • 2,211
  • 2
  • 17
  • 24

1 Answers1

0

I eventually found the answer in How to refer to test files from Xunit tests in Visual Studio?.

Instead of Assembly.GetExecutingAssembly().Location, I had to use new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath.

howcheng
  • 2,211
  • 2
  • 17
  • 24