無料でビルドを開始
CircleCI.comアカデミーブログコミュニティサポート

コードカバレッジメトリクスの生成

11 months ago3 min read
クラウド
Server v4.x
Server v3.x
このページの内容

概要

CircleCI では、CircleCI に組み込まれている機能をオープンソースライブラリと組み合わせて、またはパートナーのサービスを使用して、コードカバレッジ レポートのさまざまなオプションを提供しています。 以下のコード例では、各言語のカバレッジライブラリの設定方法や CircleCI Web アプリで コードカバレッジを閲覧する方法を紹介します。

Ruby

SimpleCov は、よく使用される Ruby コードカバレッジライブラリです。 まず、simplecov gem を Gemfile に追加します。

gem 'simplecov', require: false, group: :test

テストスイートの開始時に simplecov を実行します。 以下は SimpleCov を Rails で使用する場合の設定例です。

require 'simplecov'        # << Require SimpleCov
SimpleCov.start 'rails'    # << Start SimpleCov, using the "Rails" preset.

ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
require 'rails/test_help'

class ActiveSupport::TestCase
  # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
  fixtures :all
  # Add more helper methods to be used by all tests here...
end

次に、カバレッジレポートをアップロードするために .circleci/config.yaml ファイルを設定します。

詳細については、 SimpleCov README を参照してください。

Python

Coverage.py は、Python でコードカバレッジレポートを生成する際によく使用されるライブラリです。 まず、以下のように Coverage.py をインストールします。

pip install coverage
# previously you might have run your python project like:
python my_program.py arg1 arg2

# now prefix "coverage" to your command.
coverage run my_program.py arg1 arg2

この では、以下のコマンドを使用してカバレッジ レポートを生成できます。

coverage run -m pytest
coverage report
coverage html  # open htmlcov/index.html in a browser

生成されたファイルは htmlcov/ 下にあり、設定ファイルの store_artifacts ステップでアップロードできます。

Java

JaCoCo は、Java コードカバレッジによく使用されるライブラリです。 以下は、ビルドシステムの一部に JUnit と JaCoCo を含む pom.xml のコード例です。

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.foo</groupId>
    <artifactId>DemoProject</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>DemoProject</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.6</maven.compiler.source>
        <maven.compiler.target>1.6</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.3</version>
                <executions>
                    <execution>
                        <id>prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>report</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>post-unit-test</id>
                        <phase>test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                        <configuration>
                            <!-- Sets the path to the file which contains the execution data. -->

                            <dataFile>target/jacoco.exec</dataFile>
                            <!-- Sets the output directory for the code coverage report. -->
                            <outputDirectory>target/my-reports</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
                <configuration>
                    <systemPropertyVariables>
                        <jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
                    </systemPropertyVariables>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

mvn test を実行するとコードカバレッジレポート (exec) ファイルが生成され、他の多くのカバレッジツールと同様に、このファイルが html ページにも変換されます。 上記の Pom ファイルは target ディレクトリに書き込みを行い、これを CircleCI .circleci/config.yml ファイルでアーティファクトとして保存できます。

上記の例に対応する最小限の CI 設定は以下のとおりです。

JavaScript

Istanbul は、JavaScript プロジェクトでコードカバレッジレポートの生成によく使用されるライブラリです。 人気のテストツールである Jest でも、Istanbul を使用してレポートを生成します。 以下のコード例を参照してください。

PHP

PHPUnit は、よく使用される PHP のテストフレームワークです。 PHP では、 phpdbg というツールへのアクセスが必要です。 レポートは、phpdbg -qrr vendor/bin/phpunit --coverage-html build/coverage-report コマンドを使って生成します。

以下の基本的な .circleci/config.yml では、設定ファイルの最後にある store_artifacts ステップでカバレッジレポートをアップロードしています。

Golang

Go には、コードカバレッジレポートを生成する機能が組み込まれています。 レポートを生成するには、-coverprofile=c.out フラグを追加します。 これでカバレッジレポートが生成され、go tool を使用して html に変換できます。

go test -cover -coverprofile=c.out
go tool cover -html=c.out -o coverage.html

以下は .circleci/config.yml ファイルの例です。

version: 2.1

jobs:
  build:
    docker:
      - image: cimg/go:1.16
        auth:
          username: mydockerhub-user
          password: $DOCKERHUB_PASSWORD  # context / project UI env-var reference
    steps:
      - checkout
      - run: go build
      - run:
          name: "Create a temp directory for artifacts"
          command: |
            mkdir -p /tmp/artifacts
      - run:
          command: |
            go test -coverprofile=c.out
            go tool cover -html=c.out -o coverage.html
            mv coverage.html /tmp/artifacts
      - store_artifacts:
          path: /tmp/artifacts

コードカバレッジ サービスの使用

Codecov

Codecov には、カバレッジレポートのアップロードを簡単に行うための Orb があります。

Codecov Orb はパートナー製の Orb です。 使用するには、お客様ご自身または組織の管理者が、未承認 Orb の使用をオプトインする必要があります。 この設定は、CircleCI Web アプリの Organization Settings > Security で行えます。

version: 2.1
orbs:
  codecov: codecov/codecov@1.0.2
jobs:
  build:
    steps:
      - codecov/upload:
          file: 

Codecov の Orb の詳細については、 ゲストによるブログ記事を参照してください。

Coveralls

Coveralls をご使用の場合は、Coveralls の guide to set up your coverage stats に従ってください。 CircleCI 環境変数COVERALLS_REPO_TOKEN を追加する必要があります。

Coveralls は、同時実行ジョブのカバレッジ統計を自動的にマージします。

CircleCI でのカバレッジの表示

コードカバレッジレポートを直接 CircleCI にアップロードできます。 まず、プロジェクトにカバレッジライブラリを追加し、CircleCI のアーティファクトディレクトリにカバレッジレポートを書き込むようにビルドを設定します。 コードカバレッジレポートはビルドアーティファクトとして、参照またはダウンロード可能な場所に保存されます。 アーティファクトに保存されたカバレッジレポートのダウンロードの詳細については、 ビルドアーティファクトに関するガイドを参照してください。

Artifacts タブのスクリーンショット


Suggest an edit to this page

Make a contribution
Learn how to contribute