테스트 실습2[Elice]
핵심 기능 코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import unittest
def add_review(food, star, comment):
if not (1 <= star <=5):
raise ValueError("별점은 1~5점 사이여야함")
if len(comment) < 5:
raise ValueError("리뷰는 5글자 이상 이여야함")
return f"{food}: {'🎇' * star} - {comment}"
class Test_add_review(unittest.TestCase):
# 좋은 리뷰 남겼을 때
def test_LIKE_add_review(self):
result = add_review("피자",5,"토핑이 적당히 많아서 좋아요") # 이렇게 입력 했을 때
self.assertEqual(result,"피자: 🎇🎇🎇🎇🎇 - 토핑이 적당히 많아서 좋아요") # 입력값이 이렇게 잘 나오는가 테스트
# 안 좋은 리뷰를 남겼다
def test_Un_Like_add_reviwe(self):
result = add_review("치킨",1,"덜익었어요!!")
self.assertEqual(result,"치킨: 🎇 - 덜익었어요!!")
# 별점 누락일 경우
def test_Un_star_review(self): # with 도 예외 처리를 위함이니까 assertRaises 는 에러 발생 여부 확인이고
with self.assertRaises(ValueError) as e: # as e 가 예외 처리 된 객체 as 를 e 에 저장한단 소리임
add_review("짜장면",0,"맛있게 잘 먹었어요~")
self.assertEqual(str(e.exception),"별점은 1~5점 사이여야함")
# 리뷰 글자 누락일 경우
def test_Un_comment_review(self):
with self.assertRaises(ValueError) as e:
add_review("치즈돈까스",5," ")
self.assertEqual(str(e.exception),"리뷰는 5글자 이상 이여야함")
1
2
3
4
5
6
7
python -m unittest test_06.py
> python -m unittest test_06.py
....
----------------------------------------------------------------------
Ran 4 tests in 0.001s
OK
This post is licensed under CC BY 4.0 by the author.