如何测试与pytest他们的观点已经LoginRequired和一些特定用户的依赖关系的看法(How

2019-10-31 08:58发布

我测试的景色,而这个测试我得到这个错误

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

而且我觉得我得到,因为request.user.profile.org原因mixer.blend(用户)这个错误会PIC一些用户这是不是在我的数据库意味着一些用户还没有一个配置文件,组织和许多东西。 所以,我想知道我怎么测试这个观点,我怎么给用户一些个人资料和组织(组织)。 还有,我也这样做,像我采取了一些用户这是我的数据库中的信息,并把它传递给#test_views.py - > test_dashboard_view() - > request.user您可以在test_dashboard_view)检查(我行评论.......

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)


Answer 1:

相反,传递的request object直接向function based views ,你可以尝试self.client.get()这将模拟真实的方法request来到你的views

Django提供了一个测试客户端来模拟用户与在视图级的代码进行交互。

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


Answer 2:

我有我的答案其实主要的问题是与重定向为@Brachamul建议我在这个问题测试的URL是否是给500错误或不Django的 ,我被重定向它是创造该问题的其他地方。 因此,试验中未获得通过,因为这一观点的。 谢谢你给我你的宝贵时间。



文章来源: How to test views with pytest whose views has LoginRequired and some specific user dependencies