■ はじめに
https://dk521123.hatenablog.com/entry/2019/10/02/223658
https://dk521123.hatenablog.com/entry/2020/01/19/000000
https://dk521123.hatenablog.com/entry/2020/01/20/221014
の続き。 今回は、AWS Glue の 単体試験を調べている際に gluepytestコマンドっていうのがあるらしく、 その元となるPythonの単体試験ライブラリ pytest を調べてみた。
目次
【1】環境設定 【2】Hello world 【3】任意フォルダ(tests)内に単体試験コードを入れる
【1】環境設定
conda install pytest
【2】Hello world
+ hello.py ... テスト対象ファイル + test_hello.py ... 単体試験コード
hello.py
def add(val1, val2): return val1 + val2
test_hello.py
from hello import add def test_add(): assert add(1, 3) == 4
テスト実行
pytest test_hello.py ================= test session starts ================== platform win32 -- Python 3.7.4, pytest-5.2.1, py-1.8.0, pluggy-0.13.0 rootdir: C:\aaa plugins: arraydiff-0.3, doctestplus-0.4.0, openfiles-0.4.0, remotedata-0.3.2 collected 1 item test_hello.py . [100%] ================== 1 passed in 0.05s ===================
【3】任意フォルダ(tests)内に単体試験コードを入れる
* 結構ハマった... * 以下のフォルダ構成にする ~~~~ + hello.py ... テスト対象ファイル + tests/test_hello.py ... 単体試験コード ~~~~
ハマりどころの経緯
* tests/test_hello.py を実行したら、以下のエラーが...
エラー内容
================= test session starts ================== platform win32 -- Python 3.7.4, pytest-5.2.1, py-1.8.0, pluggy-0.13.0 rootdir: C:\aaa plugins: arraydiff-0.3, doctestplus-0.4.0, openfiles-0.4.0, remotedata-0.3.2 collected 0 items / 1 errors ======================== ERRORS ======================== _________ ERROR collecting tests/test_hello.py _________ ImportError while importing test module 'C:\aaa\tests\test_hello.py'. Hint: make sure your test modules/packages have valid Python names. Traceback: tests\test_hello.py:3: in <module> from hello import add E ModuleNotFoundError: No module named 'hello' !!!!!!! Interrupted: 1 errors during collection !!!!!!!! =================== 1 error in 0.12s ===================
対応案
* tests配下に「__init__.py(空ファイル)」を作成する ~~~~ + hello.py ... テスト対象ファイル + tests/test_hello.py ... 単体試験コード + tests/__init__.py ... エラー対策 ~~~~
参考文献
https://namonakimichi.hatenablog.com/entry/2019/06/08/205202
参考文献
https://rinatz.github.io/python-book/ch08-02-pytest/
関連記事
単体試験 / pytest ~ 基本編 / Fixture ~
https://dk521123.hatenablog.com/entry/2021/11/24/163751
単体試験 / pytest ~ 基本編 / pytest-mock ~
https://dk521123.hatenablog.com/entry/2021/11/25/131615
単体試験 / unittest ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2019/10/02/223658
単体試験 / unittest ~ 基本編 ~
https://dk521123.hatenablog.com/entry/2021/03/31/000000
単体試験 / mox ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2020/01/19/000000
単体試験 / nose ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2020/01/20/221014
AWS Glue ~ ローカル環境を作成する ~
https://dk521123.hatenablog.com/entry/2019/11/10/205535