def test_login(self):
url = reverse("token_obtain_pair")
user_data = {
"username":"testuser",
"fullname":"테스터",
"email":"test@testuser.com",
"password":"password",
}
response = self.client.post(url, user_data)
print(response.data)
self.assertEEqual(response.status_code, 200)
{'detail': ErrorDetail(string='No active account found with the given credentials', code='no_active_account')}
# 해당 유저가 없는 에러가 발생
F.
======================================================================
FAIL: test_login (users.tests.UserRegisterationAPIViewTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\donggeunim\Desktop\ai2_back (1)\users\tests.py", line 29, in test_login
self.assertEqual(response.status_code, 200)
AssertionError: 401 != 200
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"')
""""""""
from django.contrib.auth.models import AnonymousUser, User
from django.test import RequestFactory, TestCase
from .views import MyView, my_view
class SimpleTest(TestCase):
def setUp(self):
# Every test needs access to the request factory.
self.factory = RequestFactory()
self.user = User.objects.create_user(
username='jacob', email='jacob@…', password='top_secret')
https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Testing#overview
class YourTestClass(TestCase):
def setUp(self):
# Setup run before every test method.
pass
def tearDown(self):
# Clean up run after every test method.
pass
def test_something_that_will_pass(self):
self.assertFalse(False)
def test_something_that_will_fail(self):
self.assertTrue(False)
아래와 같은 방법들로 진행 할 수 있도록 한다.
class LoginUserTest(APITestCase):
def setUp(self):
self.data = {'username':'john', 'password':'johnpassword'}
self.user = User.objects.create_user('john', 'johnpassword')
def test_login(self):
response = self.client.post(reverse('token_obtain_pair'), self.data)
print(response.data["access"])
self.assertEqual(response.status_code, 200)
# class UserManager(BaseUserManager):
# def create_user(self, username, password=None)users.models.py에 있는 이름 사용
Found 2 test(s).
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
가입 # models.py에 있는 print문
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNjY4NDEyOTg1LCJpYXQiOjE2Njg0MTI2ODUsImp0aSI6ImIxODYzYjdhOTAxYjRiMjk4OGJlODNlMzlmNDNjOWM4IiwidXNlcl9pZCI6MSwidXNlcm5hbWUiOiJqb2huIn0._8Fv3IG4CdnMRlZOYlLl59Mk1y3v0zq_pvsrO_32Njs
..
----------------------------------------------------------------------
Ran 2 tests in 0.325s # 2가지 테스트 통
OK
# print문은 test 후 지우기!