gpt4 book ai didi

python - 如何在 Django 单元测试中获取请求对象?

转载 作者:IT老高 更新时间:2023-10-28 20:33:44 25 4
gpt4 key购买 nike

我有一个函数

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com