I have a function as
def getEvents(eid, request):
......
Now I want to write unit test for the above function separately (without calling the view).
So how should I call the above in TestCase
. Is it possible to create request ?
I have a function as
def getEvents(eid, request):
......
Now I want to write unit test for the above function separately (without calling the view).
So how should I call the above in TestCase
. Is it possible to create request ?
If you are using django test client (
from django.test.client import Client
) you can access request from response object like this:or if you are using
django.TestCase
(from django.test import TestCase, SimpleTestCase, TransactionTestCase
) you can access client instance in any testcase just by typingself.client
:Use
RequestFactory
to create a dummy request.You can use django test client
for more details
https://docs.djangoproject.com/en/1.11/topics/testing/tools/#overview-and-a-quick-example
See this solution:
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.