How to use assert_http_302_found method in Django Test Plus

Best Python code snippet using django-test-plus_python

test_views.py

Source:test_views.py Github

copy

Full Screen

...72 setattr(request, 'session', 'session')73 messages = FallbackStorage(request)74 setattr(request, '_messages', messages)75 response = self.post(views.QuestionCreateView, request=request)76 self.assert_http_302_found(response)77 self.assertEqual(response.url, '/qa/')78class TestQuestionDetailView(BaseQATest):79 """测试问题详情"""80 def test_context_data(self):81 response = self.get(views.QuestionDetailView, request=self.request,82 pk=self.question_one.pk)83 self.assert_http_200_ok(response)84 self.assertEqual(response.context_data['question'], self.question_one)85class TestAnswerCreateView(BaseQATest):86 """测试创建回答"""87 def test_get(self):88 """GET请求"""89 response = self.get(views.AnswerCreateView, request=self.request,90 question_id=self.question_one.pk)91 self.assert_http_200_ok(response)92 self.assertContains(response, '编辑')93 self.assertContains(response, '预览')94 self.assertIsInstance(response.context_data['view'], views.AnswerCreateView)95 def test_post(self):96 """POST请求"""97 request = RequestFactory().post('/fake-url/', data={'content': 'content'})98 request.user = self.user99 setattr(request, 'session', 'session')100 messages = FallbackStorage(request)101 setattr(request, '_messages', messages)102 response = self.post(views.AnswerCreateView, request=request, question_id=self.question_one.id)103 self.assert_http_302_found(response)104 self.assertEqual(response.url, '/qa/question-detail/{}/'.format(self.question_one.id))105class TestQAVote(BaseQATest):106 """测试投票功能(问题和回答)"""107 def setUp(self):108 super().setUp()109 self.request = RequestFactory().post('/fake-url/',110 HTTP_X_REQUESTED_WITH='XMLHttpRequest')111 self.request.POST = self.request.POST.copy()112 self.request.user = self.other_user113 def test_question_upvote(self):114 """赞同问题"""115 self.request.POST['question'] = self.question_one.id116 self.request.POST['value'] = 'U'117 response = views.question_vote(self.request)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Django Test Plus automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful