python视图的测试用例
我正在views.py中运行此方法的测试用例 views.pypython视图的测试用例,python,django,Python,Django,我正在views.py中运行此方法的测试用例 views.py 69 def delete(request, activityId): 70 """Delete activity form database 71 Return to templates if the owner does not match the user 72 otherwise, return to actuser:activity 73 """ 74 75
69 def delete(request, activityId):
70 """Delete activity form database
71 Return to templates if the owner does not match the user
72 otherwise, return to actuser:activity
73 """
74
75 activity = ActivityViewModel()
76 activity.get_by_id(activityId)
77 userName = activity.db.owner
78
79 deleteThisActivity = activity.delete_from_actinbox(activity.db.owner, request.user.username)
80
81 if deleteThisActivity == True:
82 return HttpResponseRedirect(reverse('actuser:activity', args=(userName,)))
83 else:
84 activityForm = ActivityForm(instance=activity.db)
85 return render(request, 'activity/profile.html', {
86 'message': "You Are Not Allowed To Delete This Activity",
87 'activity': activity.db,
88 'form': activityForm,
89 })
这是我的测试,但我不确定它将如何工作
tests.py
def test_activity_delete(self):
"""test activity that delete from database
"""
delete_activity = create_activity(name="New Activity", userName="esutek")
response = self.client.get(reverse('activity:delete', args=(delete_activity.pk,)))
self.assertEqual(0, delete_activity)
我非常感谢您的帮助。使用以下方法进行测试时会发生什么情况:
self.assertIsInstance(response,HttpResponseRedirect)
正如你所断言的那样
此外,您还需要检查活动是否已实际删除:
val = Activity.get(delete_activity.pk)
self.assertEqual(val, None)
使用以下方法进行测试时会发生什么情况:
self.assertIsInstance(response,HttpResponseRedirect)
正如你所断言的那样
此外,您还需要检查活动是否已实际删除:
val = Activity.get(delete_activity.pk)
self.assertEqual(val, None)
试着看看这东西什么时候会坏;然后编写适当的断言语句
活动\u id
,以及与活动不匹配的用户def test_delete_with_invalid_user_for_activity(self):
c = self.client.login(username='another_user', password='sekret')
u = User.objects.get(username='another_user')
activity = Activity.objects.exclude(db.owner=u)[0]
response = c.get(reverse('activity:delete', args=(activity.pk,)))
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['message'],
'You Are Not Allowed To Delete This Activity')
c.logout()
试着看看这东西什么时候会坏;然后编写适当的断言语句
活动\u id
,以及与活动不匹配的用户def test_delete_with_invalid_user_for_activity(self):
c = self.client.login(username='another_user', password='sekret')
u = User.objects.get(username='another_user')
activity = Activity.objects.exclude(db.owner=u)[0]
response = c.get(reverse('activity:delete', args=(activity.pk,)))
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['message'],
'You Are Not Allowed To Delete This Activity')
c.logout()
你的问题是什么?嗨,我只是想知道如何为这个修改测试用例。我做的对吗?你的问题是什么?嗨,我只是想知道如何为这个测试用例做正确的设置。我做的对吗?