前提
pytestを使用してpythonソースファイルのテストを行いたいです。
今回テストをしたいモジュールはもともと他のモジュールから定数をimportをして使っているのですが、そのモジュールを呼び出すところでModuleNotFoundErrorが出てしまいます。
発生している問題・エラーメッセージ
ディレクトリ構成と各ファイル内容は以下の通りです。
C:. ├─src │ const.py │ plus.py │ └─tests test_plus.py __init__.py
const.py
1NUM = 100
plus.py
1 2import const 3 4def plusplus(i1,i2): 5 print(i1+i2+const.NUM) 6 return i1+i2+const.NUM 7
test_plus.py
1from src.plus import plusplus 2 3def test_plus(): 4 assert plusplus(1,2) == 100 5
init.pyは何も記述していません。
試したこと
try_pytestディレクトリでpytestコマンドを実行したところ、
const.pyのモジュール読み込みの部分でModuleNotFoundErrorが発生してしまいました。
'C:\Users\xxxxx\Documents\try_pytest\tests\test_plus.py'. Hint: make sure your test modules/packages have valid Python names. Traceback: ..\..\appdata\local\programs\python\python36\lib\importlib\__init__.py:126: in import_module return _bootstrap._gcd_import(name[level:], package, level) tests\test_plus.py:1: in <module> from src.plus import plusplus src\plus.py:2: in <module> import const E ModuleNotFoundError: No module named 'const' ======================================================== short test summary info ========================================================= ERROR tests/test_plus.py !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ============================================================ 1 error in 0.07s ============================================================
パスがうまく認識されていないことが問題だと思うのですが、
python -m pytestなどをつかっても結果は変わりませんでした。
解決方法をご教示いただけますと幸いです。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/09/17 10:09