inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

파이썬 사용자를 위한 웹개발 입문 A to Z Django + Bootstrap

Post List 페이지 테스트 코드 작성하기 part B

'TestView' object has no attribute 'author_000'

280

sonagi

작성한 질문수 6

0

smith라는 User를 만들어서 author_000에 넣어주는 과정에서 다음과같은 에러가 납니다.

Creating test database for alias 'default'...

System check identified no issues (0 silenced).

E

======================================================================

ERROR: test_post_list (blog.tests.TestView)

----------------------------------------------------------------------

Traceback (most recent call last):

  File "C:\Users\User\Documents\folder\github\hallaplantweb\blog\tests.py", line 32, in test_post_list

    author = self.author_000,

AttributeError: 'TestView' object has no attribute 'author_000'

----------------------------------------------------------------------

Ran 1 test in 0.025s

FAILED (errors=1)

Destroying test database for alias 'default'...

아래는 제가 작성한 test.py와 models.py 입니다

from django.test import TestCase, Client
from bs4 import BeautifulSoup
from .models import Post
from django.utils import timezone
from django.contrib.auth.models import User

class TestView(TestCase):
    def setup(self):
        self.client = Client()
        self.author_000 = User.objects.create(username='Smith', password='nopassword')

    def test_post_list(self):
        response = self.client.get('/blog/')
        self.assertEqual(response.status_code, 200)

        soup = BeautifulSoup(response.content, 'html.parser')
        title = soup.title
        
        self.assertEqual(title.text, 'blog')

        navbar = soup.find('div', id='navbar')
        self.assertIn('Blog', navbar.text)
        self.assertIn('About Me', navbar.text)

        self.assertEqual(Post.objects.count(), 0)
        self.assertIn('아직 게시물이 없습니다', soup.body.text)

        post_000 = Post.objects.create(
            title = 'the first post',
            content = 'hello world',
            created = timezone.now(),
            author = self.author_000,
        )

        self.assertGreater(Post.objects.count(), 0)

from django.db import models
from django.contrib.auth.models import User


class Post(models.Model):
    title = models.CharField(max_length=30)
    content = models.TextField()
    head_image = models.ImageField(upload_to='blog/%Y/%m/%d/', blank=True)
    created = models.DateTimeField()
    author = models.ForeignKey(User, on_delete=models.CASCADE)

    def __str__(self):
        return str(self.title) + ' :: ' + str(self.author)

author에서만 말썽인걸보면 User.objects.create() 함수가 문제인건가 싶긴합니다..

bootstrap django python

답변 1

1

SungYong Lee

안녕하세요.

제대로 복사해서 붙이신게 맞다면, setup이 아니라 setUp으로 하면 될 것 같은데요??

U가 대문자로요. 

후속 강의

0

390

1

완성했습니다. 감사합니다

0

321

2

오늘 vps 에 domain 을 지정해주고 아직 활성화되지 않았는데

0

299

1

test 결과 두 가지 error 가 발생했습니다. 뭐가 잘못된 건지 도저히 모르겠습니다.

0

421

1

accounts/login 이 존재하지 않는다고 나옵니다

0

269

2

코드 질문입니다

0

248

1

포스트에 markdown을 사용해서 이미지를 추가할때

0

311

1

New Post 관련 질문드립니다.

0

179

1

이번 영상부터 각 포스트에 프리뷰 사진이 달라지셔서 질문 드립니다.

0

334

1

base 작업한 후 Category가 블로그 하단으로 내려갔습니다.

0

254

1

혹시 소스코드 전체가 담긴 주소를 좀 알 수 있을까요?

0

265

1

9분 경 테스트에서 Assretion Error가 발생합니다.

0

235

1

파이참 초기 설치 후 설정 관련해서 git에 항상 add되는 옵션을 체크했습니다.

0

345

3

F12를 눌러 console 창에서 오류를 확인할때 몇번째 줄인지 나오지 않습니다.

0

396

1

post view도 delete하려는데 막혀서 ㅠㅠ

0

197

1

"GET / HTTP/1.1" 400 143 그리고 "GET / HTTP/1.1" 404 2031

0

3304

3

연결이 안됩니다

0

506

8

서버에 연결하고 나면 그 후에 Cmder가 작동이 안되요

0

378

2

서버에 배포후 어드민계정

0

589

2

안녕하세요! 실서버에 올린 sqlite3의 데이터를 직접 확인하려면 어찌하나요?

0

2039

3

gitignore안먹히는현상

0

292

1

cmder 종료시 서버도 같이 종료됩니다.

0

220

1

' python manage.py makemigration blog ' 명령어 에러

0

588

2

python manage.py makemigrations 를 했을 때 오류가 납니다.

0

560

2