AZ-400 Question 120
Single answerYou are creating a CI pipeline in Azure DevOps for a .NET 6 application stored in Azure Repos. The pipeline must run unit tests on a Microsoft-hosted agent, collect code coverage metrics, and display both the test and coverage results in the pipeline summary. Which of the following configurations will best meet these requirements?
- A
Add a 'DotNetCoreCLI@2' task with the 'test' command and '/p:CollectCoverage=true' property, then include a 'PublishTestResults@2' task referencing the generated .trx file.
- B
Add a 'VisualStudioTestPlatformInstaller@1' task to install the test platform, but omit any tasks that explicitly publish test results.
- C
Add a single 'CommandLine@2' task that runs 'dotnet test' with the coverage flag and rely on console output for coverage reporting without publishing results.
- D
Add a 'VSBuild@1' task to build the solution with the 'Enable Code Coverage' property and skip adding any test-specific tasks.
Show answer and explanation
Correct answer: A
Explanation
To properly integrate unit test and coverage results into an Azure DevOps pipeline, you need both a test execution task (in this case, 'DotNetCoreCLI test' with coverage enabled) and a 'PublishTestResults' task that consumes and displays the generated .trx files and coverage data. For reference, see Microsoft Docs: https://docs.microsoft.com/azure/devops/pipelines/tasks/test/publish-test-results for details on publishing test results in Azure Pipelines.
- A. Correct.
Correct. Using the DotNetCoreCLI task with '/p:CollectCoverage=true' generates code coverage data for .NET 6 tests, and the PublishTestResults task integrates those results with the pipeline summary. This approach is recommended for .NET Core/6 projects when using Microsoft-hosted agents.
- B. Incorrect.
Incorrect. Installing the test platform alone will not publish results or coverage data to the pipeline summary. You still need to run tests and publish the outcome using the appropriate tasks.
- C. Incorrect.
Incorrect. While you can run tests with coverage in a single script task, omitting 'PublishTestResults' means the pipeline summary will not display detailed test and coverage data. The data remains only in console logs, which is not ideal for visibility.
- D. Incorrect.
Incorrect. VSBuild tasks are primarily for compiling .NET Framework solutions in Visual Studio. Simply enabling a coverage property at build time without explicitly running and publishing tests will not provide integrated test or coverage reports in the pipeline summary.