msbuildnunitjenkins-pipelinemsbuild-taskdotcover

Msbuild ignoring build errors


Context

I have a task integration testing and code coverage which I execute in my jenkins pipeline.

The tools used is dotcover and Nunit.

Nunit is executed throught dotcover during the integration test build when the configuration is Integration.

Problem

When I execute the configuration Integration in visual studio with some tests in error, then the build failed, everything are ok, but when the same configuration are executed with msbuild, it doesn't return any error code then jenkins pipelin doesn't fail.

The situation put us in delicate way because we can't trust our build pipeline anymore.

I looking for a solution on web for some days and I still on the same point, it's why I asking for your help here.

Thank you for helping.

Files

jenkinsfile

node('BUILD_PROJECT') {
    stage ('Checkout')
    {
        checkout scm
    }

    stage ('Build')
    {
        bat '"C:/Program Files (x86)/NuGet/nuget.exe" restore -NonInteractive MySolution.sln'
        bat "\"C:/Program Files (x86)/Microsoft Visual Studio/2017/BuildTools/MSBuild/15.0/Bin/MSBuild.exe\" /p:Configuration=Release;AssemblyVersion=0.1.0.${env.BUILD_NUMBER} /maxcpucount:8 MySolution.sln"
    }

    stage ('Integration')
    {
        bat "\"C:/Program Files (x86)/Microsoft Visual Studio/2017/BuildTools/MSBuild/15.0/Bin/MSBuild.exe\" /p:Configuration=Integration /maxcpucount:8 MySolution.sln"
    }

    stage ('Publish Coverage')
    {
         publishHTML target: [
            allowMissing: false,
            alwaysLinkToLastBuild: false,
            keepAll: true,
            reportDir: 'Solution/IntegrationProject/bin/Integration/TestResult',
            reportFiles: 'ProjectCoverageReport.html',
            reportName: 'Project Coverage Report'
          ]
    }

    stage ('Setup')
    {
    bat "\"C:/Program Files (x86)/Microsoft Visual Studio/2017/BuildTools/MSBuild/15.0/Bin/MSBuild.exe\" /p:Configuration=Setup;Platform=x86;AssemblyVersion=0.1.0.${env.BUILD_NUMBER} /maxcpucount:8 MySolution.sln"
    }

    stage ('Archive')
    {
        archiveArtifacts artifacts: 'Solution/SetupProject/bin/x86/Setup/MySetup.exe'
    }
}

In IntegrationProject.csproj

<Target Name="CoverageReport" AfterTargets="CopySqlFiles" Condition="$(Configuration) == Integration">
    <Exec Command="..\packages\JetBrains.dotCover.CommandLineTools.2018.1.3\tools\dotCover.exe analyse /TargetExecutable=..\packages\NUnit.ConsoleRunner.3.8.0\tools\nunit3-console.exe /ReturnTargetExitCode /TargetArguments=&quot;$(TargetPath)&quot; /Filters=-:nunit.framework;-:IntegrationProjectTest;-:type=MyNamespace.View.*;-:type=*Test /TargetWorkingDir=$(TargetDir) /Output=$(TargetDir)\TestResult\MyCoverageReport.html /ReportType=HTML" />
</Target>

Solution

  • You should be able to make use of the dotcover parameter ReturnTargetExitCode to get the return code from nunit.

    <Target Name="CoverageReport" AfterTargets="CopySqlFiles" Condition="$(Configuration) == Integration">
        <Exec Command="..\packages\JetBrains.dotCover.CommandLineTools.2018.1.3\tools\dotCover.exe analyse ^
            /TargetExecutable=..\packages\NUnit.ConsoleRunner.3.8.0\tools\nunit3-console.exe ^
            /ReturnTargetExitCode ^
            /TargetArguments=&quot;$(TargetPath)&quot; ^
            /Filters=-:nunit.framework;-:IntegrationProjectTest;-:type=MyNamespace.View.*;-:type=*Test ^
            /TargetWorkingDir=$(TargetDir) ^
            /Output=$(TargetDir)\TestResult\MyCoverageReport.html ^
            /ReportType=HTML
            /ReturnTargetExitCode">
          <Output TaskParameter="ExitCode" PropertyName="DotCoverExitCode" />
        </Exec>
    
        <Message Text="Unit Tests Failed!" Condition="$(DotCoverExitCode) != '0'"/>  
    
    </Target>