- 테스트 주도 개발 준비
python manage.py test
아무런 테스트 미션을 주지 않아서 성공한다고 나옴.
- blog/test.py
from django.test import TestCase
# Create your tests here.
class TestView(TestCase):
def test_post_list(self):
self.assertEqual(2, 3)
다시 실행해보면
- 여기서 assertEqual(2, 2)라고 하면 ok 메세지가 나온다.
- beautifulsoup4 설치하기
pip install beautifulsoup4
포스트 목록 페이지 테
스트하기
- blog/test.py
from django.test import TestCase, Client
from bs4 import BeautifulSoup
from .models import Post
# Create your tests here.
class TestView(TestCase):
def setUp(self):
self.client = Client()
def test_post_list(self):
# 1.1 포스트 목록 페이지를 가져온다.
response = self.client.get('/blog/')
# 1.2 정상적으로 페이지가 로드된다.
self.assertEqual(response.status_code, 200)
# 1.3 페이지 타이틀은 'Blog'이다.
soup = BeautifulSoup(response.content, 'html.parser')
self.assertEqual(soup.title.text, 'Blog')
# 1.4 네비게이션 바가 있다.
navbar = soup.nav
# 1.5 Blog, About Me라는 문구가 내비게이션 바에 있다.
self.assertIn('Blog', navbar.text)
self.assertIn('About Me', navbar.text)
# 2.1 메인 영역에 게시물이 하나도 없다면
self.assertEqual(Post.objects.count(), 0)
# 2.2 '아직 게시물이 없습니다'라는 문구가 보인다.
main_area = soup.find('div', id='main-area')
self.assertIn('아직 게시물이 없습니다', main_area.text)
# 3.1 게시물이 2개 있다면
post_001 = Post.objects.create(
title='첫 번째 포스트 입니다.',
content='Hello World. We are the world'
)
post_002 = Post.objects.create(
title='두 번째 포스트 입니다.',
content='1등이 전부는 아니잖아요?'
)
self.assertEqual(Post.objects.count(), 2)
# 3.2 포스트 목록 페이지를 새로고침했을 때
response = self.client.get('/blog/')
soup = BeautifulSoup(response.content, 'html.parser')
self.assertEqual(response.status_code, 200)
# 3.3 메인 영역에 포스트 2개의 타이틀이 존재한다.
main_area = soup.find('div',id='main-area')
self.assertIn(post_001.title, main_area.text)
self.assertIn(post_002.title, main_area.text)
# 3.4 '아직 게시물이 없습니다'라는 문구는 더 이상 보이지 않는다.
self.assertNotIn('아직 게시물이 없습니다', main_area.text)
- blog/templates/blog/post_list.html
<div class="container my-3">
<div class="row">
<div class="col-md-8 col-lg-9", id='main-area'>
<h1>Blog</h1>
{%if post_list.exists%}
{% for p in post_list %}
<!-- Blog Post -->
<div class="card mb-4">
{%if p.head_image%}
<img class="card-img-top" src="{{p.head_image.url}}" alt="{{p}} head image">
{%else%}
<img class="card-img-top" src="https://picsum.photos/seed/{{p.id}}/800/200" alt="random_image">
{%endif%}
<div class="card-body">
<h2 class="card-title">{{p.title}}</h2>
{% if p.hook_text %}
<h5 class="text-muted">{{p.hook_text}}</h5>
{%endif%}
<p class="card-text">{{p.content | truncatewords:45}}</p>
<a href="{{p.get_absolute_url}}" class="btn btn-primary">Read More →</a>
</div>
<div class="card-footer text-muted">
Posted on {{p.created_at}} by
<a href="#">작성자명 쓸 위치(개발예정)</a>
</div>
</div>
{%endfor%}
{%else%}
<h3>아직 게시물이 없습니다.</h3>
{%endif%}
포스트 상세 페이지 테스트하기
- test.py에 테스트할 내용 나열하기
def test_post_detail(self):
# 1.1 포스트가 하나 있다.
post_001 = Post.objects.create(
title='첫 번째 포스트입니다.',
content = 'Hello World. We are the world.'
)
# 1.2 그 포스트의 url은 '/blog/1'이다.
self.assertEqual(post_001.get_absolute_url(), '/blog/1/')
# 2. 첫번째 포스트의 상세 페이지 테스트
# 2.1 첫번째 포스트의 url로 접근하면 정상적으로 작동한다.
response = self.client.get(post_001.get_absolute_url())
self.assertEqual(response.status_code,200)
soup = BeautifulSoup(response.content, 'html.parser')
# 2.2 포스트 목록 페이지와 똑같은 내비게이션 바가 있다.
navbar = soup.nav
self.assertIn('Blog', navbar.text)
self.assertIn('About Me', navbar.text)
# 2.3 첫 번째 포스트의 제목이 웹 브라우저 탭 타이틀에 들어 있다.
self.assertIn(post_001.title, soup.title.text)
# 2.4 첫 번째 포스트의 제목이 포스트 영역에 있다.
main_area = soup.find('div', id='main-area')
post_area = soup.find('div', id='post-area')
self.assertIn(post_001.title, post_area.text)
# 2.5 첫 번째 포스트의 작서자(author)가 포스트 영역에 있다.
# 2.6 첫번째 포스트의 내용(content)이 포스트 영역에 있다.
self.assertIn(post_001.content, post_area.text)
'Programming > Django' 카테고리의 다른 글
내비게이션 바와 푸터 모듈화하기 (0) | 2023.04.09 |
---|---|
장고 프로젝트 새팅 다른 로컬에서 진행하기 (0) | 2023.04.09 |
장고(Django) 페이지 구성 개선하기 (0) | 2023.03.19 |
장고(Django) 정적 파일과 미디어 파일 관리하기 (0) | 2023.03.18 |
장고(Django) 웹 페이지 만들기 (0) | 2023.03.18 |