Python開発用テンプレート。
| ツール種類 | ツール名 |
|---|---|
| 依存関係管理 | Poetry |
| テストフレームワーク | pytest |
| リンター | flake8 |
| フォーマッター | black |
| 型チェック | mypy |
python ./src/app.py
# or
make start※ Poetryはタスクランナー機能がないのでmakeで代用
Poetry導入
開発環境に無ければ入れる。使い方まとめはこちら。
> curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python> (Invoke-WebRequest -Uri https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py -UseBasicParsing).Content | python
poetry installlintやunit testは仮想環境に入って実行する。出るのはexit。
poetry shellmake lint
make ut
make start全部実行
pytest
pytest -v # verbose
pytest -s # 標準出力を出す (--capture=noと同じ)
pytest -ra # サマリーを表示 (テストをpassしたもの以外)
pytest -rA # サマリーを表示 (テストをpassしたものも含む)指定して実行
(テストファイル名, パッケージ名, テストクラス名, メソッド名, 割と何でも拾ってくれる。部分一致でも。)
pytest -k app
pytest -k test_app.py
pytest -k myマーカーを指定して実行
pytest -m 'slow'
pytest -m 'not slow'カバレッジレポートも作成
pytest -v --capture=no --cov-config .coveragerc --cov=src --cov-report=xml --cov-report=term-missing .
# or
make utVSCodeでコードカバレッジを見るには、Coverage Gutters (プラグイン) を入れる。表示されない場合は、コマンドパレットでCoverage Gutters: Display Coverageする。
flake8 --max-line-length=100 --ignore=E203,W503 ./src ./tests
# or
make lintmake docdependabot (GitHub公式) がプルリクを挙げてくるので確認してマージする。
- 最低でもCircleCIが通っているかは確認
- CircleCIでは、最新の依存モジュールでtestするため
poetry updateしてからtestしている - dependabotは
pyproject.tomlとpoetry.lockを更新してくれる