f-lab-edu / realtor

1 stars 0 forks source link

Unit/E2E Test 도입 #21

Closed f-lab-stephen closed 1 year ago

f-lab-stephen commented 1 year ago

배경

CI/CD를 통한 자동 배포를 안전하게 수행하기 위해서는 기능이 이상 동작한다는 사실을 CI 단계에서 어느정도 걸러낼 수 있어야 한다. 이때 사용할 수 있는 대표적인 테스트가 Unit/E2E test이다.

사전 지식

Pytest

Official document: https://docs.pytest.org/en/7.3.x/

목표

각 엔드포인트를 호출했을 때 원하는 값이 나오는지 검증하는 E2E 테스트를 작성한다.

AC

f-lab-stephen commented 1 year ago

이슈에 링크해 드린 블로그는 제가 검증해 보지 않았으니 하시면서 이상한 점이 있으면 검색해 보면서 찾아 보시면 되곘습니다. 공식 도큐먼트를 많이 참조해 보시고요.

sanghunjo921 commented 1 year ago

루트에서 poetry add pytest-django를 통해 pytest 설치 후 pytest.ini 파일만들어서 DJANGO_SETTINGS_MODULE = realtor.realtor.settings 지정했는데 루트에서 poetry run pytest 시 발생하는 에러

File "", line 1206, in _gcd_import File "", line 1178, in _find_and_load File "", line 1128, in _find_and_load_unlocked File "", line 241, in _call_with_frames_removed File "", line 1206, in _gcd_import File "", line 1178, in _find_and_load File "", line 1142, in _find_and_load_unlocked ModuleNotFoundError: No module named 'properties'

해결하기 위해서 명령어를 루트가 아닌 realtor안에서 실행 (이 때 DJANGO_SETTINGS_MODULE = realtor.settings로 설정)시 테스트가 돌아가지만 다음과 같은 3가지 warnings 발생

../../../../Library/Caches/pypoetry/virtualenvs/realtor-aVQjweap-py3.11/lib/python3.11/site-packages/django/http/request.py:1 /Users/sanghunjo/Library/Caches/pypoetry/virtualenvs/realtor-aVQjweap-py3.11/lib/python3.11/site-packages/django/http/request.py:1: DeprecationWarning: 'cgi' is deprecated and slated for removal in Python 3.13 import cgi

../../../../Library/Caches/pypoetry/virtualenvs/realtor-aVQjweap-py3.11/lib/python3.11/site-packages/django/utils/encoding.py:266 /Users/sanghunjo/Library/Caches/pypoetry/virtualenvs/realtor-aVQjweap-py3.11/lib/python3.11/site-packages/django/utils/encoding.py:266: DeprecationWarning: Use setlocale(), getencoding() and getlocale() instead encoding = locale.getdefaultlocale()[1] or 'ascii'

transactions/admin.py:2 /Users/sanghunjo/Desktop/personal_project/realtor/realtor/transactions/admin.py:2: DeprecationWarning: Import from the filter module is deprecated. Use filters module. from rangefilter.filter import DateRangeFilter

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html

관련된 경고문들 검색해봤는데 https://github.com/OSGeo/grass/issues/2538 파이썬 3.11 관련된 issues라고 나오네요.

sanghunjo921 commented 1 year ago

@pytest.mark.django_db def test_user_create(create_user): user = create_user(username='test') assert User.objects.count() == 1 assert user.username == 'test' assert user.phone == '01000000000'

@pytest.mark.django_db def test_user_get(client): response = client.get("http://localhost:8000/users/") assert response.status_code == 200

@pytest.mark.django_db def test_user_post(client): data = { "username": "dd", "password": "wjddk12edaf", "phone": "01000000900" } response = client.post("http://localhost:8000/users/", json=data) assert response.status_code == 200

reverse 이용해서 url 따오는 부분을 알아보고 있는데 잘안되서 일단 이런식으로 하고 있는데 e2e로 진행하게 되면 클래스화 시켜서 그 안에서 crud가 모두 돌아가고, views.py, serializers.py도 테스트인가요? 자료들 찾아보고 있긴한데 e2e 관련 내용이랑 crud에 관련된 내용이 많지는 않네요.

reverse안에 관련된 뷰 func 넣으래서 from users.views.py import userlist, userdetail reverse(userlist, 로 해봤는데 안되네요. pytest에서 reverse를 어떤식으로 쓰는지 몰라서 막힌상태입니다.

sanghunjo921 commented 1 year ago

def test_user_serializer():

serializer = UserSerializer()
f = serializer.fields['username']
obj = User()

assert f.to_representation(obj) == ''
obj.username = "sanghun"
assert f.to_representation(obj) == 'sanghun'