如何为.NET项目使用Coverlet设置Sonarcloud并进行代码覆盖率分析。

huangapple go评论61阅读模式
英文:

How to setup Sonarcloud with code coverage for a .NET project using Coverlet

问题

我正在尝试设置一个流水线,运行SonarScanner和带代码覆盖率的单元测试。我无法弄清楚为什么它不起作用。我没有收到任何错误消息,但在SonarCloud上显示0个错误和0%的覆盖率。

Coverlet OpenCover 创建了一个XML文件,实际上确认了有一定的覆盖率。

这是我的存储库:https://github.com/Kwebbel-project/tweet-service/tree/development

这是我的流水线外观:

name: Development

on:
  push:
    branches: ["development"]
  pull_request:
    branches: ["development"]

jobs:
  Analysis:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3
      - name: Setup dotnet
        uses: actions/setup-dotnet@v3
        with:
          dotnet-version: '6.0.x'
          
      - name: Install Sonar global tool
        run: dotnet tool install --global dotnet-sonarscanner
        
      - name: Begin sonar scan
        run: | 
          dotnet sonarscanner begin /k:"Kwebbel-project_tweet-service" /o:"kwebbel-project" /d:sonar.host.url=https://sonarcloud.io /d:sonar.login=${{ secrets.SONAR_TOKEN }} /d:sonar.verbose=true 
        
      - name: Build and run unit tests
        run: |
          dotnet restore
          dotnet build
          dotnet test -p:CollectCoverage=true -p:CoverletOutputFormat=opencover -o="coverage.xml"
                    
      - name: End Sonar scan
        run: dotnet sonarscanner end /d:sonar.login=${{ secrets.SONAR_TOKEN }}
英文:

I'm trying to setup a pipeline that runs sonarscanner, unit tests with code coverage. I can't figure out why it is not working. I'm getting no errors but in sonarcloud is saying 0 errors and 0% coverage.

Coverlet open cover is creating XML file that is actually confirming that there is a percentage covered.

this is my repository: https://github.com/Kwebbel-project/tweet-service/tree/development

This is how my pipeline looks

name: Development

on:
  push:
    branches: [ "development" ]
  pull_request:
    branches: [ "development" ]

jobs:
  Analysis:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3
      - name: Setup dotnet
        uses: actions/setup-dotnet@v3
        with:
          dotnet-version: '6.0.x'
          
      - name: Install Sonar global tool
        run: dotnet tool install --global dotnet-sonarscanner
        
      - name: Begin sonar scan
        run: | 
          dotnet sonarscanner begin /k:"Kwebbel-project_tweet-service" /o:"kwebbel-project"  /d:sonar.host.url=https://sonarcloud.io /d:sonar.login=${{ secrets.SONAR_TOKEN }} /d:sonar.verbose=true 
        
      - name: Build and run unit tests
        run: |
          dotnet restore
          dotnet build
          dotnet test -p:CollectCoverage=true -p:CoverletOutputFormat=opencover -o="coverage.xml"
          
      - name: End Sonar scan
        run: dotnet sonarscanner end /d:sonar.login=${{ secrets.SONAR_TOKEN }}
        

答案1

得分: 1

因此,我无法通过Sonarcloud解决问题,所以我尝试了与Codecov 的另一种集成。这是我的新持续集成流程的外观。

名称: 持续集成

on:
push:
branches: [ "development", "master" ]
pull_request:
branches: [ "development", "master" ]

jobs:
分析:
runs-on: ubuntu-latest

steps:
  - uses: actions/checkout@v3
  - name: 设置dotnet
    uses: actions/setup-dotnet@v3
    with:
      dotnet-version: '6.0.x'
      
  - name: 恢复依赖项
    run: dotnet restore
    
  - name: 运行单元测试并创建覆盖率报告
    run: dotnet test tweet-service.csproj /p:CollectCoverage=true /p:IncludeTestAssembly=true /p:CoverletOutputFormat=opencover /p:ExcludeByFile="**/Microsoft.NET.Test.Sdk.Program.cs"
      
  - name: 将覆盖率报告上传到Codecov
    uses: codecov/codecov-action@v3
    with:
      token: ${{ secrets.CODECOV_TOKEN }}
      fail_ci_if_error: true
      files: ./coverage.opencover.xml
英文:

So I couldn't solve the problem with Sonarcloud so I tried another integration with Codecov. This is how my new Continuous integration pipeline looks.

name: Continuous integration

on:
 push:
   branches: [ "development", "master" ]
 pull_request:
   branches: [ "development", "master" ]

jobs:
 Analysis:
   runs-on: ubuntu-latest

   steps:
     - uses: actions/checkout@v3
     - name: Setup dotnet
       uses: actions/setup-dotnet@v3
       with:
         dotnet-version: '6.0.x'
         
     - name: Restore dependencies
       run: dotnet restore
       
     - name: Run Unit tests and create coverage report
       run: dotnet test tweet-service.csproj /p:CollectCoverage=true /p:IncludeTestAssembly=true /p:CoverletOutputFormat=opencover /p:ExcludeByFile=\"**/Microsoft.NET.Test.Sdk.Program.cs\"
         
     - name: Upload coverage reports to Codecov
       uses: codecov/codecov-action@v3
       with:
         token: ${{ secrets.CODECOV_TOKEN }}
         fail_ci_if_error: true
         files: ./coverage.opencover.xml

huangapple
  • 本文由 发表于 2023年4月11日 17:02:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75984135.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定