작업환경: Ubuntu
디렉토리 구조
.
├── src
│ ├── __init__.py
│ └── foo.py
└── test
└── test_foo.py
위와 같은 구조에서 아래와 같이 pytest 를 하면 바로 에러가 발생한다.
$ pytest test/
=============================================================================================== test session starts ===============================================================================================
platform linux -- Python 3.8.5, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
rootdir: /home/io2oi/test
collected 0 items / 1 error
===================================================================================================== ERRORS ======================================================================================================
________________________________________________________________________________________ ERROR collecting test/test_foo.py ________________________________________________________________________________________
ImportError while importing test module '/home/io2oi/test/test/test_foo.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/lib/python3.8/importlib/__init__.py:127: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
test/test_foo.py:1: in <module>
from src.foo import AAA
E ModuleNotFoundError: No module named 'src'
============================================================================================= short test summary info =============================================================================================
ERROR test/test_foo.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
================================================================================================ 1 error in 0.05s =================================================================================================
위와 같이 src 를 찾지 못하는데 이것은 PYTHONPATH=. 가 설정이 되지 않아서이다.
해결방법
1. test/__init__.py 를 만들어준다.
$ touch test/__init__.py
$ tree
.
├── src
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-38.pyc
│ │ └── foo.cpython-38.pyc
│ └── foo.py
└── test
├── __init__.py
├── __pycache__
│ ├── __init__.cpython-38.pyc
│ └── test_foo.cpython-38-pytest-6.2.2.pyc
└── test_foo.py
$ pytest test/
=============================================================================================== test session starts ===============================================================================================
platform linux -- Python 3.8.5, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
rootdir: /home/io2oi/test
collected 1 item
test/test_foo.py . [100%]
================================================================================================ 1 passed in 0.01s ================================================================================================
위와 같이 test 디렉토리에 __init__.py 가 있으면 그 상위 디렉토리를 우선 sys.path 에 넣는 것으로 보인다.
2. __init__.py 를 만들기 싫다면? python -m pytest 를 한다
하지만 이방법을 사용하면 VScode 에서 pytest 를 사용할 때 문제가 발생한다. 왜냐하면 VScode 내부에서 pytest 를 부를 때는 python -m pytest 를 사용하지 않고 바로 pytest 를 사용하기 때문이다.
이상 위의 정보는 pytest 설명 페이지에서 가지고 왔다.
docs.pytest.org/en/stable/pythonpath.html
pytest import mechanisms and sys.path/PYTHONPATH — pytest documentation
prepend and append import modes scenarios Here’s a list of scenarios when using prepend or append import modes where pytest needs to change sys.path in order to import test modules or conftest.py files, and the issues users might encounter because of tha
docs.pytest.org
'컴퓨터관련 쪽지' 카테고리의 다른 글
python list sorted 사용 시 내 마음대로 우선 순위 정하기 (0) | 2021.03.30 |
---|---|
pytest 에서 code coverage 사용: 얼만큼 코드에 대해서 테스트를 하는가 확인. (0) | 2021.03.25 |
vscode 에서 pylint 사용 (0) | 2021.03.24 |
yahoo mail 을 outlook 에 연결하기 (임시 비밀번호 생성) (0) | 2021.03.24 |
[Azure, jupyter] Azure 클라우드에서 jupyter 서버열고 외부에서 접속하는 방법 (0) | 2021.03.22 |
댓글