https://docs.djangoproject.com/en/5.0/topics/testing/overview/
unit test
unit test is default, and it comes with django kit
By default, this will discover tests in any file named test*.py under the current working directory.
You can also provide a path to a directory to discover tests below that directory:
$ ./manage.py test animals/
You can specify a custom filename pattern match using the -p (or --pattern) option, if your test files are named differently from the test*.py pattern:
$ ./manage.py test --pattern="tests_*.py"
!!! all the function in the test file test_me.py should have test_ prefix, like def test_my_func(self) :
When you run your tests, the default behavior of the test utility is to find all the test case classes (that is, subclasses of unittest.TestCase) in any file whose name begins with test, automatically build a test suite out of those test case classes, and run that suite.
// example
from django.test import TestCase
from django.contrib.auth import get_user_model
from django.urls import reverse
# Test Client to make request for tests
from rest_framework.test import APIClient
from rest_framework import status
# https://docs.djangoproject.com/en/4.0/ref/urlresolvers/
# path('archive/', views.archive, name='news-archive')
# reverse('news-archive')
# reverse('{{app_name}}:{{path_name}})
CREATE_USER_URL = reverse('user:create')
TOKEN_URL = reverse('user:token')
ME_URL = reverse('user:me')
def create_user(**params):
return get_user_model().objects.create_user(**params)
class PublicUserApiTests(TestCase):
# This always runs first
def setUp(self):
self.client = APIClient()
def test_create_user_success(self):
payload = {
'idp_user_id' : 'test122@test.com',
'password': 'testpwd',
'name': 'Test name'
}
res = self.client.post(CREATE_USER_URL, payload)
self.assertEqual(res.status_code, status.HTTP_201_CREATED)
user = get_user_model().objects.get(**res.data)
self.assertTrue(user.check_password(payload['password']))
No comments:
Post a Comment