Python開発用テンプレート。VSCodeのRemote Containers での開発用。
.devcontainer/devcontainer.json
- Remote Containersの設定:公式のベースを修正
.devcontainer/Dockerfile
- イメージのビルド設定:公式のベースを修正
.devcontainer/dotfiles
- zshの設定等
kind | tool |
---|---|
shell | zsh |
zsh framework | prezto |
prompt | powerlevel10k |
kind | tool |
---|---|
package management | pip |
testing framework | pytest |
linter | flake8 |
formatter | black |
type check | mypy |
※ コンテナなのでPythonの仮想環境 (PoetryとかPipenvとか) は使わない
※M1 Macでのみ動確
- ローカルにコンテナ環境を用意 (Macだと
Rancher Desktop
が簡単) - VSCodeに「Remote - Containers」プラグインをインストール
git clone
してVSCodeでプロジェクトを開く- VSCodeの画面左下の緑ゾーンをクリック
Reopen in Container
をクリック- コンテナ内でVSCodeが開いたら準備完了 (初回は時間かかる)
make lint
make ut
make start
python ./main/my/app.py
# もしくは
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=main --cov-report=xml --cov-report=term-missing .
# もしくは
make ut
VSCodeでコードカバレッジを見るにはプラグイン(Coverage Gutters)が必要(導入済み)。表示されない場合は、コマンドパレットでCoverage Gutters: Display Coverage
する。
flake8 --max-line-length=100 --ignore=E203,W503 ./main
# もしくは
make lint
requirements.txt
に追記Rebuild Container
注意:pip install
で追加すると下記警告が出る場合がある。--upgrade
するとbin
以下が消えて既に導入済みのパッケージが使えなくなる。このため上記手順が無難。
WARNING: Target directory /home/../. already exists. Specify --upgrade to force replacement.
- 【2020年1月】令和だし本格的にVSCodeのRemote Containerで、爆速の"開発コンテナ"始めよう - Qiita
- VSCode Remote Containersに自分のdotfilesを持ち込む - Kesinの知見置き場
- Remote Container拡張の設定でdotfilesをコピーする機能があるが使ってない
- Dockerfileで
COPY
している
- Configuration — pytest documentation
- Usage and Invocations — pytest documentation
- VSCodeでカバレッジを表示する(pytest-cov)