Skip to content Skip to sidebar Skip to footer

How To Test Views With Pytest Whose Views Has Loginrequired And Some Specific User Dependencies

I am testing a view and while testing this I am getting this error self = , value = '' def get_prep_value(self, value): fr

Solution 1:

I got my answer actually the main problem was with redirection as @Brachamul suggested me in that question Testing whether a Url is giving 500 error or not in Django , I was being redirected somewhere else which was creating the problem. So the test was not getting passed because of that view. Thanks for giving me your precious time.

Solution 2:

Instead of passing the request object directly to the function based views, you can try self.client.get() method which will simulate a real request coming to your views.

Django provides a test Client to simulate a user interacting with the code at the view level.

from django.test import Client
client = Client()
client.get('/path/to/your/views/')

Solution 3:

I know you have found your answer already but for anyone else with this issue, I suggest using Client.force_login()

Here is an example of how it works:

profile_url = reverse("forum:profile")
forum_user = ForumUser.objects.get(username="admin")
       
profile_data = dict(
    user=forum_user
)

self.client.force_login(forum_user)    
response = self.client.post(profile_url, data=profile_data)

Post a Comment for "How To Test Views With Pytest Whose Views Has Loginrequired And Some Specific User Dependencies"