我有一个函数
def getEvents(eid, request):
......
现在我想单独为上述函数编写单元测试(不调用 View )。
那么我应该如何在 TestCase
中调用上述内容。是否可以创建请求?
最佳答案
见 this solution :
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)
关于python - 如何在 Django 单元测试中获取请求对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10277748/