告诉Django在运行测试时忽略user_passes_test装饰器?

时间:2015-12-08 01:16:09

标签: django django-testing

有没有办法告诉默认的Django测试运行器在运行测试时忽略user_passes_test装饰器?我有以下测试:

# test.py
from django.test import TestCase
class TestFeedbackQuestionResponseFormIT(TestCase):
    def setUp(self):
        self.client = Client()

    def test_feedback_no_error(self):
        self.client.login(username='admin', password='adminpassword')
        form_inputs = {'response_text': '', 'close_issue': False}
        response = self.client.post(reverse('review-feedback-or-question', kwargs={'fqid': 1}), form_inputs)
        self.assertRedirects(response, reverse('review-feedback-or-question-queue'), status_code=302, target_status_code=200)

当我运行此测试时,我收到以下错误:

======================================================================
FAIL: test_feedback_no_error (footer.tests.test_forms.TestFeedbackQuestionResponseFormIT)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/www/proj/footer/tests/test_forms.py", line 13, in test_feedback_no_error
    self.assertRedirects(response, reverse('review-feedback-or-question-queue'), status_code=302, target_status_code=200)
  File "/Users/me/venv/my_venv/lib/python2.7/site-packages/django/test/testcases.py", line 306, in assertRedirects
    (url, expected_url))
AssertionError: Response redirected to 'http://testserver/sign_in/?next=/admin/review/feedback_or_question/1/', expected 'http://testserver/admin/review/feedback_or_question/'

----------------------------------------------------------------------

发生这种情况的原因是因为我在我的urls.py文件中使用了前面提到的装饰器:

# conf/urls.py
url(r'^admin/review/feedback_or_question/(?P<fqid>\d+)/$',
        user_passes_test(lambda u: u.is_staff)(review_feedback_or_question),
        {'template': 'review_feedback_or_question.html'},
        name='review-feedback-or-question'),

我试图在测试开始时登录管理员,但Django的测试跑步者似乎并没有认出它。当我运行测试时,有没有办法告诉Django测试运行器忽略装饰器?

感谢。

0 个答案:

没有答案