How to sonarscan and fail workflow due to failing test? (Github)

Hello,

I have my .Net project on Github setup in sonarcloud. The code coverage scan is working, however, the workflow no longer fails due to a failing test. In my setup prior to sonarqube the workflow will fail after dotnet test has a failing test. Now that dotnet test is wrapped inside the sonarscanner the workflow is successful even if there is a failing test. How do I scan my code and have the workflow fail when a test fails? I’m using Coverlet to generate the coverage report.

Bumping for visibility and to note that I’m still interested in resolving this issue or figuring out what I’m doing wrong.

Hello Aaron,

can you please post the pipeline snippets. The interesting part is the dotnet test invocation.

Thank you.

Hi,

Thanks for your response, here is the scan and test part of my pipeline.

      - name: Build, Test and Analyze
        env:
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
        shell: pwsh
        run: |
          ~/.sonar/scanner/dotnet-sonarscanner begin /k:"my-project" /o:"my-project-1" /d:sonar.token="${{ secrets.SONAR_TOKEN }}" /d:sonar.cs.opencover.reportsPaths=/**/coverage.opencover.xml /d:sonar.host.url="https://sonarcloud.io"
          dotnet build -c ${{ env.BUILD_CONFIGURATION }} --no-restore -p:Version=${{ inputs.build-version }}
          dotnet test -c ${{ env.BUILD_CONFIGURATION }} --no-build --logger:trx --verbosity normal --collect:"XPlat Code Coverage;Format=opencover"
          ~/.sonar/scanner/dotnet-sonarscanner end /d:sonar.token="${{ secrets.SONAR_TOKEN }}"

Hi,

It looks like the problem is that dotnet test is not the last command in the powershell script. The $LASTEXITCODE of dotnet test is therefore overwritten by dotnet-sonarscanner end and the script returns with “exit code = 0”.

You should try

dotnet test
$testResult = $LASTEXITCODE
dotnet-sonarscanner end
exit $testResult

Note: I haven’t tested this.

1 Like

This is the solution! Thank you Martin.

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.