Client()
객체는 request나 http와 비슷하게 작동 클라이언트로서 동작합니다.TestCase()
객체를 상속받아 클래스를 생성한다.test_
시작해야지 안그러면 인식하지않습니다.def add (x,y):
returnx+y
unittest
from django.test import TestCase, Client
import unittest
import app.name.py
client = Clinet()
class calTest(unittest.TestCase):
def test_add(self):
result = calc.add(10,5)
self.assertEqual(result, 15)
if __name__ == '__main__':
unittest.main()
example of unittest
from django.test import TestCase
from myapp.models import Animal
class AnimalTestCase(TestCase):
def setUp(self):
Animal.objects.create(name="lion", sound="roar")
Animal.objects.create(name="cat", sound="meow")
def test_animals_can_speak(self):
"""Animals that can speak are correctly identified"""
lion = Animal.objects.get(name="lion")
cat = Animal.objects.get(name="cat")
self.assertEqual(lion.speak(), 'The lion says "roar"')
self.assertEqual(cat.speak(), 'The cat says "meow"')