πŸ“• Unit Test

may_soouuΒ·2020λ…„ 10μ›” 11일
0

django 둜 ν”„λ‘œμ νŠΈ or 앱을 μƒμ„±ν•˜κ²Œ 되면, test.py λΌλŠ” 파일이 μƒμ„±λœλ‹€. 이 νŒŒμΌμ€ μœ λ‹› ν…ŒμŠ€νŠΈ ν•˜κΈ° μœ„ν•œ νŒŒμΌμ΄λ‹€.

μœ λ‹› ν…ŒμŠ€νŠΈλž€?

λ‚΄κ°€ μž‘μ„±ν•œ μ½”λ“œμ˜ κ°€μž₯ μž‘μ€ λ‹¨μœ„μΈ ν•¨μˆ˜λ₯Ό ν…ŒμŠ€νŠΈν•˜λŠ” λ©”μ†Œλ“œλ‹€.

μ½”λ“œλ₯Ό ν…ŒμŠ€νŠΈ ν•˜λŠ” 방법은 μœ„μ˜ 3가지가 μžˆλ‹€.

UI Tests

  • ν•˜λ‚˜μ˜ νŽ˜μ΄μ§€κ°€ μžˆμ„ λ•Œ κ·Έ νŽ˜μ΄μ§€μ˜ λͺ¨λ“  κΈ°λŠ₯을 ν…ŒμŠ€νŠΈ ν•˜λŠ” 것이닀.
  • 검색 해보고, κ²€μƒ‰ν•œ λ‚΄μš©μ΄ μ œλŒ€λ‘œ λ‚˜μ˜€λŠ”μ§€, ν™”λ©΄μ—μ„œ 잘 λ„μ›Œμ§€λŠ”μ§€, νšŒμ›κ°€μž… 해보고 ,, λ“±λ“±

Integration Tests

  • μ΅œμ†Œ λ‘κ°œμ΄μƒμ˜ 클래슀 λ˜λŠ” μ„œλΈŒ μ‹œμŠ€ν…œμ˜ 결합을 ν…ŒμŠ€νŠΈν•˜λŠ” 방법이닀

Unit Tests

  • κ°€μž₯ λΉ„μš©μ΄ μ €λ ΄ν•˜λ‹€ > μœ λ‹› ν…ŒμŠ€νŠΈλŠ” μ‚¬λžŒμ΄ 슀크립트둜 ν•œκΊΌλ²ˆμ— μžλ™μœΌλ‘œ μ‹€ν–‰ν•˜κΈ° λ•Œλ¬Έ

μƒν’ˆ 리슀트 views 짜 놓은 λ‚΄μš©μ„ λ°”νƒ•μœΌλ‘œ unit testλ₯Ό μ§„ν–‰ν•˜μ˜€λ‹€.

# views

class Allproducts(View):
    def get(self,request):
        product_all = Product.objects.all()
        product_list = [{
            'id'               : product.id,
            'category'         : product.category,
            'name'             : product.name,
            'heart_count'      : product.heart_count,
            'like'             : product.like,
            'retail_price'     : product.retail_price,
            'discount_percent' : product.discount_percent,
            'monthly_pay'      : product.monthly_pay,
            'monthly_payment'  : product.monthly_payment,
        } for product in product_all]

        return JsonResponse({'data':product_list}, status=200)

μœ„μ˜ μ½”λ“œλŠ” 전체 μƒν’ˆ 리슀트λ₯Ό λΆˆλŸ¬μ˜€λŠ” λΆˆλŸ¬μ˜€λŠ” viewsλ‹€.

이λ₯Ό test.py에 μž‘μ„±ν•΄λ³΄λ©΄,

# test.py

import json
from django.test import TestCase, Client

from .models import Product

client = Client()
class AllproductsTest(TestCase):
    def setUp(self):
        Product.objects.create(
            id               = 1,
            category         = 'μ—¬ν–‰',
            name             = '여행을 λ– λ‚˜λŠ” 클래슀',
            heart_count      = 100,
            like             = 900000,
            retail_price     = 900000.0,
            discount_percent = 50,
            monthly_pay      = 5,
            monthly_payment  = 10000,
        )
    def tearDown(self):
        Product.objects.all().delete()

    def test_Allproducts_get_success(self):
        client = Client()
        
# get λ’€μ—λŠ” urls에 μ„€μ •ν•œ κ²½λ‘œλ‹€
        response = client.get('/products')
        self.assertEqual(response.json(),
            {
            "data": [{
            "id": 1,
            "category": "μ—¬ν–‰",
            "name": "여행을 λ– λ‚˜λŠ” 클래슀",
            "heart_count": 100,
            "like": 900000,
            "retail_price" : 900000.0,
            "discount_percent": 50.0,
            "monthly_pay": 5.0,
            "monthly_payment": 10000
        }]
            }
        )
        self.assertEqual(response.status_code, 200)

μ‹€μ œ 데이터λ₯Ό λ„£λŠ” μ½”λ“œμ™€ λ‹€μ‹œ μ§€μš°λŠ” μ½”λ“œ Product.objects.all().delete()
λ„£μ–΄μ£Όκ³ , test_Allproducts_get_success ν•¨μˆ˜λ‘œ, 잘 λŒμ•„κ°€λŠ”μ§€ λŒλ €μ€€λ‹€.

이상이 μ—†λ‹€λ©΄, OK κ°€ 뜰 것이닀.

좜처 : μœ„μ½”λ“œ μ„Έμ…˜ 링크

profile
back-end 개발자

0개의 λŒ“κΈ€