-
[Django] Django의 search path for tests 설정django 2022. 6. 14. 14:42
# 문제 상황
: 한 앱의 여러 directory를 만들고 별도의 models.py와 serializers.py, views.py, tests.py 등을 관리하는 형태로 코드를 짜다가, python manage.py test 명령어를 입력하자, 여러 directory에 있는 tests.py를 실행하지 않고, 앱 단위로 tests.py를 search하는 난관에 부딪혔다. manage.py나 settings.py에 따로 test path를 지정해줘야 하나? 자료를 찾아보던 중 좋은 해결법을 찾아서 공유한다.
다음은 필자가 관리하던 app의 directory 구조이다.project - core - __init.py__ - lecture - tests.py - models.py - views.py ... - major - tests.py ... - plan - tests.py ... - requirement - tests.py ... - semester - tests.py ... - migrations - user - __init.py__ - tests.py - models.py - views.py - urls.py - migrations ...
이런 상황에서 python manage.py test를 하니 core와 user 앱의 tests.py만 실행을 한다. 필자는 이들 뿐만 아니라 core 앱 안에 있는 각 directory들의 tests.py도 실행하고 싶었다.
# 해결책
: 이때 그냥 각 directory 별로 빈 파일 형태의 __init__.py를 넣어주었더니 각 directory를 python package로 인식해 django가 해당 directory들까지 tests.py를 탐색해주었다.
project - core - __init.py__ - lecture - __init.py__ - tests.py - models.py - views.py ... - major - __init.py__ - tests.py ... - plan - __init.py__ - tests.py ... - requirement - __init.py__ - tests.py ... - semester - __init.py__ - tests.py ... - migrations - user - __init.py__ - tests.py - models.py - views.py - urls.py - migrations ...
- __init__.py를 각 directory에 넣기 전과 넣은 후
출처 : https://stackoverflow.com/questions/57008770/add-dir-to-django-search-path-for-tests
'django' 카테고리의 다른 글
[개념 정리] Custom Permission 구현과 has_permission과 has_object_permission의 차이 (0) 2022.03.24 [개념 정리] Django 페이지네이션의 종류와 차이 (0) 2022.02.23 [개념 정리] HTTP 메서드 종류와 속성, 차이 (0) 2022.02.22 [개념 정리] HTTP의 개념과 특징 (0) 2022.02.20 [개념 정리] Custom Manager vs Custom Queryset (0) 2022.02.19