pytest入門

インストール

pip install pytest
pip install pytest-cov

フォルダ構成

tests
├── __init__.py
└── sample

   ├── __init__.py
   |── conftest.py  # セットアップコードを記述
   └── test_code.py # テストケースを記述

テストコードの基本系

セットアップコードの書き方

xxxxxxxxxxx

https://qiita.com/mitamura_kei/items/e51c604155633ccd33dd
setup処理からparameterを引き渡したい
テストケースごとに固有の前後処理をしたい
@pytest.fixture

fixtureを使いこなす

Tips

標準出力に表示する

pytest -s

処理時間を計測する

テスト時間を記録したい
pytest --durations=0
pytest -v --duration=0

前処理と後処理

@pytest.fixture

同じテストを値を変えて行う

@pytest.mark.parametrize

テストをグループ化する

@pytest.mark

テストケース別に結果を表示する
pytest -v

前回NGだったケースだけテストする
pytest -v --lf

前回NGだったケースからテストする
pytest -v --ff

遅いテストケースを見つける
pytest -v --duration=5

テストログをテキストファイルに出力する
pytest -v --result-log=tests/log.txt

カバレッジ計測
https://qiita.com/kg1/items/e2fc65e4189faf50bfe6

コマンドラインでカバレッジを確認
pytest -v --cov=ディレクトリ

コマンドラインで網羅できなかったコード行を知りたい
pytest -v --cov=ディレクトリ --cov-report=term-missing

HTML形式のカバレッジレポートを出力する
pytest -v --cov=ディレクトリ --cov-report=html


トップ   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS