LoginSignup
1

C#でSonarQubeでカバレッジを表示する

Last updated at Posted at 2023-04-04

1. はじめに

  • SonarQube(自動コードレビューツール)にカバレッジを表示したい
  • カバレッジはOpenCoverで解析して、その内容をSonarQubeへ反映したい

2. 開発環境

  • C#
  • .NET 6
  • Visual Studio 2022
  • SonarQube
  • OpenJDK 17
  • OpenCover
  • ReportGenerator

3. SonarQubeのインストール

  • SonarQube(Community Edition)を下記サイトよりダウンロードしてインストールする

image.png

4. JDK17のインストール

  • SonarQubeを起動するためJavaを使用するため、下記サイトよりダウンロードしてインストールする

image.png

5. SonarQubeを起動

  • SonarQubeをインストールしたフォルダ内のStartSonar.batを起動する

image.png

  • http://localhost:9000\ へアクセスしてLogInボタンをクリックする
    image.png

    • 初期ID: admin
    • 初期パスワード:admin
  • 初期パスワードを変更する
    image.png

  • SonarQubeのTOPページが表示される
    image.png

6. SonarQubeのプロジェクトを作成

  • Manuallyボタンをクリックする
    image.png

  • プロジェクト名を入力する
    image.png

  • TopページのLocallyボタンをクリックする
    image.png

  • トークンをジェネレートする
    image.png

  • SonarQubeのコード解析コマンドを表示する
     image.pg
    image.png

6. SonarQubeのコード解析実行

  • コマンドプロンプトでソリューション直下でSonarQubeのコード解析コマンドを実行する

    • SonarScannerをインストールする
      image.png

    • SonarScanerを実行する
      image.png

    • MSBuildを実行する
      image.png

    • SonarScanner(End)を実行する
      image.png

7. カバレッジ取得の取得

  • 下記を参考にOpenCoverでカバレッジを取得する

8. SonarQubeへのカバレッジ反映

  • SonarScanerのコマンドのパラメータにOpenCoverの出力ファイルの指定を追加する
 /d:sonar.cs.opencover.reportsPaths="CoverageReport.xml"![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2852081/236cd5c4-9030-735b-61b9-51e19c9c75f8.png)
dotnet sonarscanner begin /k:"SampleProject" /d:sonar.host.url="http://localhost:9000"  /d:sonar.login="sqp_31ab32de8ba66a395d52a08602f0fe6a98b673f6" /d:sonar.cs.opencover.reportsPaths="CoverageReport.xml"
  • SonarQubeへカバレッジ結果が反映される
    image.png

image.png

9. 参考文献

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
1