こすたろーんエンジニアの試行錯誤部屋

作成物の備忘録を書いていきますー

【pytest】assert文と組み合わせた基本的な使い方

python単体テストができるpytestを試してみました
このブログは備忘録です

目次

スポンサーリンク

この記事でわかること

pytestの基本的な使い方

1.実行環境

Jetson Xavier NX
ubuntu18.04
docker
python3.x

2.必要なモジュールをインストール

pip install pytest pytest-cov

3.ファイルとディレクトリを作成

以下のようにファイルとディレクトリを作成します
pytestディレクトリ:テストコード一式を格納
test_function.py:functionのテストコード
(ファイルの先頭にtestをつけるとpytestコマンド実行時に自動でtestが付いたファイルを探して実行してくれます)
function:テスト対象の関数を記述

workspace
├──  pytest/
│    ├── __init__.py
│    └──  test_function.py
│
├── __init__.py
└── function.py

4.function.pyとtest_function.pyにコードを記述

今回はpytestの公式ページにあるコードを流用します

function.py

def inc(x):
    return x + 1

test_function.py

from .. import function
# import function
def test_answer():
    assert function.inc(4) == 5 #←テストが通るように設定

5.テスト実行

function.pyが格納されているディレクトリで以下コマンドを実行します

pytest

実行結果は以下になります

=============================test session starts  =======================================
platform linux -- Python 3.7.16, pytest-7.2.1, pluggy-1.0.0
rootdir: /workspace
plugins: cov-4.0.0, flake8-1.1.1
collected 1 item

pytest/test_function.py .
=============================1 passed in 0.05s =======================================

異常時は以下になります

=============================FAILURES =======================================
_______________________________________________ test_answer _______________________________________

    def test_answer():
>       assert function.inc(3) == 5
E       assert 4 == 5
E        +  where 4 = <function inc at 0x7fafe5e7a0>(3)
E        +    where <function inc at 0x7fafe5e7a0> = function.inc

pytest/test_function.py:6: AssertionError
=============================short test summary info =======================================
FAILED pytest/test_function.py::test_answer - assert 4 == 5
============================= 1 failed in 0.13s ============================================

6. カバレッジ計測

コマンド実行時に以下オプションを受けるとカバレッジ計測できます

pytest --cov .
=============================test session starts  =======================================
platform linux -- Python 3.7.16, pytest-7.2.1, pluggy-1.0.0
rootdir: /workspace
plugins: cov-4.0.0, flake8-1.1.1
collected 1 item

pytest/test_function.py .                                                                                                                                                                                                             [100%]

---------- coverage: platform linux, python 3.7.16-final-0 -----------
Name                      Stmts   Miss  Cover
---------------------------------------------
__init__.py                   0      0   100%
function.py                   2      0   100%
pytest/__init__.py            0      0   100%
pytest/test_function.py       3      0   100%
---------------------------------------------
TOTAL                         5      0   100%


=============================1 passed in 0.15s =======================================

スポンサーリンク

deeplearning関連記事

technoxs-stacker.hatenablog.com technoxs-stacker.hatenablog.com technoxs-stacker.hatenablog.com

参考

pyteyon.hatenablog.com docs.pytest.org