thomhurst / TUnit

A modern, fast and flexible .NET testing framework
MIT License
2.12k stars 35 forks source link

Cannot add TUnit as a project reference to test projects #778

Closed vbreuss closed 1 week ago

vbreuss commented 2 weeks ago

I tried using TUnit as a project reference to my test project. I could compile the project, but not execute it in Visual Studio. I get the following error in the Test Output:

Building Test Projects
Starting test discovery for requested test run
========== Starting test discovery ==========
A fatal error was encountered. The library 'hostpolicy.dll' required to execute the application was not found in 'C:\Program Files\dotnet\'.
Failed to run as a self-contained app.
  - The application was run as a self-contained app because 'D:\source\TUnit\TUnit.Assertions.Tests\bin\Debug\net8.0\testhost.runtimeconfig.json' was not found.
  - If this should be a framework-dependent app, add the 'D:\source\TUnit\TUnit.Assertions.Tests\bin\Debug\net8.0\testhost.runtimeconfig.json' file and specify the appropriate framework.
Microsoft.VisualStudio.TestPlatform.ObjectModel.TestPlatformException: Testhost process for source(s) 'D:\source\TUnit\TUnit.Assertions.Tests\bin\Debug\net8.0\TUnit.Assertions.Tests.dll' exited with error: A fatal error was encountered. The library 'hostpolicy.dll' required to execute the application was not found in 'C:\Program Files\dotnet\'.
Failed to run as a self-contained app.
  - The application was run as a self-contained app because 'D:\source\TUnit\TUnit.Assertions.Tests\bin\Debug\net8.0\testhost.runtimeconfig.json' was not found.
  - If this should be a framework-dependent app, add the 'D:\source\TUnit\TUnit.Assertions.Tests\bin\Debug\net8.0\testhost.runtimeconfig.json' file and specify the appropriate framework.
. Please check the diagnostic logs for more information.
   at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.ProxyOperationManager.ThrowOnTestHostExited(IEnumerable`1 sources, Boolean testHostExited) in /_/src/Microsoft.TestPlatform.CrossPlatEngine/Client/ProxyOperationManager.cs:line 524
   at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.ProxyOperationManager.SetupChannel(IEnumerable`1 sources, String runSettings) in /_/src/Microsoft.TestPlatform.CrossPlatEngine/Client/ProxyOperationManager.cs:line 297
   at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.ProxyDiscoveryManager.InitializeDiscovery(DiscoveryCriteria discoveryCriteria, ITestDiscoveryEventsHandler2 eventHandler, Boolean skipDefaultAdapters) in /_/src/Microsoft.TestPlatform.CrossPlatEngine/Client/ProxyDiscoveryManager.cs:line 151
========== Test discovery aborted: 0 Tests found in 58.2 ms ==========
========== Starting test run ==========
A fatal error was encountered. The library 'hostpolicy.dll' required to execute the application was not found in 'C:\Program Files\dotnet\'.
Failed to run as a self-contained app.
  - The application was run as a self-contained app because 'D:\source\TUnit\TUnit.Assertions.Tests\bin\Debug\net8.0\testhost.runtimeconfig.json' was not found.
  - If this should be a framework-dependent app, add the 'D:\source\TUnit\TUnit.Assertions.Tests\bin\Debug\net8.0\testhost.runtimeconfig.json' file and specify the appropriate framework.
Testhost process for source(s) 'D:\source\TUnit\TUnit.Assertions.Tests\bin\Debug\net8.0\TUnit.Assertions.Tests.dll' exited with error: A fatal error was encountered. The library 'hostpolicy.dll' required to execute the application was not found in 'C:\Program Files\dotnet\'.
Failed to run as a self-contained app.
  - The application was run as a self-contained app because 'D:\source\TUnit\TUnit.Assertions.Tests\bin\Debug\net8.0\testhost.runtimeconfig.json' was not found.
  - If this should be a framework-dependent app, add the 'D:\source\TUnit\TUnit.Assertions.Tests\bin\Debug\net8.0\testhost.runtimeconfig.json' file and specify the appropriate framework.
. Please check the diagnostic logs for more information.
Testhost process for source(s) 'D:\source\TUnit\TUnit.Assertions.Tests\bin\Debug\net8.0\TUnit.Assertions.Tests.dll' exited with error: A fatal error was encountered. The library 'hostpolicy.dll' required to execute the application was not found in 'C:\Program Files\dotnet\'.
Failed to run as a self-contained app.
  - The application was run as a self-contained app because 'D:\source\TUnit\TUnit.Assertions.Tests\bin\Debug\net8.0\testhost.runtimeconfig.json' was not found.
  - If this should be a framework-dependent app, add the 'D:\source\TUnit\TUnit.Assertions.Tests\bin\Debug\net8.0\testhost.runtimeconfig.json' file and specify the appropriate framework.
. Please check the diagnostic logs for more information.
========== Test run aborted: 0 Tests (0 Passed, 0 Failed, 0 Skipped) run in < 1 ms ==========

I used the following csproj file:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <ProjectReference Include="..\TUnit\TUnit.csproj" />
  </ItemGroup>
</Project>

When I replace the ProjectReference with a PackageReference, the tests execute successfully.

thomhurst commented 2 weeks ago

Can you try on the latest main and add <IsTestProject>true</IsTestProject> to your property group?

vbreuss commented 1 week ago

Thanks, it's more or less working as expected. The only remaining issue (that I could spot so far) is, that I need to write lots of using statements manuall, that seem to be set via global using when I use the PackageReference instead.

thomhurst commented 1 week ago

Ah yeah. That's fixable. I'll take a look.

thomhurst commented 1 week ago

Should be resolved on latest main