114๐
See this solution:
from django.utils import unittest
from django.test.client import RequestFactory
class SimpleTest(unittest.TestCase):
def setUp(self):
# Every test needs access to the request factory.
self.factory = RequestFactory()
def test_details(self):
# Create an instance of a GET request.
request = self.factory.get('/customer/details')
# Test my_view() as if it were deployed at /customer/details
response = my_view(request)
self.assertEqual(response.status_code, 200)
51๐
If you are using django test client (from django.test.client import Client
) you can access request from response object like this:
from django.test.client import Client
client = Client()
response = client.get(some_url)
request = response.wsgi_request
or if you are using django.TestCase
(from django.test import TestCase, SimpleTestCase, TransactionTestCase
) you can access client instance in any testcase just by typing self.client
:
response = self.client.get(some_url)
request = response.wsgi_request
- [Django]-Django FileField upload is not working for me
- [Django]-How to resolve "django.core.exceptions.ImproperlyConfigured: Application labels aren't unique, duplicates: foo" in Django 1.7?
- [Django]-Django render_to_string missing information
- [Django]-Paginate relationship in Django REST Framework?
- [Django]-Django Installed Apps Location
- [Django]-Why won't Django use IPython?
0๐
You mean def getEvents(request, eid)
right?
With Django unittest, you can use the from django.test.client import Client
to make request.
See here: Test Client
@Secatorโs answer is prefect as it creates a mock object which is really preferred for a really good unittest. But depending on your purpose, it might be easier to just use Djangoโs test tools.
- [Django]-How to set True as default value for BooleanField on Django?
- [Django]-Django โ why is the request.POST object immutable?
- [Django]-How to run own daemon processes with Django?
0๐
You can use django test client
from django.test import Client
c = Client()
response = c.post('/login/', {'username': 'john', 'password': 'smith'})
response.status_code
response = c.get('/customer/details/')
response.content
for more details
https://docs.djangoproject.com/en/1.11/topics/testing/tools/#overview-and-a-quick-example
- [Django]-Django Unique Together (with foreign keys)
- [Django]-How to use python2.7 pip instead of default pip
- [Django]-What does this Django regular expression mean? `?P`