0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Python】unittestでのModuleNotFoundErrorとAttributeErrorの解決方法

0
Posted at

エラー内容1 ModuleNotFoundError: No module named 'xxx'

  • unittestを実施したらERROR: test_program (unittest.loader._FailedTest)と表示された。
$ python -m unittest test.test_program
E
======================================================================
ERROR: test_program (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: test_program
Traceback (most recent call last):
  File "/opt/homebrew/Cellar/python@3.10/3.10.8/Frameworks/Python.framework/Versions/3.10/lib/python3.10/unittest/loader.py", line 154, in loadTestsFromName
    module = __import__(module_name)
ModuleNotFoundError: No module named 'test.test_program'


----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)

エラー内容1の原因・解決方法

  • 実行している場所が問題でした。実行コマンドではtest.test_program.pyとしていたのに、/Users/aaa/bbb/ccc/python/testで実行していました。/Users/aaa/bbb/ccc/pythonで上記コマンドを実施したら無事にテストできました。
    • 逆に、/Users/aaa/bbb/ccc/python/testであれば、test_program.pyと実行してあげる必要があります

エラー内容2 AttributeError: module 'test.xxx' has no attribute 'py'

$ python -m unittest test.test_program.py
E
======================================================================
ERROR: py (unittest.loader._FailedTest)
----------------------------------------------------------------------
AttributeError: module 'test.test_program' has no attribute 'py'

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)

エラー内容1の原因・解決方法

* 後ろに.pyをつけて実行しているのが原因でした。外して実行したら無事にテストできました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?