Django Test code 작성하기

ironcat·2022년 3월 17일
0

project

목록 보기
6/6
post-thumbnail

Django test code 작성하기

참고 - Django Test Code 작성하기

Django에서는 unittest를 제공한다고 한다. 위 내용을 바탕으로 test 코드를 작성해보기로 했다. test 하고 싶은 app의 경로로 이동하면 tests.py 파일이 보일 것이다. 해당 파일을 지운 후 tests 폴더를 생성했다. 그다음 테스트 코드를 작성할 파일을 생성했다.

> test_models.py

from django.test import TestCase
from django.contrib.auth import get_user_model

User = get_user_model()

class TestClass(TestCase):
    @classmethod
    def setUpTestData(cls):
        # class 내에서 초기에 한번 실행
        User.objects.create( 
           username='test_user',
           email='test@test.com',
           password='test123')

    def setUp(self):
        # 테스트 메소드 마다 실행
        print('TEST!!')
        pass

    def test_first_name_label(self):
        user = User.objects.get(id=1)
        field_label = user._meta.get_field('first_name').verbose_name
        self.assertEquals(field_label, 'first name')

    def test_first_name_max_length(self):
        user = User.objects.get(id=1)
        max_length = user._meta.get_field('first_name').max_length
        self.assertEquals(max_length, 150)

위 내용은 user table의 first_name field의 label, max_length 값을 비교확인 하는 테스트 코드이다. 결과는 아래와 같다.

간단하게 user model에 대한 test code를 작성 해봤다. 바쁘고 귀찮다보니 미루게 되는데 짜는 습관을 들이면 좋을 것이다.

오늘의 삽질 일기

대충 작성 후 테스트 하려는데 아무리 test를 돌려도 반응이 없는 것이다. 뭐가 문제일까 검색해도 안나오고 __init__.py 10번쯤 다시 만든 것 같음.

그리고 원인은 과거의 내가... command 명령어를 test로... (몽총...)
계속 test 돌려서 compare.txt만 업데이트됨...ㅋㅋㅋ

profile
공부하는 블로그

0개의 댓글