本文介绍了在django单元测试中如何获取请求对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个函数作为
def getEvents(eid, request):
......
现在我要单独测试上述功能的单元测试(不调用视图)。
那么我应该如何在 TestCase
中调用上面的内容。是否可以创建请求?
Now I want to write unit test for the above function separately (without calling the view).So how should I call the above in TestCase
. Is it possible to create request ?
推荐答案
请参阅:
from django.utils import unittest
from django.test.client import RequestFactory
class SimpleTest(unittest.TestCase):
def setUp(self):
# Every test needs access to the request factory.
self.factory = RequestFactory()
def test_details(self):
# Create an instance of a GET request.
request = self.factory.get('/customer/details')
# Test my_view() as if it were deployed at /customer/details
response = my_view(request)
self.assertEqual(response.status_code, 200)
这篇关于在django单元测试中如何获取请求对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!