Resolving 'hostpolicy.dll' Issues During xUnit Testing in Azure DevOps

Introduction

Testing an xUnit project in Azure DevOps can present unexpected challenges. One such instance occurred while I was conducting tests via the Azure DevOps Pipeline. I was met with an error message that was not immediately evident from the documentation.

The error:

2023-05-15T18:18:48.2712746Z ##[error]Testhost process for source(s) 'D:\a\1\s\UnitPlanner.Test\bin\Any CPU\Release\net7.0\Microsoft.TestPlatform.CoreUtilities.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\'.
2023-05-15T18:18:48.2714075Z ##[error]Failed to run as a self-contained app.
2023-05-15T18:18:48.2714744Z ##[error]  - The application was run as a self-contained app because 'D:\a\1\s\UnitPlanner.Test\bin\Any CPU\Release\net7.0\testhost.runtimeconfig.json' was not found.
2023-05-15T18:18:48.2715547Z ##[error]  - If this should be a framework-dependent app, add the 'D:\a\1\s\UnitPlanner.Test\bin\Any CPU\Release\net7.0\testhost.runtimeconfig.json' file and specify the appropriate framework.
2023-05-15T18:18:48.2716190Z ##[error]. Please check the diagnostic logs for more information.

This error was traced back to the default YAML configuration for testing in Azure DevOps not aligning perfectly with xUnit. However, the fix for this issue was surprisingly straightforward.

Here is the portion of the YAML file that needed to be altered:

steps:
- task: DotNetCoreCLI@2
  displayName: Test
  inputs:
    command: test
    projects: '**/*test*.dll'
    arguments: '--configuration $(buildConfiguration)'

The modification involved changing projects: '**\*test*.dll' to '**\*test.dll'. With this small adjustment, the tests started running smoothly again.