LoginSignup
3
2

More than 5 years have passed since last update.

【3分入門】コピペで導入!circle ciでRails のテスト

Posted at

前提

rails
mysql
rspec
circleci

code

最低限の設定に絞ってあります。
mysql の docker が立ち上がるのを待つ必要がありました。

circleci/config.yml
version: 2
jobs:
  build:
    docker:
      - image: circleci/ruby:2.5.3
        environment:
          DATABASE_HOST: 127.0.0.1
          RAILS_ENV: test
          TZ: Asia/Tokyo
      - image: circleci/mysql:8.0.12
        command: [--default-authentication-plugin=mysql_native_password]

    working_directory: ~/repo

    steps:
      - checkout
      - restore_cache:
          keys:
            - v1-dependencies-{{ checksum "Gemfile.lock" }}
            - v1-dependencies-
      - run:
          name: install dependencies
          command: |
            bundle install --jobs=4 --retry=3 --path vendor/bundle
      - save_cache:
          paths:
            - ./vendor/bundle
          key: v1-dependencies-{{ checksum "Gemfile.lock" }}
      - run:
          name: waits for db
          command: dockerize -wait tcp://127.0.0.1:3306 -timeout 120s
      - run: bundle exec rake db:create
      - run: bundle exec rake db:schema:load
      - run:
          name: run tests
          command: |
            mkdir /tmp/test-results
            TEST_FILES="$(circleci tests glob "spec/**/*_spec.rb" | \
              circleci tests split --split-by=timings)"

            bundle exec rspec \
              --format progress \
              --format RspecJunitFormatter \
              --out /tmp/test-results/rspec.xml \
              --format progress \
              $TEST_FILES

      - store_test_results:
          path: /tmp/test-results
      - store_artifacts:
          path: /tmp/test-results
          destination: test-results

注意

環境変数

rails 5.2だと RAILS_MASTER_KEYの環境変数が必要です。
これはcommitするのは危険なので、みなさん別で管理されてると思うのですが、それをcircle ciの設定から登録しないといけません。

mysqlを待つ

mysql の docker が立ち上がるのを待つ必要がありました。
dockerize -wait tcp://127.0.0.1:3306 -timeout 120s
の部分

mysqlの認証方法

password認証?を許すと楽そうでした。

command: [--default-authentication-plugin=mysql_native_password]

rspec format

出力を整えるためにgemを追加しました。

 gem 'rspec_junit_formatter' # for circle ci

この部分変えればなしでもいけそうです。

bundle exec rspec \
--format progress \
--format RspecJunitFormatter \
--out /tmp/test-results/rspec.xml \
--format progress \
$TEST_FILES

3
2
0

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
  3. You can use dark theme
What you can do with signing up
3
2