Unit Testing POST request

236 views
Skip to first unread message

onlinejudge95

unread,
Feb 12, 2020, 7:53:51 AM2/12/20
to django...@googlegroups.com
Hi Devs,

I was implementing unit-tests in my Django project and stumbled upon the following issue.

I want to unit-test my POST route. I do not want to use the test client already shipped with Django (using it in my e2e tests). I want to know how do I prepare my request object to pass to my view. Here is what I have done currently.

test_views.py
class CreateBlogTest(BaseViewTest):
    @classmethod
    def setUpClass(cls):
        cls.request.method = "POST" 
    def test_create_valid_blog(self):
        self.request.content_type = "application/json"
        self.request._body = json.dumps({"title": "new title", "body": "new body"})

        response = views.blog_collection(self.request)
        self.assertEqual(response.status_code, 201)

In my view, I am accessing the data through request.data and passing it to a serializer.

In my current setting, I am getting a 400 error message when I have checked that the user does not exist.

Any suggestions regarding the same?

Thanks,
onlinejudge95

onlinejudge95

unread,
Feb 12, 2020, 2:29:32 PM2/12/20
to django...@googlegroups.com
Hi Guys,

Any leads would be appreciated

onlinejudge95

unread,
Feb 12, 2020, 3:16:16 PM2/12/20
to django...@googlegroups.com
On Wed, Feb 12, 2020 at 6:22 PM onlinejudge95 <online...@gmail.com> wrote:
In case if someone needs it in the future, go and look at django.test.RequestFactory 

Adam Mičuda

unread,
Feb 13, 2020, 9:32:44 AM2/13/20
to django...@googlegroups.com
Hi,
or you can extract the business logic from view to some service and unit test it instead. =)

Regards.

Adam

st 12. 2. 2020 v 21:15 odesílatel onlinejudge95 <online...@gmail.com> napsal:
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAD%3DM5eTN3M5iAEvkPoB1fAi%3Du%3DOAXv8kr7S51HmaBsNd8Tubyg%40mail.gmail.com.

sachinbg sachin

unread,
Feb 13, 2020, 9:44:12 AM2/13/20
to django...@googlegroups.com
Fistly for setup create use instance for setup user credentials then call that the reverse url in that post put delite

onlinejudge95

unread,
Feb 13, 2020, 2:30:41 PM2/13/20
to django...@googlegroups.com
On Thu, Feb 13, 2020 at 8:02 PM Adam Mičuda <adam....@gmail.com> wrote:
Hi,
or you can extract the business logic from view to some service and unit test it instead. =)

I am following the same, it's just that I am also performing serialization as of now in my views, since I want to push it out first, and have documented the refactor I would be performing. My question was on how to prepare a post request with proper payload, while i was running this test my response object was an empty dictionary.  

onlinejudge95

unread,
Feb 13, 2020, 2:33:40 PM2/13/20
to django...@googlegroups.com
On Thu, Feb 13, 2020 at 8:14 PM sachinbg sachin <sachinb...@gmail.com> wrote:
Fistly for setup create use instance for setup user credentials then call that the reverse url in that post put delite

I have already created my test users in the setUpClass() method. As mentioned my problem was not something related to URL dispatching but more on how to prepare a POST request with the proper payload. I looked into the documentation and RequestFactory is exactly what i was looking for.

Reply all
Reply to author
Forward
0 new messages