How do you run .NET tests in CI and publish the results?

IntermediateConcept.NET builds (dotnet CLI)
Answer

Short answer

Run dotnet test with a logger that writes result files (trx or JUnit) and a coverage collector, then let the CI tool read those files to show test results and coverage — and feed coverage to SonarQube.

Example

dotnet test Shop.sln -c Release --no-build \
  --logger "trx;LogFileName=results.trx" \
  --collect:"XPlat Code Coverage" \
  --results-directory ./TestResults

Azure DevOps

- task: DotNetCoreCLI@2
  inputs:
    command: test
    projects: '**/*Tests.csproj'
    arguments: '-c Release --collect:"XPlat Code Coverage"'
- task: PublishCodeCoverageResults@2
  inputs: { summaryFileLocation: '$(Agent.TempDirectory)/**/coverage.cobertura.xml' }

GitHub Actions

Use a JUnit/TRX reporter action (e.g. dorny/test-reporter) to show failed tests on the PR.

Test frameworks

xUnit (most common), NUnit, MSTest — dotnet test runs all of them.

What to say in the interview

"dotnet test with a trx or JUnit logger and the XPlat Code Coverage collector; the pipeline publishes test results and the Cobertura coverage file, which SonarQube also consumes for the quality gate."

All Build Tools (Java, Node.js, Python, .NET) questions Roadmap: .NET apps: dotnet build & publish

More on .NET builds (dotnet CLI)