There is not enough lines to compute coverage with SonarSource/sonarqube-scan-action@v5.2.0

  • ALM used: GitHub
  • Languages of the repository: C#
  • Error observed: the pipeline runs well but the code coverage is not showing, and the following message is shown: There is not enough lines to compute coverage, but actually the PR has more than 100 lines of new code not covered
  • This is the github action:
  • name: SonarCloud Scan
    uses: SonarSource/sonarqube-scan-action@v5.2.0
    env:
    SONAR_TOKEN: ${{ inputs.sonar-token }}
    with:
    args: >
    ${{ inputs.verbose && ‘-X’ || ‘’ }}
    -Dsonar.organization=${{ inputs.sonar-organization }}
    -Dsonar.projectKey=${{ inputs.sonar-project-key }}
    -Dsonar.coverage.reportPaths=coverage.xml
    ${{ inputs.pull-request-key && format(‘-Dsonar.pullrequest.key={0}’, inputs.pull-request-key) || ‘’ }}
    ${{ inputs.pull-request-branch && format(‘-Dsonar.pullrequest.branch={0}’, inputs.pull-request-branch) || ‘’ }}
    ${{ inputs.pull-request-base && format(‘-Dsonar.pullrequest.base={0}’, inputs.pull-request-base) || ‘’ }}
    ${{ inputs.pull-request-key && ‘-Dsonar.pullrequest.provider=github’ || ‘’ }}
    ${{ inputs.github-repository && format(‘-Dsonar.pullrequest.github.repository={0}’, inputs.github-repository) || ‘’ }}

do you know if I’m missing some config or command?

If you’re analyzing .NET code, you cannot use sonarqube-scan-action. Please take another look at the analysis tutorials!

You can find these tutorials again under project Administration > Analysis Method

Hi Colin, thanks for the response, I used the same workflow that the tutorial suggested and the analysis is showing that new code was added and need coverage, (so the message: There is not enough lines to compute coverage disappeared) now I added some tests for that new code, but it seems the analysis is not considering those unit tests, is it necessary to add another configuration to the workflow?

Can you share a screenshot of what you’re seeing?

sure these are the lines that are marked as uncovered


but The project have tests for those lines, and this is the result of the analysis:

these are the test class for that new code:

    public CoverageTestServiceTests()
    {
        _service = new CoverageTestService();
    }

    [Fact]
    public void Add_WithValidInputs_ReturnsSum()
    {
        // Arrange
        var a = 5;
        var b = 3;

        // Act
        var result = _service.Add(a, b);

        // Assert
        Assert.Equal(8, result);
    }

    [Fact]
    public void Subtract_WithValidInputs_ReturnsDifference()
    {
        // Arrange
        var a = 10;
        var b = 4;

        // Act
        var result = _service.Subtract(a, b);

        // Assert
        Assert.Equal(6, result);
    }

    [Fact]
    public void Multiply_WithValidInputs_ReturnsProduct()
    {
        // Arrange
        var a = 6;
        var b = 7;

        // Act
        var result = _service.Multiply(a, b);

        // Assert
        Assert.Equal(42, result);
    }

    [Fact]
    public void Divide_WithValidInputs_ReturnsQuotient()
    {
        // Arrange
        var a = 10;
        var b = 2;

        // Act
        var result = _service.Divide(a, b);

        // Assert
        Assert.Equal(5.0, result);
    }

    [Fact]
    public void Divide_ByZero_ThrowsDivideByZeroException()
    {
        // Arrange
        var a = 10;
        var b = 0;

        // Act & Assert
        var exception = Assert.Throws<DivideByZeroException>(() => _service.Divide(a, b));
        Assert.Equal("Cannot divide by zero", exception.Message);
    }

    [Fact]
    public void GetGreeting_WithValidName_ReturnsPersonalizedGreeting()
    {
        // Arrange
        var name = "John";

        // Act
        var result = _service.GetGreeting(name);

        // Assert
        Assert.Equal("Hello, John!", result);
    }

    [Fact]
    public void GetGreeting_WithNullName_ReturnsAnonymousGreeting()
    {
        // Arrange
        string? name = null;

        // Act
        var result = _service.GetGreeting(name);

        // Assert
        Assert.Equal("Hello, Anonymous!", result);
    }

    [Fact]
    public void GetGreeting_WithEmptyName_ReturnsAnonymousGreeting()
    {
        // Arrange
        var name = "";

        // Act
        var result = _service.GetGreeting(name);

        // Assert
        Assert.Equal("Hello, Anonymous!", result);
    }
}

You need to make sure you’re importing the code coverage data. Find those docs here.

Thanks for sharing this documentation I was able to get the coverage, I want to share the final github workflow that worked for me (in case it can help someone else), just added an step (Install dotnet-coverage) for installing dotnet-coverage which is the tool I wanted to use, and updated the last step (Build and analyze) to let know the scanner where the coverage would be located, then running he actual tool to get the coverage.

  build:
    name: SonarQube analysis
    runs-on: windows-latest
    steps:
      - name: Set up JDK 17
        uses: actions/setup-java@v4
        with:
          java-version: 17
          distribution: 'zulu' # Alternative distribution options are available.
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0  # Shallow clones should be disabled for a better relevancy of analysis
      - name: Cache SonarQube Cloud packages
        uses: actions/cache@v4
        with:
          path: ~\sonar\cache
          key: ${{ runner.os }}-sonar
          restore-keys: ${{ runner.os }}-sonar
      - name: Cache SonarQube Cloud scanner
        id: cache-sonar-scanner
        uses: actions/cache@v4
        with:
          path: .\.sonar\scanner
          key: ${{ runner.os }}-sonar-scanner
          restore-keys: ${{ runner.os }}-sonar-scanner
      - name: Install SonarQube Cloud scanner
        if: steps.cache-sonar-scanner.outputs.cache-hit != 'true'
        shell: powershell
        run: |
          New-Item -Path .\.sonar\scanner -ItemType Directory
          dotnet tool update dotnet-sonarscanner --tool-path .\.sonar\scanner
      - name: Install dotnet-coverage
        shell: powershell
        run: |
          dotnet tool install --global dotnet-coverage
      - name: Build and analyze
        env:
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
        shell: powershell
        run: |
          .\.sonar\scanner\dotnet-sonarscanner begin /k:"<YourProjectKeyHere>" /o:"<YourOrganizationKeyHere>" /d:sonar.token="${{ env.SONAR_TOKEN }}" /d:sonar.cs.vscoveragexml.reportsPaths=coverage.xml
          dotnet build --no-incremental
          dotnet-coverage collect "dotnet test" -f xml -o "coverage.xml"
          .\.sonar\scanner\dotnet-sonarscanner end /d:sonar.token="${{ env.SONAR_TOKEN }}"

1 Like