강의

멘토링

커뮤니티

인프런 커뮤니티 질문&답변

sonagi님의 프로필 이미지
sonagi

작성한 질문수

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

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

'TestView' object has no attribute 'author_000'

작성

·

275

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() 함수가 문제인건가 싶긴합니다..

답변 1

1

SungYong Lee님의 프로필 이미지
SungYong Lee
지식공유자

안녕하세요.

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

U가 대문자로요. 

sonagi님의 프로필 이미지
sonagi

작성한 질문수

질문하기