search test django code example
Example 1: test if class-based view exists unittest
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):
self.factory = RequestFactory()
self.user = User.objects.create_user(
username='jacob', email='jacob@…', password='top_secret')
def test_details(self):
request = self.factory.get('/customer/details')
request.user = self.user
request.user = AnonymousUser()
response = my_view(request)
response = MyView.as_view()(request)
self.assertEqual(response.status_code, 200)
Example 2: run all test of app django
$ ./manage.py test animals.tests
$ ./manage.py test animals
$ ./manage.py test animals.tests.AnimalTestCase
$ ./manage.py test animals.tests.AnimalTestCase.test_animals_can_speak