安裝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