LoginSignup
58

More than 5 years have passed since last update.

CI環境構築 ~ Python編 ~

Last updated at Posted at 2014-04-06

Pythonの継続的インテグレーション環境を無料で簡単にセットアップする方法

利用するサービス

  • Travis CI(無料のCI環境)
  • CoverAll(Travis CIで実行したコードのカバレッジを閲覧できるサービス)
  • Slack(Travis CIからの通知を受け取るチャットサービス)

事前準備

  1. GitHubアカウントの取得
  2. リポジトリ作成
  3. RubyGemのインストール(Travisコマンドインストールに使用)

Travis CIのアカウント取得とリポジトリ選択

  1. 右上のリンクからGitHubアカウントでサインイン
  2. テストしたいリポジトリをONに変更

テストコード

  • tests/hoge_test.py
  • src/fuga.py

tests/hoge_test.py

  1. "test_first"を表示
  2. Fugaクラスのindexメソッドを呼び出してTrueを確認する。
tests/hoge_test.py
# coding:utf-8

import unittest
from fuga import Fuga

class HogeTest(unittest.TestCase):

    def setUp(self):
        print('setUp')

    def test_first(self):
        print('test_first')

    def test_fuga(self):
        fuga = Fuga()
        self.assertTrue(fuga.index())


def suite():
    suite = unittest.TestSuite()
    suite.addTests(unittest.makeSuite(HogeTest))
    return suite

tests/fuga.py

  1. index():Trueを返す。
src/fuga.py
# coding utf-8

class Fuga:

    def index(self):
        return True

設定ファイル

  • setup.py
  • .travis.yml

setup.py

  1. 実行環境の設定スクリプト
  2. sys.path.append()に使用するコードのパスを設定
  3. test_suiteにコード中でsuite.addTests()したテストファイル名を"testfile_name.suite"の形式で指定
setup.py
from setuptools import setup, find_packages
import sys

sys.path.append('./src')
sys.path.append('./tests')

setup(
    name = 'Hoge',
    version = '0.1',
    description='This is test codes for travis ci',
    packages = find_packages(),
    test_suite = 'hoge_test.suite'
)

.travis.yml

  1. Travis CIの設定ファイル
  2. python: バージョン指定
  3. install: pipでcoverallsパッケージをインストール
  4. script: coverageコマンドでテストを実行
  5. after_script: coverallsコマンドを実行
  6. notifications: slackへの通知設定
.travis.yml
language: python

python:
  - 3.3
  - 3.4

install:
  - pip install coveralls

script:
  - coverage run --source=hoge_test setup.py test

after_success:
  - coveralls

notifications:
  slack:
    secure: your_token_key
  • CoverAllとの連携についてはCOVERALLS FOR PYTHONを参照
  • notificationの設定はConfiguring Build Notificationsを参照
  • your_token_keyを暗号化する方法はEncryption keysを参照
    ※ rbenvを使ってる場合はrehashをお忘れなく
  • coveralls-pythonライブラリがpython3.0 ~ 3.2について対応しなくなったので、テストバージョンを3.3以上にする。

GitHubにPush

結果表示についてはCI環境構築 ~ PHP編 ~を参照

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
58