DEV Community

Yuta Goto
Yuta Goto

Posted on

CircleCIのPythonOrbsを使ってみる。

CircleCIのPythonOrbsを使って、CIでテストを実行させるものを作ります。

作業リポジトリ


やったこと

Pipfileを用意

今回はmypypytestpycodestyleを使います。

[[source]] url = "https://pypi.org/simple" verify_ssl = true name = "pypi" [packages] [dev-packages] mypy = "*" pytest = "*" pycodestyle = "*" [requires] python_version = "3.8" 
Enter fullscreen mode Exit fullscreen mode

サンプルを用意

# app.py def greeting(name: str) -> str: return 'hello ' + name print(greeting('name')) 
Enter fullscreen mode Exit fullscreen mode

テストファイルを用意

# test_app.py import app def test_greeting(): assert app.greeting('name') == 'hello name' 
Enter fullscreen mode Exit fullscreen mode

ファイルを用意したら pushします。

CircleCIの準備

https://app.circleci.com

CircleCIにアクセスして、左メニューのProjectsからリポジトリを選択してセットアップします。

(ブラウザ上で .circleci/config.yml を編集できるの便利)

editor

デフォルトでセットされている

orbs: python: circleci/python@0.2.1 
Enter fullscreen mode Exit fullscreen mode

はorbのバージョンは結構古いので、Orbsのページを見て最新バージョンに書き換えます(2021/03/31時点では 1.3.3 が最新)

https://circleci.com/developer/orbs/orb/circleci/python

steps内にある load-cache, save-cache は今は1.3.3では使えない(ライブラリのインストール時に自動でキャッシュしてくれる)ので削除します。

ライブラリのインストールは、今回作ったサンプルではpipenvを使うのでネストさせた要素の pkg-manager:pipenv を書きます(CI環境なので引数に --dev を付けます)。

Usage Examplesには、テストを実行させるために python/test を使うような記述がありますが、stepsでは使わずにjobsに直接指定します。

できあがった .circleci/config.yml が以下です。

# .circleci/config.yml version: 2.1 orbs: python: circleci/python@1.3.3 jobs: build-and-test: executor: python/default steps: - checkout - python/install-packages: args: --dev pkg-manager: pipenv - run: name: mypy command: pipenv run mypy . - run: name: pycodestyle command: pipenv run pycodestyle . workflows: main: jobs: - build-and-test - python/test: args: --dev test-tool: pytest pkg-manager: pipenv 
Enter fullscreen mode Exit fullscreen mode

テストを実行する部分で python/install-packages とほぼ同じ記述になってしまうのはどうにかしたい部分はあります。。

CI Result

CI Result

Top comments (0)