0

Everytime I try to debug unit tests from Rider IDE, test execution fails with an exception

System.IO.FileLoadException: Could not load file or assembly 'testhost, Culture=neutral, PublicKeyToken=null'. Could not find or load a specific file. (Exception from HRESULT: 0x80131621) File name: 'testhost, Culture=neutral, PublicKeyToken=null'

StackTrace:

at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, IntPtr ptrLoadContextBinder)
at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, IntPtr ptrLoadContextBinder)
at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, IntPtr ptrLoadContextBinder)
at System.Reflection.Assembly.Load(AssemblyName assemblyRef)
at Xunit.Runner.VisualStudio.VsTestRunner.GetAvailableRunnerReporters(IEnumerable`1 sources) in C:\Dev\xunit\xunit\src\xunit.runner.visualstudio\VsTestRunner.cs:line 711

Error happens only in one complicated project, with simple projects debugger starts and works properly.

Debugging in Visual Studio works ok. I would appreciate a hint of the direction to look for possible solution.

Basin
  • 887
  • 1
  • 14
  • 28
  • Delete the Debug Folder and recompile, should work from what I've seen – Clint Feb 14 '20 at 06:56
  • Is it possible your file is not prepared at the moment? For example nunit runs testcase preparation before setup.I used to use some file in testcase build up and have to manually copy it before I build up cases. – Louis Go Feb 14 '20 at 08:05
  • Thanks @Clint, I removed `obj` and `bin` folders, recompiled, ran - same error, sorry. – Basin Feb 14 '20 at 08:26
  • @LouisGo, I don't use file system in the tests, thanks. – Basin Feb 14 '20 at 08:29
  • @Basin I meant this test needs an assembly but there is a `FileLoadException`. Is this assembly placed at proper location? – Louis Go Feb 14 '20 at 08:45
  • @Basin, does the error happen only during xunit tests ? – Clint Feb 14 '20 at 09:53
  • @LouisGo, _Could not load file or assembly 'testhost'_ - I don't have project or assembly with name "testhost" – Basin Feb 15 '20 at 05:44
  • @Clint, yes, only when debugging xUnit – Basin Feb 15 '20 at 05:45
  • can you try removing the assembly `testhost`, do a cleam build and restart visual studio and add it back again. sometimes steps as trivial as this would do the trick – Clint Feb 15 '20 at 05:56
  • Is this post related? https://stackoverflow.com/questions/54770830/unable-to-find-testhost-dll-please-publish-your-test-project-and-retry Sorry I don't have exprerience in xUnit test. – Louis Go Feb 15 '20 at 05:56

1 Answers1

0

I got solved this issue by "nuking" whole project :)

After removing whole repository from the disk
- Clone repository back
- Generate Visual Studio solution file again (we don't keep them in repository)
- Build solution
- Run tests
- Debug one of the test - Debugger works properly

I found from some JetBrains forums an advise to remove Micrososft.AspNet.Core folder from dotnet location, while advised approach didn't work, it gave me an idea that the problem can be with this particular solution dependencies.

Basin
  • 887
  • 1
  • 14
  • 28
  • If you don't keep sln file, how do you handle project dependencies? I'd like to know if there is a better way. – Louis Go Feb 16 '20 at 07:23
  • 1
    `dotnet sln add` - [https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-sln](https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-sln). We generate solution file for all project files found in underlying directories. – Basin Feb 17 '20 at 01:14