はじめに
共有いただいた下記の記事はうまく行かないパータンを見つけた。補足として、対応方法を共有させていただきました。
Target
- 以下のディレクトリ構成で、github actionsで、「ubuntu環境」と「Docker仮想環境」それぞれの自動テストが無事に実行できること。
ディレクトリ構成
bash.sh
├── .github
│ └── workflows
│ ├── pipenv.yml
│ ├── pytest.yml
│ └── pythonPipenv.yml
├── source
│ ├── modules
│ │ ├── tests
│ │ │ └── test_util.py
│ │ └── util.py
│ ├── static
│ │ └── style.css
│ ├── templates
│ │ ├── error.html
│ │ ├── index.html
│ │ └── template.html
│ ├── logger.py
│ ├── manage.py
│ ├── Pipfile
│ └── test.py
├── .gitattributes
├── docker-compose.yml
├── Dockerfile
├── localenv
└── README.md
共通のテストファイルは前回より、ちょっと変更しました。
test_util.py
from modules import util
def test_sample1():
"""add test """
assert True
def test_count_num():
paramter = 3
result = 3
assert result == util.count_number(paramter)
util.py
def count_number(num):
return num
github Actionsを実行する
実行用なymlファイルは前回の記事まで、ご参考してください。
Docker仮想環境のymlファイル、ubuntu 環境 pytestのymlファイル、ubuntu 環境 pipenvのymlファイル
実行した結果はすべて失敗した~~~
悲しいけど、直さなきゃ。。
まず、エラーメッセージを確認しましょう。
bash.sh
============================= test session starts ==============================
platform linux -- Python 3.10.6, pytest-7.1.3, pluggy-1.0.0 -- /home/runner/.local/share/virtualenvs/python_pipenv_test-ulorymQ-/bin/python
cachedir: .pytest_cache
rootdir: /home/runner/work/python_pipenv_test/python_pipenv_test
collecting ... collected 0 items / 1 error
==================================== ERRORS ====================================
______________ ERROR collecting source/modules/tests/test_util.py ______________
ImportError while importing test module '/home/runner/work/python_pipenv_test/python_pipenv_test/source/modules/tests/test_util.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/opt/hostedtoolcache/Python/3.10.6/x64/lib/python3.10/importlib/__init__.py:126: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
source/modules/tests/test_util.py:1: in <module>
from modules import util
E ModuleNotFoundError: No module named 'modules'
=========================== short test summary info ============================
ERROR source/modules/tests/test_util.py
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
=============================== 1 error in 0.07s ===============================
実行エラーはほぼ同じ内容でした。「No module named 'modules'」モジュールが見つかりませんね。ごめんなさい。。。
各ファイルは以下のように、修正して動作を再確認しました。
Docker仮想環境
docker-compose.yml
environment:
...
- PYTHONPATH=/service/flask_app
???なぜ、docker-compose.ymlファイルを修正しなければならないでしょうか。
Pythonコマンドの実行ディレクトリが違うから、モジュールエラーが出ます。
我々は自動テストはDockerの仮想環境に実行するため、ここのファイル修正しなければなりません。
bash.sh
Run docker-compose exec -T flask_app pipenv run test
============================= test session starts ==============================
platform linux -- Python 3.10.7, pytest-7.1.3, pluggy-1.0.0
rootdir: /service/flask_app
collected 2 items
modules/tests/test_util.py .. [100%]
============================== 2 passed in 0.01s ===============================
Yes 無事に成功できました。
ubuntu 環境
ubuntu pytest
pytest.yml
- name: Analysing the code with pylint
env:
PYTHONPATH: /home/runner/work/python_pipenv_test/python_pipenv_test/source
run: |
ls -l
flake8 source/
pytest -vs
実行結果
bash.sh
============================= test session starts ==============================
platform linux -- Python 3.10.6, pytest-7.1.3, pluggy-1.0.0 -- /opt/hostedtoolcache/Python/3.10.6/x64/bin/python
cachedir: .pytest_cache
rootdir: /home/runner/work/python_pipenv_test/python_pipenv_test
collecting ... collected 2 items
source/modules/tests/test_util.py::test_sample1 PASSED
source/modules/tests/test_util.py::test_count_num PASSED
============================== 2 passed in 0.01s ===============================
Yes 無事に成功できました。
ubuntu pipenv
pythonPipenv.yml
- name: Add path
run: echo "$HOME/.local/share/virtualenvs/python_pipenv_test-ulorymQ-/bin" >> $GITHUB_PATH
- name: Analysing the code with pylint again
env:
PYTHONPATH: /home/runner/work/python_pipenv_test/python_pipenv_test/source
run: |
実行結果
bash.sh
============================= test session starts ==============================
platform linux -- Python 3.10.6, pytest-7.1.3, pluggy-1.0.0 -- /home/runner/.local/share/virtualenvs/python_pipenv_test-ulorymQ-/bin/python
cachedir: .pytest_cache
rootdir: /home/runner/work/python_pipenv_test/python_pipenv_test
collecting ... collected 2 items
source/modules/tests/test_util.py::test_sample1 PASSED
source/modules/tests/test_util.py::test_count_num PASSED
============================== 2 passed in 0.01s ===============================
Yes 無事に成功できました。
まとめ
- 前回のシミュレーションで、Docker仮想環境、ubuntu pytestとubuntu pipenvのメリットとデメリットを書きました。追加します。
Docker仮想環境
メリット
- エンハンスが強い、つまり、テスト環境の修正が発生する際に、dockerfileなどを修正すればOK
ubuntu pytest
デメリット
- エンハンスはdockerより、弱くて、テスト環境の修正が発生する際に、dockerfileとgithub actionsの実行ファイルを修正しなければなりません。
ubuntu pipenv
デメリット
- エンハンスはdockerより、弱くて、テスト環境の修正が発生する際に、dockerfileとgithub actionsの実行ファイルを修正しなければなりません。ただ、pythonのパッケージインストールを関わる部分は「ubuntu pytest」より強いです。