问题
I am testing a view and while testing this I am getting this error
self = <django.db.models.fields.AutoField: id>, value = ''
def get_prep_value(self, value):
from django.db.models.expressions import OuterRef
value = super().get_prep_value(value)
if value is None or isinstance(value, OuterRef):
return value
> return int(value)
E ValueError: invalid literal for int() with base 10: ''
/usr/local/lib/python3.6/site-packages/django/db/models/fields/__init__.py:965: ValueError
And I think I am getting this error because of request.user.profile.org cause mixer.blend(User) will pic some User which is not in my database means some User which hasn't a Profile, Org and many things. So, I want to know that how do I test this view, how I give that user some profile and Org(organisation). And also I was doing this like I was taking the info of some User which is in my database and passing it to #test_views.py -> test_dashboard_view()-> request.user which you can check in test_dashboard_view() the lines I have commented.......
views.py
@login_required
def dashboard_view(request):
org = request.user.profile.org
week_responses = day_wise_responses(7, org)
user_org = request.user.profile.org.name
sms_sent = org.sms_counter
email_sent = org.email_counter
today = datetime.today().date()
responses_one_week = number_of_responses(7, org)
responses_two_week = number_of_responses(14, org)
average_rating = org_average_rating(org)
responses_last_week = responses_two_week - responses_one_week
if responses_last_week:
responses_percent_change = (abs(responses_one_week - responses_last_week)/responses_last_week)*100
else:
responses_percent_change = responses_one_week*100
# last n responses
last_5_responses = last_n_responses(5, org)
# print(last_5_responses)
context = {'week_responses': week_responses, 'user_org': user_org, 'today': today,
'responses_one_week': responses_one_week, 'responses_percent_change': responses_percent_change,
'last_5_responses': last_5_responses, 'sms_sent': sms_sent, 'email_sent': email_sent,
'average_rating': average_rating}
return render(request, 'events/dashboard.html', context)
urls.py
path('dashboard/', include('fancy_tsunami.events.urls')),
test_views.py
from events.views import dashboard_view
from django.test import RequestFactory
from django.urls import resolve, reverse
from django import test
import pytest
from django.contrib.auth.models import User, AnonymousUser
@pytest.mark.django_db
class TestViews(test.TestCase):
def test_dashboard_view(self):
path = reverse('event-dashboard')
request = RequestFactory().get(path)
# Org = [{'id': 1, 'name': 'Company', 'logo': None, 'share_google_url': None, 'sharing_destinations_id': None, 'sms_counter': 0, 'email_counter': 0}]
request.user = mixer.blend(User)
# CheckUser = {'id': 3, 'password': 'argon2$argon2i$v=19$m=512,t=2,p=2$bHZkZ3Q0bmE2bEJU$N6x/LFqwI4guA', 'last_login': None, 'is_superuser': True, 'username': 'nitin', 'first_name': '', 'last_name': '', 'email': 'nitin@gmail.com', 'is_staff': True, 'is_active': True, 'date_joined': datetime.datetime(2019, 2, 21, 1, 10, 32, 146)}
# request.user = (CheckUser)
response = dashboard_view(request)
self.assertEqual(response.status_code, 200)
回答1:
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/')
回答2:
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.
来源:https://stackoverflow.com/questions/55546688/how-to-test-views-with-pytest-whose-views-has-loginrequired-and-some-specific-us