1. 程式人生 > 其它 >SonarQube整合Xunit單元測試

SonarQube整合Xunit單元測試

安裝SonarQube

  • 利用docker 安裝SonarQube

    docker run -d --name sonarqube -e SONAR_ES_BOOTSTRAP_CHECKS_DISABLE=true -p 9000:9000 sonarqube:latest
    
    • 服務的記憶體空間要充足
    • 檢查伺服器安裝的JavaSdk版本,要滿足部署後的SonarQube的基礎版本要求,比如SonarQube用的JavaSdk為11,那麼服務就需要安裝JavaSdk為11的版本
  • .NET CORE 中安裝 dotnet-sonarscanner

    dotnet tool install --global dotnet-sonarscanner
    

SonarQube整合Xunit

  • 編寫Xunit 單元測試

  • 單元測試專案使用NuGet安裝: coverlet.msbuild

  • 執行單元測試,生成單元測試覆蓋率檔案,coverage.opencover.xml

    dotnet test 以csproj結尾的檔案位置 
    /p:CollectCoverage=true 
    /p:CoverletOutputFormat=opencover 
    /p:Exclude=\"[xunit.runner.*]\"
    
  • 標記開始上傳單元測試覆蓋率檔案和原始碼分析

    dotnet sonarscanner begin /k:"test" 
    /d:sonar.host.url="http://192.168.16.236:9900"  
    /d:sonar.login="admin" 
    /d:sonar.password="123456" 
    /d:sonar.cs.opencover.reportsPaths= coverage.opencover.xml
    /d:sonar.coverage.exclusions=[**test*.cs,coverage.opencover.xml]
    
  • 編譯構建原始碼

    dotnet build
    
  • 上傳單元測試覆蓋率檔案和原始碼分析

    dotnet sonarscanner end 
    /d:sonar.login="admin" 
    /d:sonar.password=123456