Search code examples
xml.net-coreazure-devopsxunit.netcakebuild

Report .NET Core XUnit tests through Cake build script so that Azure DevOps can use it


I am trying to report the XUnit results of the .NET Core XUnit project in Azure DevOps. The build process is written in a Cake Build script which is called by the Azure DevOps build pipeline. The XUnit tests run, but they are only reporting the bare minimum to the CLI. I want summary and details written to a file: JSON, XML, it doesn't matter too much. Here is sample code currently:

Task("UnitTests")
    .Does(() =>
    {
        DotNetCoreTest(
            testProject,
            new DotNetCoreTestSettings()
            {
                Configuration = configuration,
                NoBuild = true
            }
        );
    });

The Cake script is being run by the Powershell script though a Powershell task in the Build Pipeline.

What do I have to do to run dotnetcoretest in Cake to make it report out to a format and location that I can use Azure DevOps? I tried using the "-xml" argument and that did not work for dotnet test.

Do I have to add a task in the Build Pipeline in Azure DevOps to grab the XUnit results from the Cake script?

How do I view the XUnit tests in Azure DevOps?


Solution

  • You can export in a VSTest format

    Task("UnitTests")
        .Does(() =>
        {
            DotNetCoreTest(
                testProject,
                new DotNetCoreTestSettings()
                {
                    Configuration = configuration,
                    NoBuild = true,
                    NoRestore = true,
                    ArgumentCustomization = args=>args.Append($"--logger trx;LogFileName=\"{testResultsFile}\"")
                }
            );
        });
    

    Then you can either add a task in Azure DevOps similar

    YAML

        steps:
        - task: PublishTestResults@2
          displayName: 'Publish Test Results artifacts/**/test-results/*TestResults*.xml'
          inputs:
            testResultsFormat: VSTest
            testResultsFiles: 'artifacts/**/test-results/*TestResults*.xml'
    

    Designer

    Azure Pipelines publish test results task

    Cake

    Or straight from Cake script use TFBuild.Commands.PublishTestResults(TFBuildPublishTestResultsData)

    TFBuild.Commands.PublishTestResults(
        new TFBuildPublishTestResultsData {
            TestResultsFiles = new []{
                testResultsFile
            },
            TestRunner = TFTestRunnerType.VSTest
        }
    )